一个简单的双骰子游戏

仅为一次课堂作业。



#include <stdio.h>

#include <stdlib.h>

#include <time.h>/* contains prototype for function time */


/* enumeration constants represent game status */

enum Status { CONTINUE, WON, LOST };


int rollDice( void ); /* function prototype */


/* function main begins program execution */

int main( void )

{

    int sum;/* sum of rolled dice */

    int myPoint;/* point earned */

    int wager,bankbalance=1000;

    enumStatus gameStatus;/* can contain CONTINUE, WON, or LOST */

    do{

        printf("Enter your wager,please:");

        scanf("%d",&wager);

    }while(wager<0||wager>1000);

    /* randomize random number generator using current time */

    srand(time(NULL) );

    

    sum = rollDice();/* first roll of the dice */

    

    /* determine game status based on sum of dice */

    switch( sum ) {

            /* win on first roll */

        case 7:

        case 11:

            gameStatus = WON;

            bankbalance+=wager;

            printf("Aw come on, take a chance!\nYou’are up big. Now’s the time to cash in your chips!\n");

            break;

            

            /* lose on first roll */

        case 2:

        case 3:

        case 12:

            gameStatus = LOST;

            bankbalance-=wager;

            printf("Oh,you’re going to broke,huh?\n");

            break;

            

            /* remember point */

        default:

            gameStatus = CONTINUE;

            myPoint = sum;

            printf( "Point is %d\n", myPoint );

            break; /* optional */

    } /* end switch */

    

    /* while game not complete */

    while ( gameStatus == CONTINUE ) {

        sum = rollDice();/* roll dice again */

        

        /* determine game status */

        if ( sum == myPoint ) { /* win by making point */

            gameStatus = WON;

            bankbalance+=wager;

            printf("Aw come on, take a chance!\nYou’are up big. Now’s the time to cash in your chips!\n");/* game over, player won */

        } /* end if */

        else {

            if ( sum == 7 ) { /* lose by rolling 7 */

                gameStatus = LOST;

                bankbalance-=wager;

                printf("Oh,you’re going to broke,huh?\n");/* game over, player lost */

            } /* end if */

        } /* end else */

    } /* end while */

    

    /* display won or lost message */

    if ( gameStatus == WON ) { /* did player win? */

        printf("Player wins\n" );

    } /* end if */

    else {/* player lost */

        printf("Player loses\n" );

    } /* end else */

    printf("You have $%d left\n",bankbalance);

    if(bankbalance==0)

        printf("Sorry.You busted\n");

    return0; /* indicates successful termination */

} /* end main */


/* roll dice, calculate sum and display results */

int rollDice( void )

{

    int die1;/* first die */

    int die2;/* second die */

    int workSum;/* sum of dice */

    

    die1 = 1 + (rand() % 6 ); /* pick random die1 value */

    die2 = 1 + (rand() % 6 ); /* pick random die2 value */

    workSum = die1 + die2; /* sum die1 and die2 */

    

    /* display results of this roll */

    printf("Player rolled %d + %d = %d\n", die1, die2, workSum );

    return workSum;/* return sum of dice */

} /* end function rollRice */


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值