五子棋源代码C++完美版Word格式文档下载.docx
《五子棋源代码C++完美版Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《五子棋源代码C++完美版Word格式文档下载.docx(22页珍藏版)》请在冰豆网上搜索。
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键
CheckInput(charch,intiNameLen)
if('
'
==ch||'
\t'
==ch)
{
cout<
<
"
含有非法字符!
endl;
return-1;
}
if(iNameLen>
NAME_LEN-1)
输入超出限定长度!
//将棋子放到棋盘中
WriteBoard(charcRow,charcColumn,boolbJudge)
intiRow=0;
intiColumn=0;
if(cRow>
='
0'
&
&
cRow<
9'
)
iRow=static_cast<
int>
(cRow-'
);
elseif(cRow>
A'
=static_cast<
char>
('
+WIDE_AND_LONG-10-1))
((cRow-'
)+10);
else
a'
if(cColumn>
cColumn<
iColumn=static_cast<
(cColumn-'
elseif(cColumn>
((cColumn-'
+'
!
=acBoard[iRow][iColumn])
此处已有棋子!
if(!
bJudge)
acBoard[iRow][iColumn]=static_cast<
(1);
(2);
//检查坐标输入是否合法
CheckIndexInput(charcRow,charcSeparator,charcColumn)
cRow||!
cSeparator||!
cColumn)
((cRow>
)||\
(cRow>
+WIDE_AND_LONG-10-1))||\
+WIDE_AND_LONG-10-1)))
)
=cSeparator&
'
=cSeparator)
((cColumn>
(cColumn>
//获取玩家的昵称
GetPlayerName()
fflush(stdin);
charcTemp=0;
intiNameLen=0;
是否自己定义昵称?
是(Y),否(任意键):
;
cTemp=getch();
if(('
y'
=cTemp)&
('
Y'
=cTemp))
memset(acPlayerOneName,0,sizeof(acPlayerOneName));
memset(acPlayerTwoName,0,sizeof(acPlayerTwoName));
请玩家一输入昵称:
while('
\n'
=(cTemp=getchar()))
if(-1==CheckInput(cTemp,iNameLen))
{
continue;
acPlayerOneName[iNameLen]=cTemp;
iNameLen++;
if(0==iNameLen)
strcpy(acPlayerOneName,"
玩家一"
iNameLen=0;
请玩家二输入昵称:
acPlayerTwoName[iNameLen]=cTemp;
strcpy(acPlayerTwoName,"
//初始化棋盘
InitBoard()
for(iRow=0;
iRow<
WIDE_AND_LONG;
iRow++)
for(iColumn=0;
iColumn<
iColumn++)
acBoard[iRow][iColumn]='
//检查棋盘横向是否存在五子连珠
CheckRow()
intiPlayerOneLen=0;
intiPlayerTwoLen=0;
iRow++)
while(iColumn<
WIDE_AND_LONG)
if(static_cast<
(1)==acBoard[iRow][iColumn])
iPlayerOneLen++;
iPlayerTwoLen=0;
elseif(static_cast<
(2)==acBoard[iRow][iColumn])
iPlayerTwoLen++;
iPlayerOneLen=0;
iColumn++;
if(iPlayerOneLen>
=5)
return1;
if(iPlayerTwoLen>
return2;
iColumn=0;
//检查棋盘竖向是否存在五子连珠
CheckColumn()
iColumn++)
while(iRow<
iRow++;
iRow=0;
//检查棋盘左上方(包括对角线)是否存在五子连珠
CheckTopLeft()
intiTempRow=0;
for(iRow=4;
iTempRow=iRow;
while(iTempRow>
=0)
(1)==acBoard[iTempRow][iColumn])
(2)==acBoard[iTempRow][iColumn])
iTempRow--;
//检查棋盘右上方(包括对角线)是否存在五子连珠
CheckTopRight()
intiTempColumn=0;
WIDE_AND_LONG-4;
iTempColumn=iColumn;
while(iTempColumn<
(1)==acBoard[iRow][iTempColumn])
(2)==acBoard[iRow][iTempColumn])
iTempColumn++;
//检查棋盘左下方(不包括对角线)是否存在五子连珠
CheckDownLeft()
for(iRow=1;
while(iTempRow<
iTempRow++;
//检查棋盘右下方(不包括对角线)是否存在五子连珠
CheckDownRight()
intiColumn=WIDE_AND_LONG-1;
iColumn--;
iColumn=WIDE_AND_LONG-1;
//检查是否平局
CheckDraw()
==acBoard[iRow][iColumn])
//检查是否达到结束的条件(五子连珠或平局)
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)
恭喜玩家<
acPlayerOneName<
>
获胜!
if(2==iJudgeRow||2==iJudgeColumn||2==iJudgeTopLeft||\
2==iJudgeTopRight||2==iJudgeDownLeft||2==iJudgeDownRight)
acPlayerTwoName<
if(0==iJudgeDraw)
平局!
//显示棋盘到控制台
ShowBoard()
system("
cls"
"
if(9<
iRow)
static_cast<
+iRow-10)<
iRow<
for(iColumn