C语言编译抛骰子,简单模拟投掷骰子

已结贴√

问题点数:20 回复次数:6

ca56232b3bbedf9a539d07f37fffb99a.gif

3144d8b7615c79d9f638db40d5689d26.gif

a218af6549b45ee526caf607ebff1358.gif

0f8df0e29816ae721419de940fb833d1.gif

简单模拟投掷骰子

#include

#include

#include

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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值