简单模拟投掷骰子
#include <stdio.h>#include <stdlib.h>
#include <time.h>
enum Status { CONTINUE, WON, LOST }; // enumeration constants represent game status
int rolldice(void);
enum Status craps(void);
void lostMsg(void);
void winMsg(void);
int main()
{
int bank_balance = 1000;
int wager = 0;
enum Status gameStatus; //result of current game
int response;
do{
srand(time(NULL));
printf("You have $%d in your hand.\n"
"Wish you good luck! ", bank_balance);
printf("Please place your wager: ");
scanf("%d", &wager);
while(wager <= 0 || wager > bank_balance){
printf("Please bet a valid wager!~~ ");
scanf("%d", &wager);
}
gameStatus = craps();
if(gameStatus == LOST){
bank_balance -= wager;
printf("Your new bank balance in your hand is $%d\n", bank_balance);
if(bank_balance == 0){
printf("Sorry,you burseted!\n");
}
}
else{
bank_balance += wager;
printf("Your new bank balance in your hand is $%d\n.", bank_balance);
}
printf("Would you like to contine or cash in your chips? \n");
printf("Pleae type 1=yes 2=no \n");
scanf("%d", &response);
}while(response == 1);
return 0;
}
int rolldice(void)
{
int x1,x2;
int sum = 0;
x1 = 1+rand()%6;
x2 = 1+rand()%6;
sum = x1+x2;
printf("Player rolled %d.", sum);
return sum;
}
enum Status craps(void)
{
int result; // current roll of dice
int myPoint; //point value
enum Status gameX; //can contain CONTINUE, WON or LOST
result = rolldice();
switch(result){
case 7:
case 11:
gameX = WON;
winMsg();
break;
case 2:
case 3:
case 12:
gameX = LOST;
lostMsg();
break;
default:
gameX = CONTINUE;
myPoint = result;
printf("Point is %d.\n", myPoint);
break;
}
while(gameX == CONTINUE){
printf("You get the change to play again.\n");
result = rolldice();
if(result == myPoint){
gameX = WON;
//winMsg();
}
else{
if(result == 7){
gameX = LOST;
//lostMsg();
}
}
}//End of while
if(gameX == WON){
winMsg();
return WON;
}
else{
lostMsg();
return LOST;
}
}
void winMsg()
{
switch(rand()%3){
case 0:
printf("\nYou're up big. Now's the time to cash in your chips!\n");
break;
case 1:
printf("\nWay too lucky! Those dice have to be loaded!\n");
case 2:
printf("\nHow lucky,you win the game.\n");
break;
default:
break;
}
}
void lostMsg()
{
switch(rand()%3){
case 0:
printf("\nOh, you're going for broke, huh?\n");
break;
case 1:
printf("\nAw cmon, take a chance!\n" );
case 2:
printf("\nHey, I think this guy is going to break the bank!!\n");
default:
break;
}
}