棋盘游戏.docx
- 文档编号:11025472
- 上传时间:2023-02-24
- 格式:DOCX
- 页数:16
- 大小:17.24KB
棋盘游戏.docx
《棋盘游戏.docx》由会员分享,可在线阅读,更多相关《棋盘游戏.docx(16页珍藏版)》请在冰豆网上搜索。
棋盘游戏
#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)
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 棋盘 游戏