掷骰子游戏
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
instruction.Remove 终止主程序,然后循环可以继续.
如果使用布尔变量退出 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;
}
第二大问题是playgame函数的计数器:它们必须初始化为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
到 "flush" 换行 '\n'
char left into stdin
用户每次输入。
不要在 while 循环中使用 return。相反,使用变量并将其用于条件。也不需要让它为真,因为在你按下 n 或 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;
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
的 return 值没有意义,所以让我们用它来表示玩家是否获胜:
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;
}
我正在尝试让用户输入 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
instruction.Remove 终止主程序,然后循环可以继续.
如果使用布尔变量退出 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;
}
第二大问题是playgame函数的计数器:它们必须初始化为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
到 "flush" 换行 '\n'
char left into stdin
用户每次输入。
不要在 while 循环中使用 return。相反,使用变量并将其用于条件。也不需要让它为真,因为在你按下 n 或 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;
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
的 return 值没有意义,所以让我们用它来表示玩家是否获胜:
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;
}