改
//蒟蒻自制,MAYBE有一点bug
#include <bits/stdc++.h>
#include <cstdlib>
#include <ctime>
#include <unistd.h>
using namespace std;
//防止版本低to_string CE
string tostr(int x){
string res;
if(x==0) return "0";
bool neg=0;
if(x<0){
neg=1;
x=-x;
}
while(x>0){
res+=(char)('0'+(x%10));
x/=10;
}
if(neg)res+='-';
reverse(res.begin(), res.end());
return res;
}
string s[4]={" ","剪刀","石头","布"};
struct Game{
int wingame,losegame,okgame,sumgame,killwho;
Game(){
wingame=0;
losegame=0;
okgame=0;
sumgame=0;
killwho=0;
}
};
void prinsc(string username,int wingame,int losegame,int okgame,int sumgame){
cout << username << "的战绩:\n";
cout << "赢局:" << wingame << "\n";
cout << "输局:" << losegame << "\n";
cout << "平局:" << okgame << "\n";
cout << "总场次:" << sumgame << "\n";
}
int getit(int l,int r){
int kill;
do{
cin >> kill;
if(kill>r||kill<l){
cout << "输入给我输入好的啊," << l << "~" << r << '\n';
}
} while(kill<l||kill>r);
return kill;
}
bool iwin(int a,int b){
if((a==2&&b==1)||(a==3&&b==2)||(a==1&&b==3)){
return 1;
}
return 0;
}
int main(){
srand(time(0));
while(1){
system("cls");
int choose;
cout << "请选择模式:1:普通 2:super 0结束\n";
choose=getit(0,2);
if(choose==1){
system("cls");
cout << "石头剪刀布(普通):\n石头>剪刀;剪刀>布;布>石头\n请选择是人机对战还是真人对战:\n1为人机对战 2为真人对战 0结束游戏\n";
int mod;
mod=getit(0,2);
if(!mod)break;
if(mod==1){
string username;
cout << "人机局\n请输入名字:\n";
cin >> username;
int user,wingame=0,losegame=0,okgame=0,sumgame=0;
while(1){
int robot=rand()%3+1;
int kill;
cout << "请输入您想要出的动作:1.剪刀 2.石头 3.布\n0为结束游戏(输入数字)";
user=getit(0,3);
if(!user){
break;
}
cout << "您出的" << s[user] << " 人机出的" << s[robot] << '\n';
if(user==robot){
cout << "平局\n";
okgame++;
}else if(!iwin(user,robot)){
cout << "失败\n";
losegame++;
}else {
cout << "胜利\n";
wingame++;
}
cout << "输入1继续 0结束\n";
kill=getit(0,1);
if(!kill)break;
}
sumgame=wingame+losegame+okgame;
prinsc("您",wingame,losegame,okgame,sumgame);
int kill;
cout << "是否再来一局?0结束1继续\n";
kill=getit(0,1);
if(!kill)break;
}else if(mod==2){
cout << "真人局\n";
int user1,user2;
string username1,username2;
int wingame=0,losegame=0,okgame=0,sumgame=0;
cout << "请输入名字\n玩家1:";
cin >> username1;
cout << "\n玩家2:";
cin >> username2;
while(1){
int kill;
cout << "请输入" << username1 << "想要出的动作:1.剪刀 2.石头 3.布\n0为结束游戏(输入数字)";
user1=getit(0,3);
if(!user1){
break;
}
system("cls");
cout << "请输入" << username2 << "想要出的动作:1.剪刀 2.石头 3.布\n0为结束游戏(输入数字)";
user2=getit(0,3);
if(!user2){
break;
}
cout << username1 << "出的" << s[user1] << " " << username2 << "出的" << s[user2] << '\n';
if(user1==user2){
cout << "平局\n";
okgame++;
}else if(!iwin(user1,user2)){
cout << username2 << "失败,"<< username1 << "胜利\n";
losegame++;
}else {
cout << username1 << "失败,"<< username2 << "胜利\n";
wingame++;
}
cout << "输入1继续 0结束\n";
kill=getit(0,1);
if(!kill)break;
}
sumgame=wingame+losegame+okgame;
prinsc(username1,losegame,wingame,okgame,sumgame);
prinsc(username2,wingame,losegame,okgame,sumgame);
int kill;
cout << "是否再来一局?0结束1继续\n";
kill=getit(0,1);
if(!kill)break;
}
}else if(choose==2){
system("cls");
cout << "石头剪刀布(super):\n石头>剪刀;剪刀>布;布>石头\n1为对战 0为结束游戏\n";
int mod;
mod=getit(0,1);
if(!mod)break;
else{
cout << "真人/人机局\n请输入人数,2~10\n";
int usercnt;
usercnt=getit(2,10);
int user[15];
string username[15];
Game usergame[15];
cout << "请输入真人数,0~" << usercnt << "\n";
int trcnt;
trcnt=getit(0,usercnt);
for(int i=1;i<=trcnt;i++){
cout << "请玩家" << i << "输入名字:\n";
cin >> username[i];
}
int robot[15];
string robotname[15];
int robotkill[15];
for(int i=trcnt+1;i<=usercnt;i++){
robotname[i]=tostr(i)+"号(人机)";
}
while(1){
int kill;
bool flag=0;
for(int i=trcnt+1;i<=usercnt;i++){
do{
robotkill[i]=rand()%usercnt+1;
}while(robotkill[i]==i);
}
for(int ti=1;ti<=trcnt;ti++){
system("cls");
cout << "请输入" << username[ti] << "想要出的动作:1.剪刀 2.石头 3.布\n0为结束游戏(输入数字)\n";
user[ti]=getit(0,3);
if(user[ti]==0){
flag=1;
break;
}
cout << "请选择决斗对象:\n";
for(int i=1;i<=trcnt;i++){
cout << i << ". " << username[i] << '\n';
}
for(int i=trcnt+1;i<=usercnt;i++){
cout << i << ". " << robotname[i] << '\n';
}
do{
usergame[ti].killwho=getit(1,usercnt);
if(usergame[ti].killwho==ti)cout << "不可为自己!\n";
}while(usergame[ti].killwho==ti);
}
if(flag)break;
for(int i=trcnt+1;i<=usercnt;i++){
robot[i]=rand()%3+1;
}
for(int i=1;i<=trcnt;i++){
if(flag) break;
int opp=usergame[i].killwho;
cout << username[i] << "的决斗对象是:" << opp << "\n";
string userobjname;
int userobjact;
if(opp<=trcnt){
userobjact=user[opp];
userobjname=username[opp];
}else{
userobjact=robot[opp];
userobjname=robotname[opp];
}
cout << username[i] << "出的" << s[user[i]] << " " << userobjname << "出的" << s[userobjact] << "\n";
if(user[i]==userobjact){
cout << "平局\n";
usergame[i].okgame++;
if(opp<=trcnt) usergame[opp].okgame++;
}else if(!iwin(user[i],userobjact)){
cout << username[i] << "失败,"<< userobjname << "胜利\n";
usergame[i].losegame++;
if(opp<=trcnt) usergame[opp].wingame++;
}else {
cout << userobjname << "失败,"<< username[i] << "胜利\n";
usergame[i].wingame++;
if(opp<=trcnt) usergame[opp].losegame++;
}
cout << "输入1继续 0结束\n";
kill=getit(0,1);
if(!kill){flag=1;break;}
}
if(flag) break;
for(int i=trcnt+1;i<=usercnt;i++){
if(flag) break;
cout << robotname[i] << "的决斗对象是:";
int opp=robotkill[i];
int myact=robot[i];
int userobjact;
string userobjname;
if(opp<=trcnt){
userobjact=user[opp];
userobjname=username[opp];
}else{
userobjact=robot[opp];
userobjname=robotname[opp];
}
cout << userobjname << '\n';
cout << robotname[i] << "出的" << s[myact] << " " << userobjname << "出的" << s[userobjact] << '\n';
if(myact==userobjact){
cout << "平局\n";
if(opp<=trcnt) usergame[opp].okgame++;
}else if(!iwin(myact,userobjact)){
cout << robotname[i] << "失败,"<< userobjname << "胜利\n";
if(opp<=trcnt) usergame[opp].wingame++;
}else {
cout << userobjname << "失败,"<< robotname[i] << "胜利\n";
if(opp<=trcnt) usergame[opp].losegame++;
}
cout << "输入1继续 0结束\n";
kill=getit(0,1);
if(!kill){flag=1;break;}
}
if(flag)break;
}
for(int i=1;i<=trcnt;i++){
usergame[i].sumgame=usergame[i].wingame+usergame[i].okgame+usergame[i].losegame;
}
for(int i=1;i<=trcnt;i++){
prinsc(username[i],usergame[i].wingame,usergame[i].losegame,usergame[i].okgame,usergame[i].sumgame);
}
}
}
int kill;
cout << "是否再来一局?0结束1继续\n";
kill=getit(0,1);
if(!kill)break;
}
return 0;
}