栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

石头,剪刀,布游戏

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

石头,剪刀,布游戏

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

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

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

代码如下:

#include
#include

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<<"        猜拳游戏      "<>iHuman;
            if( iHuman<0 || iHuman>2 )
            {
                 cout<<"输入错误,请重新输入:";
            }
            else
            {
                break;
            }
        }
        counter++;

        
        if( counter<10 )
        {
            iComputer = rand() % 3;     //当counter<10时,电脑随机出拳
        }
        else
        {
            iPredicate = 0;
            if( cMemory[iPrevious][iPredicate]cMemory[iPrevious][2] && cMemory[iPrevious][iPredicate]>reply;
        if( reply=='Y' || reply=='y' )
        {
            isbegin = 1;
        }
        else
        {
            isbegin = 0;
        }
        system("cls");
    }
    while( isbegin );

}

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

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1024159.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号