2015-09-25 196 views
0

我在骰子游戲中遇到麻煩。我有一個任務:C程序骰子游戲

遊戲的規則如下: 1.玩家擲骰子並加起來的面值。 2.如果第一個擲骰是7或11,則玩家獲勝。 3.如果第一個擲骰是2,3或12,玩家就會放鬆。 4.如果第一個擲骰是任何其他數字,那麼該總和就成爲玩家的點數。 5.要獲勝,玩家必須繼續擲骰子直到他/她「得分。」 6.玩家在點前擲骰子7。

1)在程序中定義WON和LOST宏。使用WON的值爲0,LOSE的值爲1 2)使用函數原型int rollDice(void);實現函數。

rollDice()應使用RAND()隨機生成1點之間的數 - 6

返回()

3由蘭特產生的數目)實施的功能,與函數原型int瑣事(空隙);

當播放機準備好打,他(她)會用Enter鍵擲骰子

如果用戶勝在他/她的第一個卷,祝賀播放機和帶WON

返回

如果用戶在第一次擲骰子時放鬆,祝賀玩家,並用LOSE返回

讓用戶繼續遊戲直到他贏/輸,直到他/她輸掉一個合適的按摩並以最後一次擲骰值結束遊戲。

4)你的main()應該 調用函數瑣事()

詢問用戶是否他(她)想要繼續玩另一個遊戲,保持損失的數字軌跡,贏得

當用戶決定完成比賽時,顯示他所贏得的比賽數量。

給用戶根據勝或負他(她)有

返回與EXIT_SUCCESS

這裏的值的數量相應的消息是我現在,但它告訴我,有是錯誤的。任何人都可以請幫我完成這個任務嗎?

#include <stdio.h> 
#include <ctype.h> 
#include <time.h> 
#include <stdlib.h> 

#define WON 0 
#define LOSE 1 

int rollDice(void); 
int playGame(void); 

int rollDice(void) { 
    return ((rand() % 6) + 1); 
} 

int playGame(void){ 
    int dice_1 = 0; 
    int dice_2 = 0; 
    int sum = 0; 
    time_t t; 
    srand(time(&t)); 
    printf("ROLL THE DICE WITH [ENTER]\n"); 
    dice_1 = rollDice(); 
    dice_2 = rollDice(); 
    sum = dice_1 + dice_2; 
    if (sum == 7 || sum == 11){ 
    printf("Congratulations you roll %d and WON at your first try!", sum); 
    } 
    else { 
    printf("Your roll was %d ,you lose try agian.\n", sum); 
    } 
    return 0; 
} 

int main (void){ 
    playGame(); 
} 

錯誤的是(在GCC的Linux):

x.c:9:1:錯誤:雜散「\ 302」在程序

INT rollDice(無效);

^

x.c:9:1:錯誤:雜散「\ 240」在程序

X。c:10:1:錯誤:在程序中丟失'\ 302'

int playGame(void);

^

XC:10:1:錯誤:雜散 '\ 240' 在程序

XC:12:1:錯誤:雜散 '\ 302' 在程序

