//扩展:联机玩这个游戏,异常处理(输入的数超出范围,以及输入的不是那个类型处理情况)
#include<iostream>
#include<time.h>
#include<stdlib.h>
#include<process.h>
using namespace std;
//int i;
struct Player
{
int bout[2];//attack and defend in a bout
int life;
}player,computer;
void showStart()
{
cout<<"************************************/n";
cout<<" author:Alice/n";
cout<<" Start/n";//以后可以做个按钮以此开始游戏
cout<<" menu:/n";
cout<<" 1:attack(defend) the head/n";
cout<<" 2:attack(defend) the waist/n";
cout<<" 3:attack(defend) the foot/n";
cout<<"*************************************/n";
cout<<"Please Input the number of bouts:";
}
void InputManner(bool pass=false,int w=0,int i=1)
{
while(pass==false)//此处需要改进,在读者输入字符时,会出现死机情况
{
int temp;
if(0==w)
{
cout<<"Please input.."<<i<<"..attack manner:";
cin>>temp;
}
else
{
cout<<"Please input.."<<i<<"..defend manner:";
cin>>temp;
}
if(temp>=1&&temp<=3)
{
player.bout[w]=temp;
//cout<<"******************/n";
w==0?w=1:pass=true;
}
}
}
void showChoose()
{
cout<<"***********************************/n";
cout<<"**Game Start! player vs computer**/n";
cout<<"please choose who to play first"<<endl;
cout<<"choose=0,the player first"<<endl;
cout<<"choose=1,the computer first"<<endl;
cout<<"Now Input choose=";
}
void win()
{
cout<<"Congraduations,you win!"<<endl;
}
void lose()
{
cout<<"Sorry,you lose"<<endl;
}
//if the result=1,the player win,result=0,the computer win
int attack(int cho,int pla,int com)//cho=choose who to attack first,pla=player,com=computer
{
int result;
if(0==cho)//the player attack first
{
switch(pla)
{
case 1:
cout<<"the player attack the head of the computer!"<<endl;
break;
case 2:
cout<<"the player attack the waist of the computer!"<<endl;
break;
case 3:
cout<<"the player attack the foot of the computer!"<<endl;
break;
}
switch(com)
{
case 1:
cout<<"the computer defend the attack on the head"<<endl;
break;
case 2:
cout<<"the computer defend the attack on the waist"<<endl;
break;
case 3:
cout<<"the computer defend the attack on the foot"<<endl;
break;
}
if(pla==com)
{
result=0;//the computer win
}
else
{
result=1;//the player win
}
}
if(1==cho)//the computer attack first
{
switch(com)
{
case 1:
cout<<"the computer attack the head of the player!"<<endl;
break;
case 2:
cout<<"the computer attack the waist of the player!"<<endl;
break;
case 3:
cout<<"the computer attack the foot of the player!"<<endl;
break;
}
switch(pla)
{
case 1:
cout<<"the player defend the attack on the head"<<endl;
break;
case 2:
cout<<"the player defend the attack on the waist"<<endl;
break;
case 3:
cout<<"the player defend the attack on the foot"<<endl;
break;
}
if(pla==com)
{
result=1;//the player win
}
else
{
result=0;//the computer win
}
}
return result;
}
void Choose(int i)
{
int choose;
//if(choose!=0||choose!=1)
cin>>choose;
int useAttack;
srand(time(0) ^ _getpid());
int n=1+rand()%3;
if(0==choose)
{
cout<<"the player"<<i<<"attack the computer/n";
useAttack=attack(choose,player.bout[0],n);
choose=1;
if(useAttack==0)
{
cout<<"the computer win/n";
player.life--;
}
else
{
cout<<"the player win/n";
computer.life--;
}
cout<<"the life of the player is"<<player.life<<"/n";
cout<<"the life of the computer is"<<computer.life<<"/n";
}
else if(1==choose)
{
cout<<"the computer"<<i<<"attack the player/n";
useAttack=attack(choose,player.bout[1],n);
choose=0;
i=1;
if(useAttack==1)
{
cout<<"the player defends successfully and win"<<endl;
computer.life--;
}
else
{
cout<<"the computer win"<<endl;
player.life--;
}
cout<<"the computer's life is"<<computer.life<<endl;
cout<<"the player's life is"<<player.life<<endl;
}
}
int main()
{
showStart();
int bouts;
cin>>bouts;
player.life=bouts;
computer.life=bouts;
for(int i=1;i<=bouts;i++)
{
InputManner(false,0,i);
showChoose();
Choose(i);
}
if(computer.life<player.life)
win();
else if(computer.life>player.life)
lose();
else
cout<<"equal"<<endl;
}