智能版石头剪刀布——c++实现
来源:互联网 发布:酒店市场数据分析 编辑:程序博客网 时间:2024/06/14 18:14
利用c++实现了相对智能一点的石头剪刀布,方法是为计算机设置偏好,有第一偏好、第二偏好,第三偏好。同时计算机选择的时候会以百分之50的概率选择第一偏好,以百分之40的概率选择第二偏好,百分之10的概率选择第三偏好。
为了更加狡猾,设置全局变量记录运行局数,每隔三局(可以自行设定)分析用户的偏好,从而改变自身的偏好,可以使得计算机更为人性化,完成简单的人机交互。
这也是c++老师曾教给我们的,在Python的学习中遇到类似问题,拿出来温习一下,后续会编写出Python版的。
#include<iostream>#include<string>#include<ctime>#include<cstdlib>#define interval 3//间隔为3局 //#define 1 rock //#define 2 paper//#define 3 scissorsusing namespace std;//enum class choice(rock,paper,scissors); //using namespace choice;//choice player_choice;//choice computer_choice;string words[3]={"rock","paper","scissors"};int player_choice;int computer_choice;int player_wins=0;int computer_wins=0;int game_numbers=0;//游戏局数int x=0,y=0,z=0;//石头,布,剪刀的次数 int favorite; //第一偏好 int second_favorite; //第二偏好 int third_favorite; //第三偏好 //choice get_computer_choice();int get_computer_choice();void decide_winner();string get_msg(int winner);int randOtoN1(int n);int max_number(int x,int y,int z);int main(){srand(time(NULL));string input_str;char c;while(true){cout<<"enter rock,paper,scissors or exit"<<endl;getline(cin,input_str);if(input_str.size()<1){cout<<"invalid input."<<endl;continue;//结束本次循环 }game_numbers++;//局数加一 c=input_str[0];//取首字母 if(c=='R'||c=='r'){//出石头 x++;player_choice=0;//0表示石头 }else if(c=='p'||c=='P'){y++;player_choice=1;//1表示布 }else if(c=='S'||c=='s'){z++;player_choice=2;//2表示剪刀 }else if(c=='E'||c=='e')//退出 break;else{cout<<"invalid input."<<endl;continue;}computer_choice=get_computer_choice();//int p=(int) player_choice;int p=player_choice;//int c=(int) computer_choice;int c=computer_choice;//cout<<"you chose "<<words[p];cout<<"you chose "<<words[p];cout<<","<<endl;//cout<<"I chose"<<words[c];cout<<"I chose "<<words[c];cout<<","<<endl;decide_winner();cout<<"game numbers are:"<<game_numbers<<endl<<endl;}system("pause");return 0;}void set_favorite(){//设置计算机偏好 //随机选出第一第二第三偏好 ,以50%的概率选择第一偏好,40%概率选择第二偏好 int n = randOtoN1(3);favorite = n;int m = randOtoN1(2);if(m == 0){second_favorite = (n+1)%3;third_favorite = (n+2)%3;}else{second_favorite = (n+2)%3;third_favorite = (n+1)%3;}int max = max_number(x,y,z); if((game_numbers)%interval == 0 && max != 0)//当局数等于间隔的整数倍时 且xyz不相等 //每隔三局判定用户偏好,制定策略 { if(max == x) { favorite = 2; if(y >= z){ second_favorite = 3; third_favorite = 1; } else{ second_favorite = 1; third_favorite = 3; } } else if(max == y) { favorite = 3; if(x >= z){ second_favorite = 2; third_favorite = 1; }else{ second_favorite = 1; third_favorite = 2; } } else { favorite = 1; if(x >= y){ second_favorite = 2; third_favorite = 3; }else{ second_favorite = 3; third_favorite = 2; } } } }int get_computer_choice(){set_favorite();int w = randOtoN1(10);if(w <= 5) return favorite;else if (w > 5 && w < 8) return second_favorite;else return third_favorite;}void decide_winner(){if(computer_choice==player_choice){cout<<"result is a tie"<<endl;cout<<"player wins:"<<player_wins<<endl;cout<<"computer wins:"<<computer_wins<<endl;return ;}//int p=static_cast<int>(play_choice);int p=player_choice;//int c=static_cast<int>(computer_choice);int c=computer_choice;if(p-c==1||p-c==-2){cout<<get_msg(player_choice);cout<<"you win"<<endl;player_wins++;}else{cout<<get_msg(computer_choice);cout<<"I win"<<endl;computer_wins++;}cout<<"player wins:"<<player_wins<<endl;cout<<"computer wins:"<<computer_wins<<endl;cout<<endl;}string get_msg(int winner){if(winner==0) return string("Rock smashes scissors...");else if(winner==1) return string("paper smashes rock...");else return string("scissors smashes paper...");} int randOtoN1(int n) //产生随机数的函数 {return rand()%n;}int max_number(int x,int y,int z){int max;if(x==y&&y==z) return 0;if(x>=y&&x>=z) max=x;else if(y>=x&&y>=z) max=y;else max=z;return max;}
阅读全文
0 0
- 智能版石头剪刀布——c++实现
- [C++]石头剪刀布之---最终幻想版
- C 【石头剪刀布 问题】
- 石头剪刀布c++实现
- 简单小游戏-剪刀石头布的c语言实现
- 黑马程序员——C基础之石头剪刀布游戏
- 石头、剪刀、布程序,C语言模拟
- C语言剪刀石头布小游戏
- 用Python实现的"石头,剪刀,布"
- Unity-->实现剪刀石头布的游戏
- JSP实现石头剪刀布游戏
- java实现剪刀石头布功能
- Python实现石头-剪刀-布小游戏
- 剪刀,石头,布游戏——类的初学习。
- Python入门——石头剪刀布程序
- OJ题——剪刀、石头和布
- 期末作业——石头剪刀布游戏
- Python学习——石头剪刀布小游戏
- spring+oauth 遇到的问题
- ios中列表UITableView的使用
- 【LeetCode】valid-parenthesis
- [Muduo网络库源码分析] (10) base/ThreadPoll_cc_h_线程池
- linux常用文件操作命令
- 智能版石头剪刀布——c++实现
- Webpack "Invalid Host Header"
- UITableView+FDTemplateLayoutCell xib demo 例子
- MVC5学习小记(4):自定义授权过滤器
- PMM部署遇到的坑
- 学习规划
- s2sh 分发服务
- Java transient关键字使用小记
- 最大似然估计(MLE)和最大后验概率(MAP)