用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c代码库

一个简单的赌博游戏

2012-11-02 作者: 程序猿style举报

[c]代码库

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define WON 0
#define LOST 1
#define CONTINUE 2
 
 
/*一个简单的赌博游戏,游戏规则如下:玩家掷两个骰子,点数为1到6,如果第一次点数和为7或11,则玩家胜,如果点数和为2、3或12,则玩家输,如果和 为其它点数,则记录第一次的点数和,然后继续掷骰,直至点数和等于第一次掷出的点数和,则玩家胜,如果在这之前掷出了点数和为7,则玩家输。*/
 
int rollDice()
{
    return ( rand() % 6 ) + ( rand() % 6 ) + 2;
}
 
int main ( void )
{
    int firstRoll = 1;
    int gameStatus = CONTINUE;
    int die1, die2, sumOfDice;
    int firstPoint = 0;
    char c;
 
    srand ( time ( 0 ) );
 
    printf ( "Craps赌博游戏,按Enter键开始游戏****" );
 
    while ( 1 )
    {
        getchar();
 
        if ( firstRoll )
        {
            sumOfDice = rollDice();
            printf ( "\n玩家掷出点数和:%d\n", sumOfDice );
 
            switch ( sumOfDice )
            {
            case 7:
            case 11:
                gameStatus = WON;
                break;
            case 2:
            case 3:
            case 12:
                gameStatus = LOST;
                break;
            default:
                firstRoll = 0;
                gameStatus = CONTINUE;
                firstPoint = sumOfDice;
                break;
            }
        }
        else
        {
            sumOfDice = rollDice();
            printf ( "\n玩家掷出点数和:%d\n", sumOfDice );
 
            if ( sumOfDice == firstPoint )
                gameStatus = WON;
            else if ( sumOfDice == 7 )
                gameStatus = LOST;
        }
 
        if ( gameStatus == CONTINUE )
            puts ( "未分胜负,再掷一次****\n" );
        else
        {
            if ( gameStatus == WON )
                puts ( "玩家胜" );
            else
                puts ( "玩家输" );
 
            printf ( "再玩一次?" );
            scanf ( "%c", &c );
            if ( c == 'n' )
            {
                puts ( "游戏结束" );
                break;
            }
            firstRoll = 1;
        }
    }
    return 0;
}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...