用C语言编写一个模拟掷骰子游戏谢谢了,大神帮忙啊
2015-10-19
念,亦是留恋
念,亦是留恋
念,亦是留恋
念,亦是留恋
念,亦是留恋
念,亦是留恋
念,亦是留恋
绝招树立正反面典型
弄清思绪万千望穿
#include<stdio.h>
#include<stdbool.h>
#include<time.h>
#include<stdlib.h>
int roll_dice(void);
bool play_game(void);
int main(void){
srand((unsigned) time(NULL));
int win=0,lose=0; //记录胜败的次数
char ch1,ch2; //用来判定前进还是退出
//反复调用play_gram函数
for(;;){
for(;;){
if(play_game()==true){
win++;
printf("You win!\n");
break;
}
else {
printf("You lose!\n");
lose++;
break;
}
}
//用来存储Y(y)或其他字母
printf("\nPlay again?");
ch1=getchar();
ch2=getchar();
printf("\n");
//判定是否继续
if(ch1=='y'||ch1=='Y'){
continue;
}else{
break;
}
}
printf("\nWins=%d Losses=%d",win,lose);
return 0;
}
//两个随机数的总和
int roll_dice(void){ // 随机时间不应放在函数内
int num1,num2;
num1=1+rand() % 6;
num2=1+rand() % 6;
return num1+num2;
}
bool play_game(void){
int x,y=0;
for(int i=1;;i++){
x=roll_dice(); //随机数总和
if(1==i){ //第一次的判定
if(x==7||x==11){
printf("You rolled:%d\n",x);
printf("Your point is %d\n",x);
return true;
break;
}
else if(x==2||x==3||x==12){
printf("You rolled:%d\n",x);
printf("Your point is %d\n",x);
return false;
break;
}
else {
printf("You rolled:%d\n",x);
printf("Your point is %d\n",x);
continue;
}
}
//两次以上的判定
if(x==4||x==5||x==6||x==8||x==9||x==10){
y++; //判定两次是否是目标
printf("You rolled:%d\n",x);
if(y==2){ //只有两次都出现才能返回真
return true;
break;
}
else{
continue;
}
}
else if(x==7){
printf("You rolled:%d\n",x);
return false;
break;
}
else{
printf("You rolled:%d\n",x);
continue;
}
}
}