骰子游戏程序
我正在写一个掷骰子程序,其中有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;
}
while (ta = 'Y')
应该while (ta == 'Y')
所以如果你的if语句是==不是=
你只是打败了我... – Floris 2013-02-13 03:21:14
也是它下面的if语句。 – Nashibukasan 2013-02-13 03:21:27
注意:“启用警告”可能会将此检测为可疑。 – 2013-02-13 03:30:19
建议:摆脱代码的99%,看看你可以创建一个循环,它你想要什么。然后将其余的代码添加回来,直到它中断。 – Floris 2013-02-13 03:20:09
建议#2:*启用并阅读警告*(对于GCC,我推荐的最低限度是[-Wall](http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html)) – 2013-02-13 03:31:28
此外,为了进一步阅读,查找[“yoda条件”](http://*.com/search?q=yoda+conditional) – 2013-02-13 03:35:04