#include <time.h>
#include<stdlib.h> //rand()函数所在头文件
#include<iostream>
using namespace std;
enum Gamestatus{win,lose,tie};
Gamestatus status;
////////////////定义用户出石头,剪刀,布时所对应的数字,将数字转化成对应的石头剪刀布输出
void display(int a)
{
switch(a)
{
case 0:
{cout<<"石头"<<endl;}
break;
case 1:
{cout<<"剪刀"<<endl;}
break;
case 2:
{cout<<"布"<<endl;}
}
}
///////////////////////////////////////////////判断输赢
int judge(int x,int y)
{
Gamestatus status;
switch(x)
{
case 0:
if(y==0){
cout<<"平局"<<endl;
status=tie;
}else if(y==1){
cout<<"你赢一局";
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
status=win;
} else if(y==2){
cout<<"你输一局"<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
status=lose;
}
break;//一定要加break!!!!!!要检查!!!!!
case 1:
if(y==1){
cout<<"平局"<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
status=tie;
}else if(y==0){
cout<<"你输一局"<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
status=lose;
} else if(y==2){
cout<<"你赢一局"<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
status=win;
}
break;
case 2:
if(y==2){
cout<<"平局"<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
status=tie;
}else if(y==1){
cout<<"你输一局"<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
status=lose;
}else if(y==0){
cout<<"你赢一局"<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
status=win;
}
}
return status;//把status值返回给调用它的Play函数
}
int Play(){
int x;
srand((unsigned)time(NULL));//表示以当前时间对应的int值为随机序列起点,这样每次运行程序,由于起点不同得到不同的随机数
int y = rand()%3;//产生随机数0,1,2,如果想产生的是1,2,3就加1
cout<<"请出拳:"<<endl;
cin>>x;
cout<<"你出";
display(x);
while(1)
{
if(y!=0)//防止无限循环
break;
}
cout<<"电脑出";
display(y);
int f=judge(x,y);
return f;
}
void Continue1(){
int f;
do{
f=Play();}
while(f==tie);
if(f==win){
cout<<"恭喜你战胜了电脑!!"<<endl;
}else
cout<<"很遗憾,您输了"<<endl;
}
void Continue2(){
int f;
do{
f=Play();}
while(f==tie);
if(f==win){
Continue1();
}
else
cout<<"很遗憾,您输了"<<endl;}
void Continue3(){
int f;
do{
f=Play();}
while(f==tie);
if(f==lose){
Continue1();
}
else
cout<<"恭喜你战胜了电脑!!"<<endl;}
void Continue4(){
int flag1=2;
int flag2=0;
int f;
do{
f=Play();
if(f==win){
flag1=++flag1;}
else if(f==lose){
flag2=++flag2;}}
while(f==tie);
if(flag2==1){
Continue4();
}
else
cout<<"恭喜你战胜了电脑!!"<<endl;
}
void Continue5(){
int flag1=0;
int flag2=2;
int f;
do{
f=Play();
if(f==win){
flag1=++flag1;}
else if(f==lose){
flag2=++flag2;}}
while(f==tie);
if(flag1==1){
Continue5();
}
else
cout<<"很遗憾,您输了"<<endl;}
void Set1(){
int flag1=0;
int flag2=0;
int i;
for(i=0;i<2;i++){
int g=Play();
if(g==win){
flag1=++flag1;
}
else if(g==lose){
flag2=++flag2;
}
}
if(flag1==2){
cout<<"恭喜你战胜了电脑!!"<<endl;
}else if(flag2==2){
cout<<"很遗憾,您输了"<<endl;
}else if(flag1==1&&flag2==1){
Continue1(); }
else if(flag1==1&&flag2==0){
Continue3();}
else if(flag1==0&&flag2==1){
Continue2();}
else
Set1();
}
void Set2(){
int flag1=0;
int flag2=0;
int i;
for(i=0;i<3;i++){
int g=Play();
if(g==win){
flag1=++flag1;
}
else if(g==lose){
flag2=++flag2;
}
}
if(flag1==3){
cout<<"恭喜你战胜了电脑!!"<<endl;
}else if(flag2==3){
cout<<"很遗憾,您输了"<<endl;
}else if(flag1==1&&flag2==2){
Continue2(); }
else if(flag1==2&&flag2==1){
Continue3();}
else if(flag1==1&&flag2==1){
Set1();}
else if(flag1==2&&flag2==0){
Continue4();}
else if(flag1==0&&flag2==2){
Continue5();}
else
Set2();
}
int main()
{
int a;
cout<<"欢迎来到剪刀石头布游戏,按1选择三局两胜制,按2选择五局三胜制"<<endl;
cin>>a;
switch (a)//选择三局两胜或五局三胜
{
case 1:
cout<<"您选择三局两胜"<<endl;
cout<<"0代表出石头,1代表出剪刀,2代表出布"<<endl;
Set1();
break;
case 2:
cout<<"您选择五局三胜制"<<endl;
cout<<"1代表出石头,2代表出剪刀,3代表出布"<<endl;
Set2();
break;
}
getchar();
return 0;
}
然后运行经常这样卡住,无论按什么键都没有反应,程序也不进行了,卡的时间随机,但全部都是在电脑准备出拳的时候卡住,请问是什么原因?