欢迎来到天天文库
浏览记录
ID:39886030
大小:14.88 KB
页数:9页
时间:2019-07-14
《六子棋-人人对战-源代码》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、#ifndefChessBoard_H#defineChessBoard_h#defineBlack7#defineWhite1typedefstruct{intposX;intposY;intscore;}Position;classChessBoard{public:voidinitChessBoard();voidsetPosition(Positionpos);voidsetPosition(Positionpos1,Positionpos2,intside);boolisFull();boolisCover(Positionpos);bool
2、isWin(Positionpos1,Positionpos2);voidshowChesssBoard();private:intchessboard[19][19];};#endif#include#include#include"ChessBoard_H.h"usingnamespacestd;voidChessBoard::initChessBoard()//初始化棋盘{inti,j;for(i=0;i<19;i++){for(j=0;j<19;j++){chessboard[i][j]=0;}}}void
3、ChessBoard::setPosition(Positionpos)//设定第一步黑子落子位置{chessboard[pos.posX][pos.posY]=Black;}voidChessBoard::setPosition(Positionpos1,Positionpos2,intside)//设定各方棋子落子位置{if(side==1)//设定白方落子位置{if(isCover(pos1))//判断白方第一个落子位置是否重叠{do{cout<<"白方第一个坐标输入错误,请重新输入:";cin>>pos1.posX>>pos1.posY;}wh
4、ile(isCover(pos1));}chessboard[pos1.posX][pos1.posY]=White;if(isCover(pos2))//判断白方第二个落子位置是否重叠{do{cout<<"白方第二个坐标输入错误,请重新输入:";cin>>pos2.posX>>pos2.posY;}while(isCover(pos2));}chessboard[pos2.posX][pos2.posY]=White;}elseif(side==-1)//设定黑方落子位置{if(isCover(pos1))//判断黑方第一个落子位置是否重叠{do{c
5、out<<"黑方第一个坐标输入错误,请重新输入:";cin>>pos1.posX>>pos1.posY;}while(isCover(pos1));}chessboard[pos1.posX][pos1.posY]=Black;if(isCover(pos2))//判断黑方第二个落子位置是否重叠{do{cout<<"黑方第二个坐标输入错误,请重新输入:";cin>>pos2.posX>>pos2.posY;}while(isCover(pos2));}chessboard[pos2.posX][pos2.posY]=Black;}}boolChessB
6、oard::isFull()//判断棋盘的状态是否已满{inti,j;intflag=0;for(i=0;i<19;i++){for(j=0;j<19;j++){if(chessboard[i][j]==0){flag=1;}}}if(flag==0){returntrue;}else{returnfalse;}}boolChessBoard::isWin(Positionpos1,Positionpos2)//判断当前棋局是否分出输赢{intx1=pos1.posX,x2=pos2.posX;inty1=pos1.posY,y2=pos2.posY;
7、intflag=1;inti;intvalue1=chessboard[x1][y1];intvalue2=chessboard[x2][y2];for(i=1;i<6;i++){if((x1-i>=0)&&(y1-i>=0)&&chessboard[x1-i][y1-i]==value1)//判断左斜方向是否赢棋{flag++;}else{break;}}for(i=1;i<6;i++){if((x1+i<=19)&&(y1+i<=19)&&chessboard[x1+i][y1+i]==value1){flag++;}else{break;}}if
8、(flag>=6){returntrue;}flag=1;for(i=1;i<6;i++){i
此文档下载收益归作者所有