#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;// 获取1到6之间的随机数
y = rand() % 6 + 1;
z = x + y;//两点数之和
printf("你的点数之后为 :%d\n", z);
return z;//返回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("你的点数之后为 :%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 = 100, j = 0;
srand((unsigned) time(0)); // 放在循环外面,更新种子,使得每次产生不同的随机数
do {
printf("请下注:\n");
scanf("%d",&j);
b = play_game();
if(b) {
printf("你赢了!\n\n");
i=i+j;
}
else {
printf("你输了!\n\n");
i=i-j;
}
printf("是否输入Y或者y继续游戏? ");
ch = getchar();
getchar(); // 除去回车符
printf("\n");
}
while(ch=='y') ; printf("您的钱数:%d",i);
return 0;
}