else;
}
voidstick:
:
reduceHP()
{
if(HP>0)--HP;
else;
}
voidstartgame(intx,inty)
{
inti,j,a,b,n[3];//n[]是生成三种棋子的个数
intgameover[3];//gameover[]判断游戏结束计数
intreduce[4];//reduce[]是判断是否有两个以上的棋子在格子里
intstep,axes;//axes表示方向;step表示走的距离
CELL**p_unit=newCELL*[x];
for(i=0;i{
p_unit[i]=newCELL[y];
}
Urandtemp0(x*y);//产生随机个物体
temp0.seed(time(NULL));
for(i=0;i<3;i++)n[i]=temp0()/3;
tiger*p_tiger=newtiger[n[0]];
chicken*p_chicken=newchicken[n[1]];
stick*p_stick=newstick[n[2]];
Urandtemp(x);//给每个生成的物体随机赋予地址
temp.seed(time(NULL));
srand((unsigned)time(NULL));
for(i=0;ia=temp();
b=rand()%y;
p_tiger[i].setvalue(a,b);
p_unit[a][b].setthing
(1);
}
for(i=0;ia=temp();
b=rand()%y;
p_chicken[i].setvalue(a,b);
p_unit[a][b].setthing
(2);
}
for(i=0;ia=temp();
b=rand()%y;
p_stick[i].setvalue(a,b);
p_unit[a][b].setthing(3);
}
for(i=0;ifor(j=0;jp_unit[i][j].displaycell();
p_unit[i][j].empty();
}
cout<cout<Urandtstep(4);//随机运动
tstep.seed(time(NULL));
Urandcstep
(2);
cstep.seed(time(NULL));
Urandsstep(3);
sstep.seed(time(NULL));
srand((unsigned)time(NULL));
do
{
for(i=0;i<3;i++)
{
gameover[i]=0;
}
for(i=0;iloop1:
axes=tstep();
step=tstep();
if(p_tiger[i].HP>0)
{
p_tiger[i].move(x,y,step,axes);
if(p_unit[p_tiger[i].xx][p_tiger[i].yy].coincide
(1))//判断下一跳是否有同样的角色
{
p_tiger[i].x=p_tiger[i].xx;
p_tiger[i].y=p_tiger[i].yy;
p_unit[p_tiger[i].x][p_tiger[i].y].setthingmove
(1);
}
else{
p_tiger[i].xx=p_tiger[i].x;
p_tiger[i].yy=p_tiger[i].y;
gotoloop1;
}
gameover[0]=1;}
}
for(i=0;iloop2:
axes=rand()%8;
step=cstep();
if(p_chicken[i].HP>0)
{p_chicken[i].move(x,y,step,axes);
if(p_unit[p_chicken[i].xx][p_chicken[i].yy].coincide
(2))
{
p_chicken[i].x=p_chicken[i].xx;
p_chicken[i].y=p_chicken[i].yy;
p_unit[p_chicken[i].x][p_chicken[i].y].setthingmove
(2);
}
elsegotoloop2;
gameover[1]=1;}
}
for(i=0;iloop3:
axes=rand()%4;
step=sstep();
if(p_stick[i].HP>0)
{p_stick[i].move(x,y,step,axes);
if(p_unit[p_stick[i].xx][p_stick[i].yy].coincide(3))
{
p_stick[i].x=p_stick[i].xx;
p_stick[i].y=p_stick[i].yy;
p_unit[p_stick[i].x][p_stick[i].y].setthingmove(3);
}
elsegotoloop3;
gameover[2]=1;}
}
for(i=0;ifor(j=0;jp_unit[i][j].reduce(reduce[0],reduce[1],reduce[2],reduce[3]);
if(reduce[0])//表示tiger和chicken都在时chicken减血
{
for(inta=0;aif(p_chicken[a].x==i&&p_chicken[a].y==j){
p_chicken[a].reduceHP();
}
else;
}
}
elseif(reduce[1])//表示tiger和stick都在时减血
{
for(inta=0;aif(p_tiger[a].x==i&&p_tiger[a].y==j){
p_tiger[a].reduceHP();
}
else;}
}
elseif(reduce[2])//表示chicken和stick都在时减血
{
for(inta=0;aif(p_stick[a].x==i&&p_stick[a].y==j){
p_stick[a].reduceHP();
}
else;}
}
elseif(reduce[3])//表示三者都在时减血
{
for(inta=0;aif(p_chicken[a].x==i&&p_chicken[a].y==j){
p_chicken[a].reduceHP();
}
else;}
for(a=0;aif(p_tiger[a].x==i&&p_tiger[a].y==j){
p_tiger[a].reduceHP();
}
else;}
for(a=0;aif(p_stick[a].x==i&&p_stick[a].y==j){
p_stick[a].reduceHP();
}
else;}
}
else;
}}
for(i=0;ifor(j=0;jp_unit[i][j].displaycell();
p_unit[i][j].empty();
}
cout<cout<}while(!
((gameover[0]+gameover[1]+gameover[2]==1)||(gameover[0]+gameover[1]+gameover[2]==0)));
delete[]p_stick;//释放开辟的空间
delete[]p_chicken;
delete[]p_tiger;
for(i=0;idelete[]p_unit;
};
intmain()
{intx,y;
cout<<"请输入棋盘的长:
";
cin>>x;
cout<<"请输入棋盘的宽:
";
cin>>y;
startgame(x,y);
return0;
}