猜拳小游戏
发表在达人风采
2020-01-03
是否精华
是
否
版块置顶:
是
否
#include<iostream>
#include <ctime>
using namespace std;
void main()
{
system("color 71");//背景颜色
time_t now = time(0);//存储
tm *ltm = localtime(&now);
//猜拳小游戏
//定义一个玩家类型
//定义一个电脑类型
//输出规则
//获取输入
//判断输入
//判断随机数
//判断结果
//默认情况:退出程序
cout<<"----欢迎来到猜拳小游戏----"<<endl;
cout<<"规则:"<<endl;
cout<<"按0退出"<<endl;
cout<<"与电脑进行猜拳,您需要输入对应的数字代表您出的是什么拳"<<endl;
cout<<"[1]石头"<<endl;
cout<<"[2]剪刀"<<endl;
cout<<"[3]布 "<<endl;
cout<<"打开游戏的时间是:"<<1900+ ltm ->tm_year<<"年"<<ends;
cout<<1 + ltm->tm_mon<<"月"<<ends;
cout<<ltm->tm_mday<<"日"<<ends;
cout<<ltm->tm_hour<<"时"<<ltm->tm_min<<"分"<<ltm->tm_sec<<"秒"<<endl;
cout<<"-------祝您游戏愉快-------"<<endl;//时间
do
{
int playerInput;
cout<<"您可以出拳啦!!!"<<endl;
cout<<"请出拳:"<<endl;
cin>>playerInput;//输入
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;
}
int npcInput;//定义电脑输入
npcInput=rand();
npcInput=rand() % 3 + 1;//随机数1,2,3
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;
cout<<"结束游戏的时间是:"<<1900+ ltm ->tm_year<<"年"<<ends;
cout<<1 + ltm->tm_mon<<"月"<<ends;
cout<<ltm->tm_mday<<"日"<<ends;
cout<<ltm->tm_hour<<"时"<<ltm->tm_min<<"分"<<ltm->tm_sec<<"秒"<<endl;
}
购物车
发表新帖
立即签到







