棋盘游戏.docx

上传人:b****7 文档编号:11025472 上传时间:2023-02-24 格式:DOCX 页数:16 大小:17.24KB
下载 相关 举报
棋盘游戏.docx_第1页
第1页 / 共16页
棋盘游戏.docx_第2页
第2页 / 共16页
棋盘游戏.docx_第3页
第3页 / 共16页
棋盘游戏.docx_第4页
第4页 / 共16页
棋盘游戏.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

棋盘游戏.docx

《棋盘游戏.docx》由会员分享,可在线阅读,更多相关《棋盘游戏.docx(16页珍藏版)》请在冰豆网上搜索。

棋盘游戏.docx

棋盘游戏

#include

#include

usingnamespacestd;

classRandint{//均匀分布函数,取自互联网,梅森旋转算法,假定32位long

unsignedlongrandx;

public:

Randint(longs=0){randx=s;}

voidseed(longs){randx=s;}

//魔幻数选用32位long中的31位

longabs(longx){returnx&0x7fffffff;}

staticdoublemax(){return2147483648.0;}//注意:

double

longdraw(){returnrandx=randx*1103515245;}

doublefdraw(){returnabs(draw())/max();}//在区间[0,1]

longoperator()(){returnabs(draw());}//在区间[0,pow(2,31)]

};

classUrand:

publicRandint{//均匀分布,区间[0:

n-1]

longn;

public:

Urand(longnn){n=nn;}

longoperator()(){longr=n*fdraw();return(r==n)?

n-1:

r;}

};

classCELL//格子

{

public:

CELL();

intreduce(int&,int&,int&,int&);//reduce[]是判断是否有两个以上的棋子在格子里

voidsetthing(int);//初始化时设置各棋子的位置

voidsetthingmove(int);//移动时改变格子里面含有棋子的状态

intcoincide(int);//判断是否下一跳已经有了同类棋子

voiddisplaycell();//显示格子

voidempty();//清空格子所含棋子状态

private:

inttiger;//用来记录格子是否拥有棋子

intchicken;

intstick;

};

CELL:

:

CELL(){//棋子初始化,默认不拥有任何棋子

tiger=0;

chicken=0;

stick=0;

}

intCELL:

:

reduce(int&a,int&b,int&c,int&d){

a=tiger==1&&chicken==1&&stick==0;

b=tiger==1&&chicken==0&&stick==1;

c=tiger==0&&chicken==1&&stick==1;

d=tiger==1&&chicken==1&&stick==1;

return0;

}

voidCELL:

:

setthing(inta)

{

if(tiger==0&&chicken==0&&stick==0&&a==1)tiger=1;

elseif(tiger==0&&chicken==0&&stick==0&&a==2)chicken=1;

elseif(tiger==0&&chicken==0&&stick==0&&a==3)stick=1;

}

voidCELL:

:

setthingmove(inta)

{

if(tiger==0&&a==1)tiger=1;

if(chicken==0&&a==2)chicken=1;

if(stick==0&&a==3)stick=1;

}

intCELL:

:

coincide(inta)

{

if(tiger==1&&a==1)a=0;

if(chicken==1&&a==2)a=0;

if(stick==1&&a==3)a=0;

returna;

}

voidCELL:

:

displaycell()

{

if(tiger==0&&chicken==0&&stick==0)cout<<"口";

elseif(tiger==1&&chicken==0&&stick==0)cout<<"○";//○表示tiger

elseif(tiger==0&&chicken==1&&stick==0)cout<<"◇";//◇表示chicken

elseif(tiger==0&&chicken==0&&stick==1)cout<<"△";//△表示stick

elseif(tiger==1&&chicken==1&&stick==0)cout<<"●";//●表示tiger和chicken

elseif(tiger==1&&chicken==0&&stick==1)cout<<"◆";//◆表示tiger和stick

elseif(tiger==0&&chicken==1&&stick==1)cout<<"▲";//▲表示chicken和stick

elsecout<<"■";//表示三者都有

}

voidCELL:

:

empty()

{

tiger=0;

chicken=0;

stick=0;

};

classthing

{

public:

voidsetvalue(int,int,int);

voidmove(int,int,int,int);

voidreduceHP();

intHP;

intxx,yy;

intx,y;

};

classtiger:

publicthing//老虎

{

public:

tiger(){

xx=0,yy=0;

x=0;

y=0;

HP=3;

};

voidsetvalue(int,int);//设置棋子的坐标

voidmove(int,int,int,int);//棋子跳动,改变坐标

voidreduceHP();//对棋子进行减血

};

voidtiger:

:

setvalue(inta,intb)

{

xx=a;yy=b;

x=a;

y=b;

}

voidtiger:

:

move(inta,intb,intst,intax)

