C语言编写程序,模拟掷(两个)骰子的游戏。求解?

第一次掷的时候,如果两个骰子的点数之和为7或11则获胜;如果点数之和为2、3或12则落败;其他情况下的点数之和称为“目标”,游戏继续。在后续的投掷中,如果再次掷出“目标”... 第一次掷的时候,
如果两个骰子的点数之和为7或11则获胜;
如果点数之和为2、3或12则落败;
其他情况下的点数之和称为“目标”,游戏继续。
在后续的投掷中,
如果再次掷出“目标”点数则获胜;
如果点数之和为7则落败;
其他情况被忽略,游戏继续。

每局游戏结束时,程序询问用户是否再玩一次,如果用户输入的回答不是y或Y,程序将显示胜败次数然后终止。

You rolled: 8
if 8
You rolled: 3
You rolled: 10
You rolled: 8
You win!

Play again? y

You rolled: 6
Your point is 6
You rolled: 5
You rolled: 12
You rolled: 3
You rolled: 7
You lose!

Play again? y

You rolled: 11
You win!

Play again? n

Wins: 2 Losses: 1

程序包括main函数和以下两个函数:

int roll_dice(void);
int play_game(void);

roll_dice生成两个[1, 6]的随机数,并且返回它们的和。
play_game进行一次掷骰子游戏,每次调用roll_dice确定点数,玩家获胜则返回TRUE,否则返回FALSE。
还要显示玩家每次掷骰子的结果。
main函数反复调用play_game函数,记录获胜和落败的次数,并显示"yoy win"和"you lose"消息。
展开
 我来答
樱花漫友ed
2016-03-29
知道答主
回答量:14
采纳率:0%
帮助的人:3.1万
展开全部
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
#define bool int
#define FALSE 0
#define TRUE 1
int roll_dice();
void if_again(int,int);
void new_deal(int,int,int);
bool play_game();
int main()
{
play_game();
return 0;
}
int roll_dice()
{
int a=0,b=0;
srand((unsigned)time(NULL));
a=rand()%6+1;
// printf("\n a is :%d",a);
b=rand()%6+1;
//printf("\n b is :%d\n",b);
return a+b;
}
void if_again(int win,int lose)
{
printf("\nPlay again ?");
char ch='a';
scanf("%c",&ch);
if(ch=='y')
{
system("cls");
play_game();
}
if(ch=='n')
{
printf("\nWins: %d\tLosess: %d",win,lose);
return;
}
}
void new_deal(int result,int win,int lose)
{
int once_again;
once_again= roll_dice();
printf("You rolled: %d\n",once_again);
if( once_again==result )
{
printf("You win!\n");
win++;
if_again(win,lose);
}
else
{
once_again=roll_dice();
printf("You rolled: %d\n ",once_again);
if(once_again==7)
{
printf("You lose!\n");
lose++;
if_again(win,lose);
}else
new_deal(result,win,lose);
}
}
bool play_game()
{
int result,win=0,lose=0;
result=roll_dice();
printf("You rolled: %d\n",result);
if(result==7||result==11)
{
printf("You win!");
win++;
if_again(win,lose);
printf("\n");
}
else if(result==2||result==3||result==12)
{
printf("You lose!");
lose++;
if_again(win,lose);
printf("\n");
}
else
{
new_deal(result,win,lose);
}
return 0;
}
来自龙山寺得体的皮诺曹
2020-06-22
知道答主
回答量:2
采纳率:0%
帮助的人:1140
展开全部
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>

bool play_game(void); // 模拟游戏,进行一次游戏,返回一个bool值,决出胜负
int roll_dice(void); // 记录两个骰子之和
int roll_dice(void) {
int x, y, z;
x = rand() % 6 + 1;
y = rand() % 6 + 1;
z = x + y;
printf("you rolled: %d\n", z);
return z;
}
bool play_game(void) {
int p, t;
p = roll_dice();
if(p == 7 || p == 11) {
return true;
}
else if(p == 2 || p == 3 || p == 12) {
return false;
}
else {
t = p;
printf("your point is %d\n", t);
for(; ; ) {
p = roll_dice();
if(p == t) {
return true;
}
else if(p == 7) {
return false;
}
}
}
}

int main(int argc, const char * argv[]) {
// insert code here...
printf("骰子游戏\n");
bool b;
char ch = 'y';
int i = 0, j = 0;

srand((unsigned) time(0)); // 放在循环外面,更新种子,使得每次产生不同的随机数

while(ch == 'Y' || ch == 'y') {
b = play_game();
if(b) {
printf("You win!\n\n");
i++;
}
else {
printf("You lose!\n\n");
j++;
}
printf("Play again? ");
ch = getchar();
getchar(); // 除去回车符
printf("\n");
}

printf("Wins: %d Losess: %d\n",i, j);

return 0;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式