题目
输出分为几个部分:
1:说明
石头(0)剪刀(1)布(2)
石头0>剪刀1
剪刀1>布 2
布2>石头0
2:游戏
无限循环,
AI随机出一个,
接着再由你出,
两者PK,
输出AI和你的输入
输出胜负
3:计分
201积分制
胜利+2分,失败不加,平局各加1分
每次把分数输出一遍
导入库
<iostream> 输入,输出
<ctime> 随机数辅助库
<cstdlib> 随机数
#include <iostream>
#include <ctime>
#include <cstdlib>
变量
a(AI),b(你),n(胜负),s1(你的分数),s2(AI分数)
int a,b,n,s1=0,s2=0;
判断胜负(用函数)
int PSS(int a,int b){
if (a==b) return 1;
if (a==0){
if (b==1) return 0;
else return 2;
}
if (a==1){
if (b==0) return 2;
else return 0;
}
if (a==2){
if (b==0) return 0;
else return 2;
}
}
其中2代表胜利,1平局,0失败
输入和比较
cout<<endl;
srand(time(0));
a=rand()%3;
cin>>b;
b=b%3;
n=PSS(a,b);
计分板操作
switch (n){
case 1:s1++;s2++;break;
case 2:s1+=2;break;
case 0:s2+=2;break;
}
输出操作
cout<<"COMPUTER>> "<<a<<"VS"<<b<<" <<YOU"<<endl;
switch (n){
case 1:cout<<"平局";break;
case 2:cout<<"胜利";break;
case 0:cout<<"失败";break;
}
cout<<endl;
switch (n){
case 1:s1++;s2++;break;
case 2:s1+=2;break;
case 0:s2+=2;break;
}
cout<<"COMPUTER>> "<<s2<<":"<<s1<<" <<YOU"<<endl;
代码展示
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int PSS(int a,int b){
if (a==b) return 1;
if (a==0){
if (b==1) return 0;
else return 2;
}
if (a==1){
if (b==0) return 2;
else return 0;
}
if (a==2){
if (b==0) return 0;
else return 2;
}
}
int main(){
cout<<"石头=0,剪刀=1,布=2"<<endl;
cout<<"石头WIN剪刀,剪刀WIN布,布WIN石头"<<endl;
int a,b,n,s1=0,s2=0;
while (1){
cout<<endl;
srand(time(0));
a=rand()%3;
cin>>b;
b=b%3;
n=PSS(a,b);
cout<<"COMPUTER>> "<<a<<"VS"<<b<<" <<YOU"<<endl;
switch (n){
case 1:cout<<"平局";break;
case 2:cout<<"胜利";break;
case 0:cout<<"失败";break;
}
cout<<endl;
switch (n){
case 1:s1++;s2++;break;
case 2:s1+=2;break;
case 0:s2+=2;break;
}
cout<<"COMPUTER>> "<<s2<<":"<<s1<<" <<YOU"<<endl;
}
return 0;
}
(作者第二次发IT文章,希望大家能承蒙关照)
你们还想让我编点什么?
在QQ邮箱3547877477@qq.com告诉我吧!
下期预告:
计算器(栈)程序