[英]The game of Craps
我试图要求用户输入y或n,游戏将退出或继续。 我还想显示总的赢和输和用户退出。 也许我没有真正了解布尔值并返回函数中的内容?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
int rollDice(void);
bool playGame(void);
int main(void)
{
srand((unsigned)(time(NULL)));
char userInput;
while (true)
{
playGame();
printf("Would you like to play again?");
scanf("%c", &userInput);
if (userInput == 'n' || userInput == 'N')
{
return false;
}
else
{
return true;
}
}
return 0;
}
int rollDice(void)
{
int dice1 = rand()%6+1;
int dice2 = rand()%6+1;
int totaldice = dice1 + dice2;
return totaldice;
}
bool playGame(void)
{
int point, total;
int winCounter, looseCounter;
printf("The game is starting!\n");
total = rollDice();
printf("You rolled: %d\n", total);
if (total == 7 || total == 11)
{
printf("Wow it's your lucky day! You Win!\n");
winCounter++;
}
else if (total == 2 || total == 3 || total == 12)
{
printf("Unlucky! You Loose!\n");
looseCounter++;
}
else {
point = total;
printf("Your Point is: %d\n", point);
while (true)
{
total = rollDice();
printf("You rolled: %d\n", total);
if (total == point)
{
printf("You made your point! You Win!\n");
winCounter++;
break;
}
else if (total == 7)
{
printf("Thats a %d. You Loose!\n", total);
looseCounter++;
break;
}
}
}return true;
}
您的主要问题是,如果用户输入的内容与'n'
或'N'
不同,则使用return
指令终止该主程序, return
其删除并继续循环。
如果您使用布尔变量退出while循环,那就更好了:
int main(void)
{
srand((unsigned)(time(NULL)));
char userInput;
bool paygame = true;
while (paygame)
{
playGame();
printf("Would you like to play again?");
scanf(" %c", &userInput);
printf ("Test: %c\n", userInput);
if (userInput == 'n' || userInput == 'N')
{
paygame = false;
}
}
return 0;
}
第二个大问题是游戏功能的计数器:必须将它们初始化为0。
int winCounter = 0, looseCounter = 0;
否则,计数从随机数开始。
如果您想计算所有玩过的游戏的全部胜负,则可以简单地使用静态变量:
bool playGame(void)
{
int point, total;
static int winCounter = 0, looseCounter = 0;
printf("The game is starting!\n");
total = rollDice();
printf("You rolled: %d\n", total);
if (total == 7 || total == 11)
{
printf("Wow it's your lucky day! You Win!\n");
winCounter++;
}
else if (total == 2 || total == 3 || total == 12)
{
printf("Unlucky! You Loose!\n");
looseCounter++;
}
else {
point = total;
printf("Your Point is: %d\n", point);
while (true)
{
total = rollDice();
printf("You rolled: %d\n", total);
if (total == point)
{
printf("You made your point! You Win!\n");
winCounter++;
break;
}
else if (total == 7)
{
printf("Thats a %d. You Loose!\n", total);
looseCounter++;
break;
}
}
}
printf ("Won: %d - Lose: %d\n", winCounter, looseCounter);
return true;
}
最后,将scanf
格式说明符更改为" %c"
以允许scanf
在用户每次输入后“刷新”换行符'\\n'
n'char到stdin
。
不要在while循环中使用return。 而是使用变量并将其用于条件。 也不需要将其设置为true,因为直到您按下n或N时,条件一直为true
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
int rollDice(void);
bool playGame(void);
int main(void)
{
srand((unsigned)(time(NULL)));
char userInput;
bool again = true;
while (again==true)
{
printf("Would you like to play again?");
scanf("%c", &userInput);
if (userInput == 'n' || userInput == 'N')
{
again = false;
}
}
return 0;
}
如果要在用户退出后显示总计,则需要将其存储在playGame
函数之外。
目前, playGame
的返回值毫无意义,因此我们可以使用它来指示玩家是否获胜:
bool playGame(void)
{
int point, total;
printf("The game is starting!\n");
total = rollDice();
printf("You rolled: %d\n", total);
if (total == 7 || total == 11)
{
printf("Wow it's your lucky day! You Win!\n");
return true;
}
else if (total == 2 || total == 3 || total == 12)
{
printf("Unlucky! You Lose!\n");
return false;
}
else
{
point = total;
printf("Your Point is: %d\n", point);
while (true)
{
total = rollDice();
printf("You rolled: %d\n", total);
if (total == point)
{
printf("You made your point! You Win!\n");
return true;
}
else if (total == 7)
{
printf("Thats a %d. You Lose!\n", total);
return false;
}
}
}
return false;
}
并稍微重写main
:
int main(void)
{
srand((unsigned)(time(NULL)));
int total = 0;
int wins = 0;
char userInput;
while (true)
{
total += 1;
if (playGame())
{
wins += 1;
}
printf("Would you like to play again?");
scanf("%c", &userInput);
if (userInput == 'n' || userInput == 'N')
{
break;
}
}
printf("Of %d games, you won %d.", total, wins);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.