2013-02-13 3 views
-3

Donc je fais un jeu de dés où vous obtenez une quantité x de dé et ensuite l'ordinateur roule le même montant et le joueur avec le total le plus élevé gagne la partie. Cependant je suis coincé sur la boucle qui demande au joueur s'il veut encore rouler. Peu importe ce que je saisis, il roule à nouveau. J'ai été bloqué sur cela pendant un moment alors toute aide serait grandement appréciée.Le programme de dés ne fonctionne pas

#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

Donc le découpage en dés n'est pas pertinent à la question? –

+2

Vous utilisez 'yesorno' dans votre' if' pour vérifier si l'utilisateur veut relancer mais 'yesorno' n'est jamais défini comme vous le faites' ta = getchar(); ' – koopajah

+0

Vous attribuez également" yesorno "au lieu de" comparint " 'Y'. –

Répondre

4

changer votre programme

if (yesorno == 'Y') 

Vous assignez au lieu de vérifier pour oui.

1

Vous êtes coincé ici:

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++; 
    } 
} 

Vous avez votre valeur yesorno de l'entrée précédente; alors vous recevez une nouvelle entrée, mais la variable yesorno est la même - vous venez de définir la variable ta Donc yesorno est toujours 'Y', i est toujours 0 et vous êtes en boucle while infinie.

éditez et vous attribuez yesorno dans votre si, comme l'a dit le deuxième commentateur. mais de toute façon, si vous écrivez == au lieu de =, vous serez toujours en boucle infinie.

0

Il y a une différence de TRÈS entre ces deux lignes.
Pouvez-vous le repérer?

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

Aussi, parce que vous vérifiez la valeur de yesorno, vous devez vous demander: « Où suis-je réglage de cette valeur Comment suis-je définir la valeur »

Questions connexes