2013-02-13 186 views
0

我正在寫一個擲骰子程序,其中有7輪在每一輪用戶得到滾動多次,他們的願望,總和加起來,然後計算機滾動相同數量的骰子和它的總和加起來。本輪比賽的勝者由最高的骰子決定,而最多輪的人贏得比賽。這是我迄今爲止,但我不能得到循環詢問用戶,如果他們想再次滾動正常工作,所以任何幫助將不勝感激。骰子游戲程序

#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 

int humanTurn() 
{ 
int toss; 
toss = throwDie(); 
printf("Human throws a %d\n", toss); 
return toss; 

} 

// Computer turn 

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; 
const int numberOfRounds = 7; 
char ta=0; 
/* Play 7 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]"); 
scanf("%s", ta); 

while (ta = 'Y') 
{ 

    if(ta = 'Y') 
    { 
    humanToss = humanTurn(); 
    printf("Do you wish to throw again? [Y or N]"); 
    scanf("%s", ta); 
    } 
    else 
    { 
     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; 
    } 
+2

建議:擺脫代碼的99%,看看你可以創建一個循環,它你想要什麼。然後將其餘的代碼添加回來,直到它中斷。 – Floris 2013-02-13 03:20:09

+1

建議#2:*啓用並閱讀警告*(對於GCC,我推薦的最低限度是[-Wall](http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html)) – 2013-02-13 03:31:28

+0

此外,爲了進一步閱讀,查找[「yoda條件」](http://stackoverflow.com/search?q=yoda+conditional) – 2013-02-13 03:35:04

回答

4
while (ta = 'Y') 

應該while (ta == 'Y')

所以如果你的if語句是==不是=

+0

你只是打敗了我... – Floris 2013-02-13 03:21:14

+1

也是它下面的if語句。 – Nashibukasan 2013-02-13 03:21:27

+1

注意:「啓用警告」可能會將此檢測爲可疑。 – 2013-02-13 03:30:19