C程序骰子游戏
我在骰子游戏中遇到麻烦。我有一个任务:C程序骰子游戏
游戏的规则如下: 1.玩家掷骰子并加起来的面值。 2.如果第一个掷骰是7或11,则玩家获胜。 3.如果第一个掷骰是2,3或12,玩家就会放松。 4.如果第一个掷骰是任何其他数字,那么该总和就成为玩家的点数。 5.要获胜,玩家必须继续掷骰子直到他/她“得分。” 6.玩家在点前掷骰子7。
1)在程序中定义WON和LOST宏。使用WON的值为0,LOSE的值为1 2)使用函数原型int rollDice(void);实现函数。
rollDice()应使用RAND()随机生成1点之间的数 - 6
返回()
3由兰特产生的数目)实施的功能,与函数原型int琐事(空隙);
当播放机准备好打,他(她)会用Enter键掷骰子
如果用户胜在他/她的第一个卷,祝贺播放机和带WON
返回如果用户在第一次掷骰子时放松,祝贺玩家,并用LOSE返回
让用户继续游戏直到他赢/输,直到他/她输掉一个合适的按摩并以最后一次掷骰值结束游戏。
4)你的main()应该 调用函数琐事()
询问用户是否他(她)想要继续玩另一个游戏,保持损失的数字轨迹,赢得
当用户决定完成比赛时,显示他所赢得的比赛数量。
给用户根据胜或负他(她)有
返回与EXIT_SUCCESS
这里的值的数量相应的消息是我现在,但它告诉我,有是错误的。任何人都可以请帮我完成这个任务吗?
#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include <stdlib.h>
#define WON 0
#define LOSE 1
int rollDice(void);
int playGame(void);
int rollDice(void) {
return ((rand() % 6) + 1);
}
int playGame(void){
int dice_1 = 0;
int dice_2 = 0;
int sum = 0;
time_t t;
srand(time(&t));
printf("ROLL THE DICE WITH [ENTER]\n");
dice_1 = rollDice();
dice_2 = rollDice();
sum = dice_1 + dice_2;
if (sum == 7 || sum == 11){
printf("Congratulations you roll %d and WON at your first try!", sum);
}
else {
printf("Your roll was %d ,you lose try agian.\n", sum);
}
return 0;
}
int main (void){
playGame();
}
错误的是(在GCC的Linux):
x.c:9:1:错误:杂散“\ 302”在程序
INT rollDice(无效);
^
x.c:9:1:错误:杂散“\ 240”在程序
X。c:10:1:错误:在程序中丢失'\ 302'
int playGame(void);
^
XC:10:1:错误:杂散 '\ 240' 在程序
XC:12:1:错误:杂散 '\ 302' 在程序
INT rollDice(空隙){
^
XC:12:1:错误:杂散 '\ 240' 在程序
XC:16:1:错误:16:1:错误:杂散 '\ 240' 在程序在程序
INT琐事(无效){
^
XC杂散 '\ 302'
这里有一些错误。
- 您没有在阅读/使用从
playGame()
返回的值。您应该存储结果并对其执行操作。 - 你的逻辑不完整,因为“玩点”的标准和损失都是相同的。
- 您没有任何强制程序等待用户按下的地方ENTER。
我在下面为您列出了完整的代码清单。
代码
/*******************************************************************************
* Preprocessor directives
******************************************************************************/
#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include <stdlib.h>
#include <stdbool.h>
#define WON 0
#define LOSE 1
/*******************************************************************************
* Function prototypes
******************************************************************************/
int rollDice(void);
int playGame(void);
/*******************************************************************************
* Function definitions
******************************************************************************/
/*----------------------------------------------------------------------------*/
int rollDice(void) {
return ((rand() % 6) + 1);
}
/*----------------------------------------------------------------------------*/
int playGame(void){
int dice_1 = 0;
int dice_2 = 0;
int sum = 0;
int result;
int point = 0;
time_t t;
bool playForPoint = false;
srand(time(&t)); // Initialize random seed
printf("ROLL THE DICE WITH [ENTER]\n");
fgetc(stdin);
dice_1 = rollDice();
dice_2 = rollDice();
sum = dice_1 + dice_2;
printf("D1:%2d - D2:%2d - Sum:%2d\n", dice_1, dice_2, sum);
switch (sum)
{
case 7:
case 11:
result = WON;
break;
case 2:
case 3:
case 12:
result = LOSE;
break;
default:
playForPoint = true;
point = sum;
printf("Playing for point:%d. Please hit enter.\n", point);
fgetc(stdin);
break;
}
while (playForPoint)
{
dice_1 = rollDice();
dice_2 = rollDice();
sum = dice_1 + dice_2;
printf("D1:%2d - D2:%2d - Sum:%2d\n", dice_1, dice_2, sum);
if (sum == 7) {
playForPoint = false;
result = LOSE;
} else if (sum == point) {
playForPoint = false;
result = WON;
} else {
printf("Please roll the dice again with [ENTER].\n");
fgetc(stdin);
}
}
return result;
}
/*----------------------------------------------------------------------------*/
int main (void){
int result = playGame();
switch (result)
{
case WON:
printf("You won the game.\n");
break;
case LOSE:
printf("You lost the game.\n");
break;
default:
printf("Something went wrong in the program.\n");
break;
}
return 0;
}
样本输出
ROLL THE DICE WITH [ENTER]
D1: 3 - D2: 5 - Sum: 8
Playing for point:8. Please hit enter.
D1: 3 - D2: 1 - Sum: 4
Please roll the dice again with [ENTER].
D1: 3 - D2: 2 - Sum: 5
Please roll the dice again with [ENTER].
D1: 1 - D2: 5 - Sum: 6
Please roll the dice again with [ENTER].
D1: 3 - D2: 2 - Sum: 5
Please roll the dice again with [ENTER].
D1: 2 - D2: 6 - Sum: 8
You won the game.
看来你什么都没有,现在...
作为题外话,因为没有“做我的家工作“网站。 –
我投票关闭这个问题 – Olaf
什么不行? – xxbbcc