#include
#include
#include
#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;
}
#include
#include
#include
#include
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;
}