《六子棋-人人对战-源代码.docx》由会员分享,可在线阅读,更多相关《六子棋-人人对战-源代码.docx(9页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、#ifndef ChessBoard_H#define ChessBoard_h#define Black 7#define White 1typedef struct int posX;int posY;int score;Position;class ChessBoardpublic:void initChessBoard();void setPosition(Position pos);void setPosition(Position pos1,Position pos2,int side);bool isFull();bool isCover(Position pos);bool i
2、sWin(Position pos1,Position pos2);void showChesssBoard();private:int chessboard1919;#endif#include#include#includeChessBoard_H.husing namespace std;void ChessBoard:initChessBoard()/初始化棋盘int i,j;for(i=0;i19;i+)for(j=0;j19;j+)chessboardij=0;void ChessBoard:setPosition(Position pos)/设定第一步黑子落子位置chessboa
3、rdpos.posXpos.posY=Black;void ChessBoard:setPosition(Position pos1,Position pos2,int side)/设定各方棋子落子位置if(side=1)/设定白方落子位置if(isCover(pos1)/判断白方第一个落子位置是否重叠docoutpos1.posXpos1.posY;while(isCover(pos1);chessboardpos1.posXpos1.posY=White;if(isCover(pos2)/判断白方第二个落子位置是否重叠docoutpos2.posXpos2.posY;while(isCov
4、er(pos2);chessboardpos2.posXpos2.posY=White;else if(side=-1)/设定黑方落子位置if(isCover(pos1)/判断黑方第一个落子位置是否重叠docoutpos1.posXpos1.posY;while(isCover(pos1);chessboardpos1.posXpos1.posY=Black;if(isCover(pos2)/判断黑方第二个落子位置是否重叠docoutpos2.posXpos2.posY;while(isCover(pos2);chessboardpos2.posXpos2.posY=Black;bool Ch
5、essBoard:isFull()/判断棋盘的状态是否已满int i,j;int flag=0;for(i=0;i19;i+)for(j=0;j19;j+)if(chessboardij=0)flag=1;if(flag=0)return true;elsereturn false;bool ChessBoard:isWin(Position pos1,Position pos2)/判断当前棋局是否分出输赢int x1=pos1.posX,x2=pos2.posX;int y1=pos1.posY,y2=pos2.posY;int flag=1;int i;int value1=chessbo
6、ardx1y1;int value2=chessboardx2y2;for(i=1;i=0) & (y1-i=0) & chessboardx1-iy1-i=value1)/判断左斜方向是否赢棋flag+;else break;for(i=1;i6;i+)if(x1+i=19) & (y1+i=6)return true;flag=1;for(i=1;i=0) & (y1+i=19) & chessboardx1-iy1+i=value1)/判断右斜方向是否赢棋flag+;elsebreak;for(i=1;i6;i+)if(x1+i=0) & chessboardx1+iy1-i=value
7、1)flag+;elsebreak;if(flag=6)return true;flag=1;for(i=1;i=0) & chessboardx1-iy1=value1)/判断竖直方向是否赢棋flag+;else break;for(i=1;i6;i+)if(x1+i=6)return true;flag=1;for(i=1;i=0) & chessboardx1y1-i=value1)/判断水平方向是否赢棋flag+;else break;for(i=1;i6;i+)if(y1+i=6)return true;return false;bool ChessBoard:isCover(Pos
8、ition pos)/判断落子位置是否重叠if(chessboardpos.posXpos.posY!=0)return true;elsereturn false;void ChessBoard:showChesssBoard()/输出棋盘状态int i,j;cout 六子棋 endl;cout ;for(i=0;i19;i+)coutsetw(3)i;coutendl;for(i=0;i19;i+)cout ;coutsetw(2)i;for(j=0;j19;j+)if(chessboardij=Black)cout ;else if(chessboardij=White)cout ;el
9、se if(chessboardij=0)cout -;cout setw(-2)i;coutendl;cout ;for(i=0;i19;i+)coutsetw(3)i;coutendl;coutendl;#include#includeChessBoard_H.husing namespace std;int main()Position pos1,pos2;int side=1;int flag=0;ChessBoard chessboard;chessboard.initChessBoard();dochessboard.showChesssBoard();if(flag=0)cout
10、pos1.posXpos1.posY;chessboard.setPosition(pos1);chessboard.showChesssBoard();system(cls);flag=1;else if(side=-1)coutpos1.posXpos1.posY;cinpos2.posXpos2.posY;chessboard.setPosition(pos1,pos2,side);chessboard.showChesssBoard();system(cls);if(chessboard.isWin(pos1,pos2)break;side=side*(-1);else if(side
11、=1)coutpos1.posXpos1.posY;cinpos2.posXpos2.posY;chessboard.setPosition(pos1,pos2,side);chessboard.showChesssBoard();system(cls);if(chessboard.isWin(pos1,pos2)break;side=side*(-1);while(!(chessboard.isFull();chessboard.showChesssBoard();if(!(chessboard.isFull()if(side=1)cout 白方赢!endl;elsecout 黑方赢!endl;elsecout 棋盘已满,双方平局!endl;return 0;