智能版石头剪刀布——c++实现

利用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 scissors
using 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;
}

原文链接: https://www.cnblogs.com/young-for-you/p/7286898.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    智能版石头剪刀布——c++实现

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/257703

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月14日 上午11:10
下一篇 2023年2月14日 上午11:10

相关推荐