2013-02-13 6 views
-3

그래서 저는 x 개의 주사위를 굴린 다음 컴퓨터가 같은 양의 주사위를 굴려 가장 높은 점수를 얻은 플레이어가 주사위를 굴립니다. 그러나 나는 다시 굴러 가고 싶은지 묻는 루프에 붙어있다. 내가 무엇을 입력 했든간에 그것은 다시 굴러 간다. 나는 이것에 잠시 붙어 있었기 때문에 어떤 도움이라도 대단히 감사 할 것입니다.주사위 프로그램이 작동하지 않습니다

#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

'yes'를'if'에서 사용하면 다시 롤백 할 것인지를 체크 할 수 있지만'yesorno'는'ta = getchar();처럼 설정하지 않습니다. – koopajah

+0

또한 comparint 대신 "yesorno"를 지정합니다 '와이'. –

답변

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은 if에 할당됩니다. 두 번째 주석자가 말한 것처럼. 하지만 어쨌든 = 대신 ==을 쓰면 여전히 무한 루프 상태가됩니다.

0

VERY 큰 차이가 있습니다.
찾을 수 있습니까? 또한

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

, 당신은 yesorno의 값을 확인하고 있기 때문에, 당신 자신에게 물어 봐야 : "? 나는이 값을 설정하고 어디에서 어떻게 값을 설정하고"

관련 문제