2013-02-13 136 views
-3

因此,我正在製作一個骰子游戲,擲出一定數量的骰子,然後計算機擲出相同數量,總贏得最高的玩家贏得一輪。但是我陷入循環中,詢問玩家是否想再次滾動。無論我輸入什麼,它都會再次滾動。我一直堅持這一段時間,所以任何幫助將不勝感激。骰子程序不能正常工作

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
    /* Easy dice game 
    | 
    | The game consists of 7 rounds. 
    | In each round, the computer throws a die, 
    | then the human throws a die. 
    | The winner of the round is the player who has the highest throw. 
    | In case of a tie, neither player wins. 
    | The winner of the game is the player who has won the most rounds. 
    | 
    */ 

    char input[132]; /* user input buffer */ 

int throwDie() 
{ 
    static int initialized = 0; 
    int num; 

    if (!initialized) 
    { 
     printf("Initializing Die!\n\n"); 
     srand(time(NULL)); 
     initialized = 1; 
    } 
    num = rand()%6 + 1 ; 
    return num; 
} 

/* Human turn 
| 
| This might be mode made interesting in the future. 
| 
*/ 
int humanTurn() 
{ 
    int toss; 
    toss = throwDie(); 
    printf("Human throws a %d\n", toss); 
    return toss; 

} 

/* Computer turn 
| 
| This might be made more interesting in the future. 
| 
*/ 
int computerTurn() 
{ 
    int toss; 
    toss = throwDie(); 
    printf("Computer throws a %d\n", toss); 
    return toss; 
} 

int main(int argc, char *argv[]) 
{ 
    int round, humanWins=0, computerWins=0 ; 
    int humanToss, computerToss; 
    int i = 0, yesorno; 
    const int numberOfRounds = 7; 
    char ta=0; 
    /* Play 13 Rounds */ 
    for (round = 1; round<=numberOfRounds; round++) 
    { 
     printf("\nRound %d\n\n", round); 
     printf("Player's Turn: (hit enter)"); 
     gets(input); /* pause for dramatic effect */ 
     humanToss = humanTurn(); 
     printf("Do you wish to throw again? [Y or N]"); 
     ta = getchar(); 


     while (i == 0) 
     { 
      if (yesorno = 'Y') 
      { 
       gets(input); 
       humanToss = humanTurn(); 
       printf("Do you wish to throw again? [Y or N]"); 
       ta = getchar(); 

      } 
      if(yesorno == 'N') 
      { 
       i++; 
      } 
     } 




     printf("Computer's Turn: (hit enter)"); 

     gets(input); /* pause for dramatic effect */ 
     computerToss = computerTurn(); 

     /* Determine Winner of the Round */ 
     if (humanToss > computerToss) 
     { 
      humanWins++; 
      printf("\tHuman wins the round. human: %3d. computer: %3d\n", 
       humanWins, computerWins); 
     } 
     else if (computerToss > humanToss) 
     { 
      computerWins++; 
      printf("\tComputer wins the round. human:%3d. computer: %3d\n", 
       humanWins, computerWins); 
     } 
     else if (computerToss == humanToss) 
     { 
      printf("\tTie.      human:%3d. computer: %3d\n", 
       humanWins, computerWins); 
     } 
    } 

    /* Determine Winner to the Game */ 
    if (humanWins > computerWins) 
     printf("\n\nWINNER!! The human wins the game!\n"); 
    else if (computerWins < humanWins) 
     printf("\n\nThe computer wins the game!\n"); 
    else 
     printf("\n\nTie Game!\n"); 

    printf("\n"); 
    system("pause"); 
    return 0; 
} 
+0

所以切塊與問題無關? – 2013-02-13 18:28:10

+2

在'if'中使用'yesorno'來檢查用戶是否想再次滾動,但是'yesorno'從未設置爲'ta = getchar();' – koopajah 2013-02-13 18:28:19

+0

您也正在分配「yesorno」而不是comparint 'Y'。 – 2013-02-13 18:31:45

回答

4

改變你的程序作爲

if (yesorno == 'Y') 

您正在分配的,而不是檢查是肯定的。

1

你困在這裏:

while (i == 0) 
{ 
    if (yesorno = 'Y') 
    { 
    gets(input); 
    humanToss = humanTurn(); 
    printf("Do you wish to throw again? [Y or N]"); 
    ta = getchar(); 

    } 
    if(yesorno == 'N') 
    { 
    i++; 
    } 
} 

你必須從以前的輸入您的yesorno值;那麼你會收到新的輸入,但變量yesorno是一樣的 - 你只需設置變量ta 因此yesorno總是'Y',i總是0,你處於無限的while循環中。正如第二位評論者所說,你在分配yesorno。但無論如何,如果您會寫==而不是=,那麼您仍然處於無限循環。

0

有一個非常有這兩行之間的巨大差異。
你能發現它嗎?

if (yesorno = 'Y') 
if (yesorno == 'N') 

而且,因爲要檢查的yesorno的價值,你應該問自己:「?我在哪裏設置這個值,我怎麼設置值」