石头,剪刀,布游戏

设计一个游戏,让用户与计算机玩“石头,剪 刀,布”游戏,要求:

(1)程序能够统计玩的次数以及每一次的结果,说明是谁赢了, 还是平了;

(2)注意:计算机出牌的最简单的方法是随机算法,请尝试考虑 能不能设置一种分析用户出牌规律的算法,让计算机能够赢用户的 几率大于50%

代码如下:

#include<iostream>
#include<ctime>

using namespace std;

//注意:用0表示石头,1表示剪刀,2表示布

class CGame
{
private:
    int iHuman;     //玩家出拳
    int iComputer;  //电脑出拳
    int iPrevious = 0;  //玩家上次出拳
    int iPredicate;  //预测玩家下次可能出拳
    char cMemory[3][4]; //记忆玩家出拳
    int counter = 0;   //记录回合数
public:
    CGame() {}
    void GameCourse();  //猜拳游戏
};

/*  猜拳游戏  */
void CGame::GameCourse()
{
    srand( time(NULL) );   //设置随机数种子
    char reply;
    int isbegin = 1;
    do
    {
        cout<<"        猜拳游戏      "<<endl;
        cout<<"用0表示石头,1表示剪刀,2表示布"<<endl<<endl;
        cout<<"        开始游戏            "<<endl;
        cout<<"请出拳: ";
        while( 1 )
        {
            cin>>iHuman;
            if( iHuman<0 || iHuman>2 )
            {
                 cout<<"输入错误,请重新输入:";
            }
            else
            {
                break;
            }
        }
        counter++;

        /*  让电脑与玩家下10次,记忆玩家上一次和当前次出拳内容的次数,以探索玩家出拳规律,
            分析玩家习惯,之后根据上一次出拳自行判断下一次出拳结果  */
        if( counter<10 )
        {
            iComputer = rand() % 3;     //当counter<10时,电脑随机出拳
        }
        else
        {
            iPredicate = 0;
            if( cMemory[iPrevious][iPredicate]<cMemory[iPrevious][2] )
            {
                iPredicate = 2;
            }
            if( cMemory[iPrevious][1]>cMemory[iPrevious][2] && cMemory[iPrevious][iPredicate]<cMemory[iPrevious][1] )
            {
                iPredicate = 1;
            }
            iComputer = ( iPredicate+2 ) % 3;
        }

        switch(iHuman)
        {
        case 0:
            cout<<"玩家出拳:石头!"<<"\t";
            break;
        case 1:
            cout<<"玩家出拳:剪刀!"<<"\t";
            break;
        case 2:
            cout<<"玩家出拳:布!"<<"\t";
            break;
        }
        if( iComputer == 0 )
        {
            cout<<"电脑出拳:石头!"<<endl;
            switch(iHuman)
            {
            case 0:
                cout<<"平局!"<<endl<<endl;
                break;
            case 1:
                cout<<"电脑赢了!"<<endl<<endl;
                break;
            case 2:
                cout<<"玩家赢了!"<<endl<<endl;
                break;
            }
        }
        else
        {
            if( iComputer == 1 )
            {
                cout<<"电脑出拳:剪刀!"<<endl<<endl;
                switch(iHuman)
                {
                case 1:
                    cout<<"平局!"<<endl<<endl;
                    break;
                case 2:
                    cout<<"电脑赢了!"<<endl<<endl;
                    break;
                case 0:
                    cout<<"玩家赢了!"<<endl<<endl;
                    break;
                }
            }
            else
            {
                cout<<"电脑出拳:布!"<<endl<<endl;
                switch(iHuman)
                {
                case 2:
                    cout<<"平局!"<<endl<<endl;
                    break;
                case 0:
                    cout<<"电脑赢了!"<<endl<<endl;
                    break;
                case 1:
                    cout<<"玩家赢了!"<<endl<<endl;
                    break;
                }
            }
        }
        cMemory[iPrevious][iHuman]++;
        iPrevious = iHuman;
        cout<<endl<<"游戏次数:"<<counter<<"   ";
        cout<<"是否继续游戏( Y / y 或 N / n )  ";
        cin>>reply;
        if( reply=='Y' || reply=='y' )
        {
            isbegin = 1;
        }
        else
        {
            isbegin = 0;
        }
        system("cls");
    }
    while( isbegin );

}

int main()
{
    CGame gm;
    gm.GameCourse();      //进行猜拳游戏
    return 0;
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值