{

--a,--b;

intstep,axes;//axes:

0表示向左,1表示向右,2表示向上,3表示向下;step表示走的距离

step=st;

axes=ax;

if(axes==0&&(yy-step)>=0)yy=yy-step;

elseif(axes==1&&(yy+step)

elseif(axes==2&&(xx-step)>=0)xx=xx-step;

elseif(axes==3&&(xx+step)

else;

}

voidtiger:

:

reduceHP()

{

if(HP>0)--HP;

else;

}

classchicken:

publicthing//鸡

{

public:

chicken(){

xx=0,yy=0;

x=0;

y=0;

HP=3;

};

voidsetvalue(int,int);

voidmove(int,int,int,int);

voidreduceHP();

};

voidchicken:

:

setvalue(inta,intb)

{

xx=a;yy=b;

x=a,y=b;

}

voidchicken:

:

move(inta,intb,intst,intax)

{

--a,--b;

intstep,axes;//axes:

0表示向左,1表示向右,2表示向上,3表示向下,4表示左上,5表示右上,6表示左下,7表示右下;step表示走的距离

step=st;

axes=ax;

if(axes==0&&(yy-step)>=0)yy=yy-step;

elseif(axes==1&&(yy+step)

elseif(axes==2&&(xx-step)>=0)xx=xx-step;

elseif(axes==3&&(xx+step)

elseif(axes==4&&(yy-step)>=0&&(xx-step)>=0)xx=xx-step,yy=yy-step;

elseif(axes==5&&(xx-step)>=0&&(yy+step)

elseif(axes==6&&(yy-step)>=0&&(xx+step)

elseif(axes==7&&(yy+step)<=b&&(xx+step)<=a)xx=xx+step,yy=yy+step;

else;

}

voidchicken:

:

reduceHP()

{

if(HP>0)--HP;

else;

}

classstick:

publicthing//棒子

{

public:

stick(){

xx=0,yy=0;

x=0;

y=0;

HP=3;

};

voidsetvalue(int,int);

voidmove(int,int,int,int);

voidreduceHP();

};

voidstick:

:

setvalue(inta,intb)

{

xx=a;yy=b;

x=a;

y=b;

}

voidstick:

:

move(inta,intb,intst,intax)

{

--a,--b;

intstep,axes;//axes:

0表示向左上,1表示向右上,2表示向左下,3表示右下;step表示走的距离

step=st;

axes=ax;

if(axes==0&&(yy-step)>=0&&(xx-step)>=0)xx=xx-step,yy=yy-step;

elseif(axes==1&&(xx-step)>=0&&(yy+step)

elseif(axes==2&&(yy-step)>=0&&(xx+step)

elseif(axes==3&&(xx+step)

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;i

a=temp();

b=rand()%y;

p_tiger[i].setvalue(a,b);

p_unit[a][b].setthing

(1);

}

for(i=0;i

a=temp();

b=rand()%y;

p_chicken[i].setvalue(a,b);

p_unit[a][b].setthing

(2);

}

for(i=0;i

a=temp();

b=rand()%y;

p_stick[i].setvalue(a,b);

p_unit[a][b].setthing(3);

}

for(i=0;i

for(j=0;j

p_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;i

loop1:

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;i

loop2:

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;i

loop3:

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;i

for(j=0;j

p_unit[i][j].reduce(reduce[0],reduce[1],reduce[2],reduce[3]);

if(reduce[0])//表示tiger和chicken都在时chicken减血

{

for(inta=0;a

if(p_chicken[a].x==i&&p_chicken[a].y==j){

p_chicken[a].reduceHP();

}

else;

}

}

elseif(reduce[1])//表示tiger和stick都在时减血

{

for(inta=0;a

if(p_tiger[a].x==i&&p_tiger[a].y==j){

p_tiger[a].reduceHP();

}

else;}

}

elseif(reduce[2])//表示chicken和stick都在时减血

{

for(inta=0;a

if(p_stick[a].x==i&&p_stick[a].y==j){

p_stick[a].reduceHP();

}

else;}

}

elseif(reduce[3])//表示三者都在时减血

{

for(inta=0;a

if(p_chicken[a].x==i&&p_chicken[a].y==j){

p_chicken[a].reduceHP();

}

else;}

for(a=0;a

if(p_tiger[a].x==i&&p_tiger[a].y==j){

p_tiger[a].reduceHP();

}

else;}

for(a=0;a

if(p_stick[a].x==i&&p_stick[a].y==j){

p_stick[a].reduceHP();

}

else;}

}

else;

}}

for(i=0;i

for(j=0;j

p_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;i

delete[]p_unit;

};

intmain()

{intx,y;

cout<<"请输入棋盘的长:

";

cin>>x;

cout<<"请输入棋盘的宽:

";

cin>>y;

startgame(x,y);

return0;

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 法学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1