INT rollDice(空隙){

^

XC:12:1:錯誤:雜散 '\ 240' 在程序

XC:16:1:錯誤:16:1:錯誤:雜散 '\ 240' 在程序在程序

INT瑣事(無效){

^

XC雜散 '\ 302'

+1

作爲題外話,因爲沒有「做我的家工作「網站。 –

+0

我投票關閉這個問題 – Olaf

+1

什麼不行? – xxbbcc

回答

1

這裏有一些錯誤。

  1. 您沒有在閱讀/使用從playGame()返回的值。您應該存儲結果並對其執行操作。
  2. 你的邏輯不完整,因爲「玩點」的標準和損失都是相同的。
  3. 您沒有任何強制程序等待用戶按下的地方ENTER

我在下面爲您列出了完整的代碼清單。

代碼


/******************************************************************************* 
* Preprocessor directives 
******************************************************************************/ 
#include <stdio.h> 
#include <ctype.h> 
#include <time.h> 
#include <stdlib.h> 
#include <stdbool.h> 

#define WON 0 
#define LOSE 1 


/******************************************************************************* 
* Function prototypes 
******************************************************************************/ 
int rollDice(void); 
int playGame(void); 


/******************************************************************************* 
* Function definitions 
******************************************************************************/ 
/*----------------------------------------------------------------------------*/ 
int rollDice(void) { 
    return ((rand() % 6) + 1); 
} 

/*----------------------------------------------------------------------------*/ 
int playGame(void){ 
    int dice_1 = 0; 
    int dice_2 = 0; 
    int sum = 0; 
    int result; 
    int point = 0; 
    time_t t; 
    bool playForPoint = false; 

    srand(time(&t)); // Initialize random seed 
    printf("ROLL THE DICE WITH [ENTER]\n"); 
    fgetc(stdin); 
    dice_1 = rollDice(); 
    dice_2 = rollDice(); 
    sum = dice_1 + dice_2; 
    printf("D1:%2d - D2:%2d - Sum:%2d\n", dice_1, dice_2, sum); 

    switch (sum) 
    { 
     case 7: 
     case 11: 
      result = WON; 
      break; 
     case 2: 
     case 3: 
     case 12: 
      result = LOSE; 
      break; 
     default: 
      playForPoint = true; 
      point = sum; 
      printf("Playing for point:%d. Please hit enter.\n", point); 
      fgetc(stdin); 
      break; 
    } 

    while (playForPoint) 
    { 
     dice_1 = rollDice(); 
     dice_2 = rollDice(); 
     sum = dice_1 + dice_2; 
     printf("D1:%2d - D2:%2d - Sum:%2d\n", dice_1, dice_2, sum); 
     if (sum == 7) { 
      playForPoint = false; 
      result = LOSE; 
     } else if (sum == point) { 
      playForPoint = false; 
      result = WON; 
     } else { 
      printf("Please roll the dice again with [ENTER].\n"); 
      fgetc(stdin); 
     } 
    } 

    return result; 
} 

/*----------------------------------------------------------------------------*/ 
int main (void){ 
    int result = playGame(); 
    switch (result) 
    { 
     case WON: 
      printf("You won the game.\n"); 
      break; 
     case LOSE: 
      printf("You lost the game.\n"); 
      break; 
     default: 
      printf("Something went wrong in the program.\n"); 
      break; 
    } 

    return 0; 
} 

樣本輸出


ROLL THE DICE WITH [ENTER] 

D1: 3 - D2: 5 - Sum: 8 
Playing for point:8. Please hit enter. 

D1: 3 - D2: 1 - Sum: 4 
Please roll the dice again with [ENTER]. 

D1: 3 - D2: 2 - Sum: 5 
Please roll the dice again with [ENTER]. 

D1: 1 - D2: 5 - Sum: 6 
Please roll the dice again with [ENTER]. 

D1: 3 - D2: 2 - Sum: 5 
Please roll the dice again with [ENTER]. 

D1: 2 - D2: 6 - Sum: 8 
You won the game. 
看來你什麼都沒有,現在...
+0

謝謝!!!你救了我的生命 – AnaF

+1

@AnaF沒問題,如果這能夠充分回答你的問題問題,請檢查問題頂部附近的複選標記,並考慮提升它。 – DevNull

+0

@Dogbert - 你爲他們做了功課。 – PhillyNJ

1

你還沒有滿足遊戲的規則。您的代碼需要7和11作爲獲勝者,並將其他任何數字作爲失敗者。

在7/11檢查後,您需要檢查2,3或12並打印「丟失」消息(如果爲true)。如果沒有,你有點數,你需要提示用戶繼續滾動,直到他得到點數(勝利)或7(失敗)。

您還需要跟蹤main的贏/輸。您需要在循環中調用playGame,提示用戶在每次迭代時繼續。

+0

謝謝!我不'我真的明白如何在main – AnaF

+0

@AnaF中調用playGame(),你需要做的是從'playGame'返回'WIN'或'LOSE',並在'main'中捕獲它,更新相應的計數器 – dbush