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

本文介绍了一款使用C++实现的智能石头剪刀布游戏。游戏通过分析玩家的选择来调整计算机的策略,提高胜率。计算机有第一、第二、第三偏好,并根据玩家的行为调整这些偏好。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


河北工业大学计算机软件技术基础(VC) 课程设计任务书 题目:人机对战——石头剪刀 目的与要求 目的 通过编写该程序,培养学生综合利用C++语言进行程序设计的能力,加强函数的运用及学生对软件工程方法的初步认识,提高软件系统分析能力和程序文档建立、归纳总结的能力,培养学生利用系统提供的标准函数及典型算法进行设计,并对Windows平台下的控制台进行深入的了解。 基本要求 要求用C++语言编程,在Visual C++环境下调试完成; 要求划分功能模块,各个功能分别使用函数来完成; 源代码程序要求必要的注释。 设计方法和基本原理 课题功能描述 编写一个人机进行石头剪刀的小游戏。要求能够进行完一轮猜拳后,显示统计信息。每一轮游戏的过程是首先由玩家选择要出的拳,然后计算机选择出拳,输出本轮游戏的胜负情况,然后输出统计信息(玩家胜利次数、计算机胜利次数和平局次数),然后每轮猜拳结束后,询问玩家是否要继续游戏游戏界面如下: 主要技术问题的描述 随机函数 随机函数名为rand(),使用时必须包含头文件stdlib.h。 创新要求 在基本要求达到后,进行创新设计: 程序要保证计算机的胜利次数至少是玩家胜利次数的1.5倍。 课程设计的考核方式及评分方法 考核方式 (1) 学生要提交书面课程设计报告(A4纸打印);并将设计报告的电子文档、.cpp源文件和.h头文件放到一个文件夹里上传到所对应班级的学生名称相应文件夹中。 (2) 课程设计结束时,在机房当场验收。教师提供测试数据,由学生运行所设计的系统,检查运行结果是否正确,并回答教师提出的有关问题。 评分方法 根据出勤率、课程设计期间纪律、课程设计运行结果、课程设计报告及答辩情况综合评分。 书写设计报告的要求(详细内容见“设计报告模板”) 课程设计的有关文档“设计报告模板”和“课程设计要求”请在下载任务书处下载。    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wangbowj123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值