2013-02-13 5 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

Таким образом, вопросник не имеет отношения к вопросу? –

+2

Вы используете 'yesorno' в своем' if', чтобы проверить, хочет ли пользователь снова катиться, но 'yesorno' никогда не устанавливается, как вы делаете' ta = getchar(); ' – koopajah

+0

Также вы назначаете« yesorno »вместо сравнения с 'Y'. –

ответ

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, вы должны спросить себя: «Где я устанавливать это значение Как я установить значение»

Смежные вопросы