设计一个游戏,让用户与计算机玩“石头,剪 刀,布”游戏,要求:
(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; }



