| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 683 人关注过本帖
标题:简单模拟投掷骰子
取消只看楼主 加入收藏
imzaghi333
Rank: 2
来 自:江苏昆山
等 级:论坛游民
帖 子:34
专家分:61
注 册:2013-9-7
结帖率:66.67%
收藏
已结贴  问题点数:20 回复次数:0 
简单模拟投掷骰子
#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;
    }
}
搜索更多相关主题的帖子: represent current include result 
2013-09-07 22:02
快速回复:简单模拟投掷骰子
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019521 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved