C语言编写程序,模拟掷(两个)骰子的游戏。求解?
#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;
}