五子棋.docx
- 文档编号:23864865
- 上传时间:2023-05-21
- 格式:DOCX
- 页数:20
- 大小:18.02KB
五子棋.docx
《五子棋.docx》由会员分享,可在线阅读,更多相关《五子棋.docx(20页珍藏版)》请在冰豆网上搜索。
五子棋
#include
#include
usingnamespacestd;
classCHESS
{
public:
CHESS();
voidsetStep(bool&ipjudge);//双人对战轮流走棋函数
voidsetStepC(bool&ipjudge);//人机对战走棋函数
voidcoutChess();//输出棋盘
voidcoutPW();//输出权值表
boolgetTurn(){flag=!
flag;returnflag;}//轮流走棋控制函数
voidflushChess();//刷新棋盘信息函数
voidjudgeWin();//判断是否赢棋函数
voidwiner();//赢家输出函数
intgetAns(){returnresult;}//返回结果(赢家判断)
staticintcount;//走棋步数变量
private:
boolflag;//轮流走棋判断变量
intPW[16][16],tPW[4];//权值变量,最高权值变量
intresult,num[2];//结果(赢家判断),玩家输入棋子坐标判断
charinPut[2],temp[2];//玩家输入数据,转换暂存数据
charCBoard[16][16];//棋盘数据
intjudgeAWin(inta,intb);//判断是否A为赢家函数
intjudgeBWin(inta,intb);//判断是否B为赢家函数
voidcSetStep();//电脑走棋函数
voidsetPower();//初始化权值函数
intadddepth(intdepth);//填充权值函数
voidjudgePw(intx,inty,intdirection,intdepth,chartest);//棋子判断[x坐标,y坐标,方向(顺时针0-无,1-左,2-左上,3-上,4-右上),深度(depth),标识符(A/B)]
voidgetFinalPw();//权值判断函数
};
intCHESS:
:
count=0;
voidVsComputer();//人机对战
voidVsPlayer();//双人对战
intmain()
{
intchoose;
CHESSnewP;
do
{
choose=0;
system("cls");
cout<<"欢乐五子棋"< cout<<"请选择: "< cout<<"1: 人机对战模式"< cout<<"2: 双人对战模式"< cout<<"3: 退出游戏"< cout<<"**************"< cout<<"**************"< cout<<"请输入你的选择: "; cin>>choose; if(choose==2) VsPlayer(); elseif(choose==1) VsComputer(); elseif(choose==3) exit(0); else { cout<<"输入错误,请重新输入! "< system("pause"); } }while(choose! =3); return0; } voidVsComputer() { boolipjudge; CHESSnewP; do { newP.coutChess(); newP.setStepC(ipjudge);//人机对战函数 if(! ipjudge) continue; if(! newP.getTurn()) newP.flushChess(); newP.coutChess(); newP.judgeWin(); CHESS: : count++; }while(newP.getAns()==0&&CHESS: : count<256); newP.winer(); } voidCHESS: : setStepC(bool&ipjudge) { inti; if(flag) { cout<<"棋手走棋: "; cin>>inPut; for(i=0;i<=1;i++) if(inPut[i]<'0'||(inPut[i]>'9'&&inPut[i]<'O')||(inPut[i]>'F'&&inPut[i]<'O')||inPut[i]>'f') { ipjudge=false; cout<<"输入越界,请重新输入! "; system("pause"); break; } } else cSetStep();//轮到电脑走棋 } voidCHESS: : cSetStep() { inti,j,depth=0; setPower(); for(i=0;i<16;i++) { for(j=0;j<16;j++) { if(CBoard[i][j]=='+')//优化: 排除周围全是+的情况 { if(CBoard[i-1][j]=='O'||CBoard[i-1][j-1]=='O'||CBoard[i][j-1]=='O'||CBoard[i+1][j-1]=='O'||CBoard[i+1][j]=='O'||CBoard[i+1][j+1]=='O'||CBoard[i][j+1]=='O'||CBoard[i-1][j+1]=='O') { judgePw(i,j,0,depth,'O'); judgePw(i,j,0,depth,'X'); } elseif(CBoard[i-1][j]=='X'||CBoard[i-1][j-1]=='X'||CBoard[i][j-1]=='X'||CBoard[i+1][j-1]=='X'||CBoard[i+1][j]=='X'||CBoard[i+1][j+1]=='X'||CBoard[i][j+1]=='X'||CBoard[i-1][j+1]=='X') { judgePw(i,j,0,depth,'O'); judgePw(i,j,0,depth,'X'); } } } } getFinalPw(); //coutPW(); //system("pause"); if(tPW[0]>0) CBoard[tPW[1]][tPW[2]]='X'; /*elseif(tPW[0]>0&&tPW[3]>1) { for(i=0;i<16;i++) { for(j=0;j<16;j++) { if(tPW[0]==PW[i][j]) if() } } }*/ else { cout<<"权值函数错误! "; system("pause"); exit (1); } } voidCHESS: : judgePw(intx,inty,intdirection,intdepth,chartest) { if(depth>=0&&depth<4) { if(direction==1)//左方 { if(CBoard[x-depth-1][y]==test) judgePw(x,y,1,depth+1,test); else PW[x][y]+=adddepth(depth); } elseif(direction==2)//左上方 { if(CBoard[x-depth-1][y-depth-1]==test) judgePw(x,y,2,depth+1,test); else PW[x][y]+=adddepth(depth); } elseif(direction==3)//正上方 { if(CBoard[x][y-depth-1]==test) judgePw(x,y,3,depth+1,test); else PW[x][y]+=adddepth(depth); } elseif(direction==4)//右上方 { if(CBoard[x+depth+1][y-depth-1]==test) judgePw(x,y,4,depth+1,test); else PW[x][y]+=adddepth(depth); } elseif(direction==5)//右方 { if(CBoard[x+depth+1][y]==test) judgePw(x,y,5,depth+1,test); else PW[x][y]+=adddepth(depth); } elseif(direction==6)//右下方 { if(CBoard[x+depth+1][y+depth+1]==test) judgePw(x,y,6,depth+1,test); else PW[x][y]+=adddepth(depth); } elseif(direction==7)//正下方 { if(CBoard[x][y+depth+1]==test) judgePw(x,y,7,depth+1,test); else PW[x][y]+=adddepth(depth); } elseif(direction==8)//左下方 { if(CBoard[x-depth-1][y+depth+1]==test) judgePw(x,y,6,depth+1,test); else PW[x][y]+=adddepth(depth); } elseif(direction==0) { if(CBoard[x-depth-1][y]==test)//左方 judgePw(x,y,1,depth+1,test); if(CBoard[x-depth-1][y-depth-1]==test)//左上方 judgePw(x,y,2,depth+1,test); if(CBoard[x][y-depth-1]==test)//正上方 judgePw(x,y,3,depth+1,test); if(CBoard[x+depth+1][y-depth-1]==test)//右上方 judgePw(x,y,4,depth+1,test); if(CBoard[x+depth+1][y]==test)//右方 judgePw(x,y,5,depth+1,test); if(CBoard[x+depth+1][y+depth+1]==test)//右下方 judgePw(x,y,6,depth+1,test); if(CBoard[x][y+depth+1]==test)//正下方 judgePw(x,y,7,depth+1,test); if(CBoard[x-depth-1][y+depth+1]==test)//左下方 judgePw(x,y,6,depth+1,test); } } elseif(depth==4) PW[x][y]+=adddepth(depth); else { cout<<"深度函数出错! "; system("pause"); exit (1); } } intCHESS: : adddepth(intdepth) { switch(depth) { case0: return0;break; case1: return1;break; case2: return2;break; case3: return4;break; case4: return6;break; default: { cout<<"深度函数错误! "; system("pause"); exit (1); } } } voidCHESS: : getFinalPw() { inti,j; for(i=0;i<=15;i++) for(j=0;j<=15;j++) { if(PW[i][j]>tPW[0]) { tPW[0]=PW[i][j]; tPW[1]=i; tPW[2]=j; tPW[3]=1; } elseif(PW[i][j]==tPW[0]&&PW[i][j]! =0) tPW[3]+=1; } } //////////////////////////////////////////////////////////////////////// //双人对战 //////////////////////////////////////////////////////////////////////// voidVsPlayer() { boolipjudge; CHESSnewP; do { newP.coutChess(); newP.setStep(ipjudge); if(! ipjudge) continue; newP.getTurn(); newP.flushChess(); newP.coutChess(); newP.judgeWin(); CHESS: : count++; }while(newP.getAns()==0&&CHESS: : count<256); newP.winer(); } voidCHESS: : winer() { if(CHESS: : count==256) { cout<<"|||||||||平局! 本局结束! "< system("pause"); exit (1); } if(result==1) cout<<"|||||||||恭喜! 棋手A赢得本局胜利! "< elseif(result==2) cout<<"|||||||||恭喜! 棋手B赢得本局胜利! "< system("pause"); } //默认构造函数 CHESS: : CHESS() { inti,j; count=0; result=0; flag=true; for(i=0;i<=15;i++) { if(i<10) CBoard[i][0]=i+48; else CBoard[i][0]=i+55; } for(i=0;i<=15;i++) for(j=0;j<=15;j++) CBoard[i][j]='+'; } //填充权值函数 voidCHESS: : setPower() { inti,j; for(i=0;i<=15;i++) for(j=0;j<=15;j++) PW[i][j]=0; tPW[0]=0; tPW[1]=0; tPW[2]=0; tPW[3]=0; } //输出棋盘函数 voidCHESS: : coutChess() { inti,j; system("cls"); cout<<"欢乐五子棋"< cout<<"0123456789ABCDEF"< for(i=0;i<=15;i++) { if(i>=0&&i<10) cout< elseif(i>=10) cout< else cout<<"棋盘列序号输出错误! "; for(j=0;j<=15;j++) cout< cout< } cout< } //双人对战,棋手轮流走棋函数 voidCHESS: : setStep(bool&ipjudge) { if(flag) cout<<"棋手A走棋: "; else cout<<"棋手B走棋: "; cin>>inPut; for(inti=0;i<=1;i++) { if(inPut[i]<'0'||(inPut[i]>'9'&&inPut[i]<'O')||(inPut[i]>'F'&&inPut[i]<'O')||inPut[i]>'f') { ipjudge=false; cout<<"输入越界,请重新输入! "; system("pause"); break; } } } //刷新棋盘 voidCHESS: : flushChess() { inti; temp[0]=inPut[0]; temp[1]=inPut[1]; for(i=0;i<=1;i++) { if(temp[i]>='0'&&temp[i]<='9') num[i]=static_cast elseif(temp[i]>='O'&&temp[i]<='F') num[i]=static_cast elseif(temp[i]>='O'&&temp[i]<='f') num[i]=static_cast else { cout<<"用户输入未知错误1,请重新输入! "; system("pause"); exit (1); } } if(CBoard[num[0]][num[1]]=='+'&&! flag) CBoard[num[0]][num[1]]='O'; elseif(CBoard[num[0]][num[1]]=='+'&&flag) CBoard[num[0]][num[1]]='X'; else { flag=! flag; cout<<"用户输入错误,请重新输入! "; system("pause"); } } //判断胜出新算法 voidCHESS: : judgeWin() { inti=0,j; do { j=0; do { if(CBoard[i][j]=='O') result=judgeAWin(i,j); elseif(CBoard[i][j]=='X') result=judgeBWin(i,j); elseif(CBoard[i][j]=='+'); else { cout<<"棋盘["< "< system("pause"); exit (1); } if(result==1||result==2) break; j++; }while(j<=15); if(result==1||result==2) break; i++; }while(i<=15); } //对棋手A的棋子检测 intCHESS: : judgeAWin(inta,intb) { if(CBoard[a][b-1]=='O'&&CBoard[a][b-2]=='O'&&CBoard[a][b+1]=='O'&&CBoard[a][b+2]=='O')//横向五子 return1; elseif(CBoard[a+1][b]=='O'&&CBoard[a+2][b]=='O'&&CBoard[a-1][b]=='O'&&CBoard[a-2][b]=='O')//纵向五子 return1; elseif(CBoard[a+1][b+1]=='O'&&CBoard[a+2][b+2]=='O'&&CBoard[a-1][b-1]=='O'&&CBoard[a-2][b-2]=='O')//左上右下五子 { return1; } elseif(CBoard[a+1][b-1]=='O'&&CBoard[a+2][b-2]=='O'&&CBoard[a-1][b+1]=='O'&&CBoard[a-2][b+2]=='O')//左下右上五子 return1; else return0; } //对棋手B的棋子检测 intCHESS: : judgeBWin(inta,intb) { if(CBoard[a][b-1]=='X'&&CBoard[a][b-2]=='X'&&CBoard[a][b+1]=='X'&&CBoard[a][b+2]=='X')//横向五子 return2; elseif(CBoard[a+1][b]=='X'&&CBoard[a+2][b]=='X'&&CBoard[a-1][b]=='X'&&CBoard[a-2][b]=='X')//纵向五子 return2; elseif(CBoard[a+1][b+1]=='X'&&CBoard[a+2][b+2]=='X'&&CBoard[a-1][b-1]=='X'&&CBoard[a-2][b-2]=='X')//左上右下五子 return2; elseif(CBoard[a+1][b-1]=='X'&&CBoard[a+2][b-2]=='X'&&CBoard[a-1][b+1]=='X'&&CBoard[a-2][b+2]=='X')//左下右上五子 return2; else return0; } //输出权值表 voidCHESS: : coutPW() { inti,j; system("cls"); cout<<"欢乐五子棋(权值表)"< cout<<"0123456789ABCDEF"< for(i=0;i<=15;i++) { if(i>=0&&i<10) cout< elseif(i>=10) cout< else cout<<"棋盘列序号输出错误! "; for(j=0;j<=15;j++) cout< cout< } cout< }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 五子棋
![提示](https://static.bdocx.com/images/bang_tan.gif)