五子棋源代码C++完美版.docx
- 文档编号:8122959
- 上传时间:2023-01-28
- 格式:DOCX
- 页数:22
- 大小:17.21KB
五子棋源代码C++完美版.docx
《五子棋源代码C++完美版.docx》由会员分享,可在线阅读,更多相关《五子棋源代码C++完美版.docx(22页珍藏版)》请在冰豆网上搜索。
五子棋源代码C++完美版
#include
#include
usingnamespacestd;
#defineWIDE_AND_LONG20//棋盘的长和宽
#defineNAME_LEN20//输入姓名的长度
typedefclassGobang
{
public:
intInitPlayerName(char*,char*);
intCheckInput(char,int);
intCheckIndexInput(char,char,char);
intGetPlayerName();
intInitBoard();
intWriteBoard(char,char,bool);
intBeginOrNot();
intCheckRow();
intCheckColumn();
intCheckTopLeft();
intCheckTopRight();
intCheckDownLeft();
intCheckDownRight();
intCheckDraw();
intCheckFinish();
intChess();
intShowBoard();
private:
characBoard[WIDE_AND_LONG][WIDE_AND_LONG];
characPlayerOneName[NAME_LEN];
characPlayerTwoName[NAME_LEN];
}GOBANG;
//初始化姓名的缺省值
intGOBANG:
:
InitPlayerName(char*pPlayerOne,char*pPlayerTwo)
{
strcpy(acPlayerOneName,pPlayerOne);
strcpy(acPlayerTwoName,pPlayerTwo);
return0;
}
//检查输入姓名时是否含非法字符空格和Tab键
intGOBANG:
:
CheckInput(charch,intiNameLen)
{
if(''==ch||'\t'==ch)
{
cout<<"含有非法字符!
"< return-1; } if(iNameLen>NAME_LEN-1) { cout<<"输入超出限定长度! "< return-1; } return0; } //将棋子放到棋盘中 intGOBANG: : WriteBoard(charcRow,charcColumn,boolbJudge) { intiRow=0; intiColumn=0; if(cRow>='0'&&cRow<='9') { iRow=static_cast } elseif(cRow>='A'&&cRow<=static_cast { iRow=static_cast } else { iRow=static_cast } if(cColumn>='0'&&cColumn<='9') { iColumn=static_cast } elseif(cColumn>='A'&&cColumn<=static_cast { iColumn=static_cast } else { iColumn=static_cast } if('+'! =acBoard[iRow][iColumn]) { cout<<"此处已有棋子! "< return-1; } if(! bJudge) { acBoard[iRow][iColumn]=static_cast (1); } else { acBoard[iRow][iColumn]=static_cast (2); } return0; } //检查坐标输入是否合法 intGOBANG: : CheckIndexInput(charcRow,charcSeparator,charcColumn) { if(! cRow||! cSeparator||! cColumn) { return-1; } if(! ((cRow>='0'&&cRow<='9')||\ (cRow>='A'&&cRow<=static_cast (cRow>='a'&&cRow<=static_cast ) { return-1; } if(''! =cSeparator&&'\t'! =cSeparator&&','! =cSeparator) { return-1; } if(! ((cColumn>='0'&&cColumn<='9')||\ (cColumn>='A'&&cColumn<=static_cast (cColumn>='a'&&cColumn<=static_cast ) { return-1; } return0; } //获取玩家的昵称 intGOBANG: : GetPlayerName() { fflush(stdin); charcTemp=0; intiNameLen=0; cout<<"是否自己定义昵称? 是(Y),否(任意键): "; cTemp=getch(); cout< if(('y'! =cTemp)&&('Y'! =cTemp)) { return0; } fflush(stdin); memset(acPlayerOneName,0,sizeof(acPlayerOneName)); memset(acPlayerTwoName,0,sizeof(acPlayerTwoName)); cout<<"请玩家一输入昵称: "; while('\n'! =(cTemp=getchar())) { if(-1==CheckInput(cTemp,iNameLen)) { fflush(stdin); cout<<"请玩家一输入昵称: "; continue; } acPlayerOneName[iNameLen]=cTemp; iNameLen++; } if(0==iNameLen) { strcpy(acPlayerOneName,"玩家一"); } iNameLen=0; fflush(stdin); cout<<"请玩家二输入昵称: "; while('\n'! =(cTemp=getchar())) { if(-1==CheckInput(cTemp,iNameLen)) { fflush(stdin); cout<<"请玩家二输入昵称: "; continue; } acPlayerTwoName[iNameLen]=cTemp; iNameLen++; } if(0==iNameLen) { strcpy(acPlayerTwoName,"玩家一"); } return0; } //初始化棋盘 intGOBANG: : InitBoard() { intiRow=0; intiColumn=0; for(iRow=0;iRow { for(iColumn=0;iColumn { acBoard[iRow][iColumn]='+'; } } return0; } //检查棋盘横向是否存在五子连珠 intGOBANG: : CheckRow() { intiRow=0; intiColumn=0; intiPlayerOneLen=0; intiPlayerTwoLen=0; for(iRow=0;iRow { while(iColumn { if(static_cast (1)==acBoard[iRow][iColumn]) { iPlayerOneLen++; iPlayerTwoLen=0; } elseif(static_cast (2)==acBoard[iRow][iColumn]) { iPlayerTwoLen++; iPlayerOneLen=0; } else { iPlayerTwoLen=0; iPlayerOneLen=0; } iColumn++; } if(iPlayerOneLen>=5) { return1; } if(iPlayerTwoLen>=5) { return2; } iPlayerOneLen=0; iPlayerTwoLen=0; iColumn=0; } return0; } //检查棋盘竖向是否存在五子连珠 intGOBANG: : CheckColumn() { intiRow=0; intiColumn=0; intiPlayerOneLen=0; intiPlayerTwoLen=0; for(iColumn=0;iColumn { while(iRow { if(static_cast (1)==acBoard[iRow][iColumn]) { iPlayerOneLen++; iPlayerTwoLen=0; } elseif(static_cast (2)==acBoard[iRow][iColumn]) { iPlayerTwoLen++; iPlayerOneLen=0; } else { iPlayerTwoLen=0; iPlayerOneLen=0; } iRow++; } if(iPlayerOneLen>=5) { return1; } if(iPlayerTwoLen>=5) { return2; } iPlayerOneLen=0; iPlayerTwoLen=0; iRow=0; } return0; } //检查棋盘左上方(包括对角线)是否存在五子连珠 intGOBANG: : CheckTopLeft() { intiRow=0; intiTempRow=0; intiColumn=0; intiPlayerOneLen=0; intiPlayerTwoLen=0; for(iRow=4;iRow { iTempRow=iRow; while(iTempRow>=0) { if(static_cast (1)==acBoard[iTempRow][iColumn]) { iPlayerOneLen++; iPlayerTwoLen=0; if(iPlayerOneLen>=5) { return1; } } elseif(static_cast (2)==acBoard[iTempRow][iColumn]) { iPlayerTwoLen++; iPlayerOneLen=0; if(iPlayerTwoLen>=5) { return2; } } else { iPlayerTwoLen=0; iPlayerOneLen=0; } iTempRow--; iColumn++; } iPlayerOneLen=0; iPlayerTwoLen=0; iColumn=0; } return0; } //检查棋盘右上方(包括对角线)是否存在五子连珠 intGOBANG: : CheckTopRight() { intiRow=0; intiColumn=0; intiTempColumn=0; intiPlayerOneLen=0; intiPlayerTwoLen=0; for(iColumn=0;iColumn { iTempColumn=iColumn; while(iTempColumn { if(static_cast (1)==acBoard[iRow][iTempColumn]) { iPlayerOneLen++; iPlayerTwoLen=0; if(iPlayerOneLen>=5) { return1; } } elseif(static_cast (2)==acBoard[iRow][iTempColumn]) { iPlayerTwoLen++; iPlayerOneLen=0; if(iPlayerTwoLen>=5) { return2; } } else { iPlayerTwoLen=0; iPlayerOneLen=0; } iRow++; iTempColumn++; } iPlayerOneLen=0; iPlayerTwoLen=0; iRow=0; } return0; } //检查棋盘左下方(不包括对角线)是否存在五子连珠 intGOBANG: : CheckDownLeft() { intiRow=0; intiTempRow=0; intiColumn=0; intiPlayerOneLen=0; intiPlayerTwoLen=0; for(iRow=1;iRow { iTempRow=iRow; while(iTempRow { if(static_cast (1)==acBoard[iTempRow][iColumn]) { iPlayerOneLen++; iPlayerTwoLen=0; if(iPlayerOneLen>=5) { return1; } } elseif(static_cast (2)==acBoard[iTempRow][iColumn]) { iPlayerTwoLen++; iPlayerOneLen=0; if(iPlayerTwoLen>=5) { return2; } } else { iPlayerTwoLen=0; iPlayerOneLen=0; } iTempRow++; iColumn++; } iPlayerOneLen=0; iPlayerTwoLen=0; iColumn=0; } return0; } //检查棋盘右下方(不包括对角线)是否存在五子连珠 intGOBANG: : CheckDownRight() { intiRow=0; intiTempRow=0; intiColumn=WIDE_AND_LONG-1; intiPlayerOneLen=0; intiPlayerTwoLen=0; for(iRow=1;iRow { iTempRow=iRow; while(iTempRow { if(static_cast (1)==acBoard[iTempRow][iColumn]) { iPlayerOneLen++; iPlayerTwoLen=0; if(iPlayerOneLen>=5) { return1; } } elseif(static_cast (2)==acBoard[iTempRow][iColumn]) { iPlayerTwoLen++; iPlayerOneLen=0; if(iPlayerTwoLen>=5) { return2; } } else { iPlayerTwoLen=0; iPlayerOneLen=0; } iTempRow++; iColumn--; } iPlayerOneLen=0; iPlayerTwoLen=0; iColumn=WIDE_AND_LONG-1; } return0; } //检查是否平局 intGOBANG: : CheckDraw() { intiRow=0; intiColumn=0; for(iRow=0;iRow { for(iColumn=0;iColumn { if('+'==acBoard[iRow][iColumn]) { return1; } } } return0; } //检查是否达到结束的条件(五子连珠或平局) intGOBANG: : CheckFinish() { intiJudgeRow=0; intiJudgeColumn=0; intiJudgeTopLeft=0; intiJudgeTopRight=0; intiJudgeDownLeft=0; intiJudgeDownRight=0; intiJudgeDraw=0; iJudgeRow=CheckRow(); iJudgeColumn=CheckColumn(); iJudgeTopLeft=CheckTopLeft(); iJudgeTopRight=CheckTopRight(); iJudgeDownLeft=CheckDownLeft(); iJudgeDownRight=CheckDownRight(); iJudgeDraw=CheckDraw(); if(1==iJudgeRow||1==iJudgeColumn||1==iJudgeTopLeft||\ 1==iJudgeTopRight||1==iJudgeDownLeft||1==iJudgeDownRight) { cout<<"恭喜玩家<"< "< return1; } if(2==iJudgeRow||2==iJudgeColumn||2==iJudgeTopLeft||\ 2==iJudgeTopRight||2==iJudgeDownLeft||2==iJudgeDownRight) { cout<<"恭喜玩家<"< "< return1; } if(0==iJudgeDraw) { cout<<"平局! "< return1; } return0; } //显示棋盘到控制台 intGOBANG: : ShowBoard() { intiRow=0; intiColumn=0; system("cls"); cout<<""; for(iRow=0;iRow { if(9 { cout< } else { cout< } } cout< for(iRow=0;iRow { if(9 { cout< } else { cout< } for(iColumn
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 五子棋 源代码 C+ 完美