| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 683 人关注过本帖
标题:简单模拟投掷骰子
只看楼主 加入收藏
imzaghi333
Rank: 2
来 自:江苏昆山
等 级:论坛游民
帖 子:34
专家分:61
注 册:2013-9-7
结帖率:66.67%
收藏
已结贴  问题点数:20 回复次数:6 
简单模拟投掷骰子
#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
power_c
Rank: 2
来 自:火星
等 级:论坛游民
帖 子:19
专家分:33
注 册:2013-9-7
收藏
得分:4 
赢得概率大些啊...


           ----------------脑袋空不要紧,关键是不要进水
2013-09-07 22:35
guhemeng
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:100
专家分:165
注 册:2013-7-27
收藏
得分:4 
楼主搞明白,这个地方是解决问题的,不是发表代码的地方!  可以去代码去发个文章,说不定会有朋友会一起仔细研究!!!!!!
2013-09-07 22:39
jg658237
Rank: 7Rank: 7Rank: 7
来 自:青藏高原
等 级:黑侠
帖 子:224
专家分:529
注 册:2013-8-8
收藏
得分:4 
你发代码想干嘛啊

武功再高也怕菜刀.
2013-09-07 22:51
人_神
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:44
专家分:103
注 册:2013-8-1
收藏
得分:4 
观摩学习
2013-09-08 13:41
youngdavid
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:107
专家分:698
注 册:2012-9-24
收藏
得分:4 
写的不错
2013-09-09 07:43
peach5460
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
来 自:武汉
等 级:贵宾
威 望:30
帖 子:2780
专家分:6060
注 册:2008-1-28
收藏
得分:4 
好长,没兴趣看

我总觉得授人以鱼不如授人以渔...
可是总有些SB叫嚣着:要么给代码给答案,要么滚蛋...
虽然我知道不要跟SB一般见识,但是我真的没修炼到宠辱不惊...
2013-09-09 10:12
快速回复:简单模拟投掷骰子
数据加载中...
 
   



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

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