新微赢技术网
标题:
猜拳游戏求教
[打印本页]
作者:
地瓜
时间:
2009-11-4 00:00
标题:
猜拳游戏求教
#include <iostream>
#include <ctime>
#include <stdlib.h>
using namespace std;
void CQ()
{int i,j;
srand(unsigned(time(0)));
cout<<"输入的数值为1,2,3否则后果自负"<<endl;
cout<<"1-石头 2-剪刀 3-布"<<endl<<"你与对方大喊到:";
cin>>i;
if(i<=3&&i!=0)
{//你出拳
if(i==1)cout<<"石头!"<<endl;
if(i==2)cout<<"剪刀!"<<endl;
if(i==3)cout<<"布!"<<endl;
//它出拳
j=(rand()%3+1);
if(j==1)cout<<"石头!"<<endl;
if(j==2)cout<<"剪刀!"<<endl;
if(j==3)cout<<"布!"<<endl;
//比大小
if(j==i)
{cout<<"你俩不相上下啊!"<<endl;}
if(j==1&&i==2)
{cout<<"你说到:佩服!佩服!还是你高!"<<endl;}
if(i==1&&j==2)
{cout<<"它说到:它NND该死的剪刀!"<<endl;}
if(j==2&&i==3)
{cout<<"你说到佩服!佩服!还是你高!"<<endl;}
if(j==3&&i==2)
{cout<<"它说到:它NND该死的布!"<<endl;}
if(j==1&&i==3)
{cout<<"它说到:它NND该死的石头!"<<endl;}
if(j==3&&i==1)
{cout<<"你说到:佩服!佩服!还是你高!"<<endl;}
}
else
{cout<<"它说道:哎! 就 就 就你不按套路出牌!"<<endl;exit(0);}
}
int main()
{int i,k=0,p=0,q=0;
cout<<"你要来多少局"<<endl;
cin>>i;
while(i!=0)
{CQ();i--;}
return 0;
}
我还想实现 平局 胜利 失败的统计数和当我输入数值不是1,2,3时能够重新输入。
作者:
听妈妈的话
时间:
2009-11-4 00:00
挺有意思的闹
作者:
敬你永远遗忘
时间:
2009-11-4 00:00
小游戏的总结构自己想好 在写出个大概的总程序 多想多改----完美程序
出错的小地方想得实在不明白或者...... 才可以问别人么
你已经画出了轮廓,在帮它补妆,就OK了`
你写出了该游戏的核心部分,加油噢~~
作者:
风忠人
时间:
2009-11-4 00:00
我写的一个
#include <iostream>
#include <ctime>
using namespace std;
class Random
{
private:
Random() { srand(time(0)); }
Random(const Random &ri) { }
void operator=(Random &ri) { }
public:
static Random& get() { static Random r; return r; }
int random() { return rand(); }
};
const string& RPS(int rps)
{
const static string rps_str[3] = {"Rock", "Scissors", "Paper"};
switch(rps)
{
case 1: return rps_str[0];
case 2: return rps_str[1];
case 3: return rps_str[2];
default:
cout<<"RPS error"<<endl;
}
}
class PK
{
public:
void run(int n)
{
cout<<"input 1-Rock 2-Scissors 3-Paper"<<endl;
win = loss = draw = 0;
int total = n;
while(n-- != 0)
{
your_act();
computer_act();
judge();
}
cout<<"\nresult\ntotal : "<<total<<"\nyou win : "<<win
<<"\nyou loss : "<<loss<<"\ndraw : "<<draw<<endl;
}
private:
void your_act()
{
cout<<"\nyour turn\n";
char in;
cin>>in;
while(in<'1' || in>'3')
{
cout<<"illegality!over again\n";
cin>>in;
}
switch(in)
{
case '1': you = 1; cout<<"you "<<RPS(1)<<endl;break;
case '2': you = 2; cout<<"you "<<RPS(2)<<endl;break;
case '3': you = 3; cout<<"you "<<RPS(3)<<endl;break;
}
}
void computer_act()
{
computer = (Random::get().random()%3 + 1);
}
void judge()
{
if(you == computer)
{
draw++;
cout<<"computer "<<RPS(computer)<<"\ndraw"<<endl;
}
else if(you - computer == 1 || you - computer == -2)
{
loss++;
cout<<"computer "<<RPS(computer)<<"\nyou loss"<<endl;
}
else
{
win++;
cout<<"computer "<<RPS(computer)<<"\nyou win"<<endl;
}
}
int you, computer;
int win, loss, draw;
};
int main()
{
int n;
cout<<"how many ?"<<endl;
cin>>n;
PK pk;
pk.run(n);
return 0;
}
复制代码
欢迎光临 新微赢技术网 (http://bbs.weiying.cn/)
Powered by Discuz! X3.2