C++实现猜拳游戏

标准输入输出流
命名空间
主函数
如果你喜欢美观
你可以定义其它的函数
然后在主函数中调用

#include<iostream>
using namespace std;
int main()
{
    
    
return 0;
}

开始界面

system("color 71");//背景颜色
	//猜拳游戏
	//定义一个玩家类型
	//定义一个电脑类型
	//输出规则
	//获取输入
	//判断输入
	//判断随机数
	//判断结果
	//默认情况:退出程序
	cout << "----欢迎来到猜拳小游戏----" << endl;
	cout << "规则:" << endl;
	cout << "按0退出" << endl;
	cout << "与电脑进行猜拳,您需要输入对应的数字代表您出的是什么拳" << endl;
	cout << "[1]石头" << endl;
	cout << "[2]剪刀" << endl;
	cout << "[3]布  " << endl;
	cout << "-------祝您游戏愉快-------" << endl;

定义变量和用户的输入
因为电脑是随机输入所以调用了rand()函数
随机数a%3,得到的结果有0,1,2,所以+1,变为1,2,3

int playerInput;
cout<<"您可以出拳啦!!!"<<endl;
cout<<"请出拳:"<<endl;
cin>>playerInput;//输入
int npcInput;//定义电脑输入
npcInput=rand();
npcInput=rand() % 3 + 1;//随机数1,2,3

判断并且输出结果

switch(playerInput)
{
    
    
case 1://假如输入1,则是石头
cout<<"您出的是石头"<<endl;//输出提示语句
break;
case 2://假如输入2,则是剪刀
 cout<<"您出的是剪刀"<<endl;
 break;
case 3:
 cout<<"您出的是布  "<<endl;
 break;
case 0:
 goto esc;
 break;
default:
 cout<<"程序输入错误,请重启程序"<<endl;
 goto esc;
}
switch(npcInput)
{
    
    
case 1:
 cout<<"电脑出的是石头"<<endl;
 
  break;
case 2:
 cout<<"电脑出的是剪刀"<<endl;
   break;
case 3:
 cout<<"电脑出的是布  "<<endl;
}
//判断结局
//1.石头,2.剪刀,3.布
if(playerInput == npcInput)//当相同时
{
    
    
 cout<<"结果:"<<"平局"<<endl;
 cout<<"----------"<<endl;
}
else if(playerInput == npcInput-2)//当输入1,电脑随机数为3时
{
    
    
 cout<<"结果:"<<"电脑赢了!!!"<<endl;

  cout<<"----------"<<endl;
}
else if(playerInput == npcInput-1)//当输入1,电脑随机数为2时
{
    
    
 cout<<"结果:"<<"你赢了!!!"<<endl;
  cout<<"----------"<<endl;
}
else if(playerInput == npcInput+2)//当输入3,电脑随机数为1时
{
    
    
cout<<"结果:"<<"你赢了!!!"<<endl;
 cout<<"----------"<<endl;
}
else if(playerInput == npcInput+1)//当输入3,电脑随机数为2时
{
    
    
 cout<<"结果:"<<"电脑赢了!!!"<<endl;
  cout<<"----------"<<endl;
}

}while(9);//循环程序
esc://退出程序

cout<<"按任意键退出程序..."<<endl;

猜你喜欢

转载自blog.csdn.net/HONKER_MENGSHANG/article/details/107050720