五子棋(C++).doc
《五子棋(C++).doc》由会员分享,可在线阅读,更多相关《五子棋(C++).doc(16页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、#include #include using namespace std;#define WIDE_AND_LONG 20 /棋盘的长和宽#define NAME_LEN 20 /输入姓名的长度typedef class Gobangpublic:int InitPlayerName(char *, char *);int CheckInput(char,int);int CheckIndexInput(char, char, char);int GetPlayerName();int InitBoard();int WriteBoard(char, char, bool);int Begi
2、nOrNot();int CheckRow();int CheckColumn();int CheckTopLeft();int CheckTopRight();int CheckDownLeft();int CheckDownRight();int CheckDraw();int CheckFinish();int Chess();int ShowBoard();private:char acBoardWIDE_AND_LONGWIDE_AND_LONG;char acPlayerOneNameNAME_LEN;char acPlayerTwoNameNAME_LEN;GOBANG;/初始化
3、姓名的缺省值int GOBANG:InitPlayerName(char *pPlayerOne,char *pPlayerTwo)strcpy(acPlayerOneName,pPlayerOne);strcpy(acPlayerTwoName,pPlayerTwo);return 0;/检查输入姓名时是否含非法字符空格和Tab键int GOBANG:CheckInput(char ch, int iNameLen)if( = ch | t = ch)cout含有非法字符! NAME_LEN - 1)cout输入超出限定长度!= 0 & cRow = 9)iRow = static_cast
4、(cRow - 0);else if(cRow = A & cRow = static_cast(A + WIDE_AND_LONG - 10 - 1)iRow = static_cast(cRow - A) + 10);elseiRow = static_cast(cRow - a) + 10);if(cColumn = 0 & cColumn = 9)iColumn = static_cast(cColumn - 0);else if(cColumn = A & cColumn = static_cast(A + WIDE_AND_LONG - 10 - 1)iColumn = stati
5、c_cast(cColumn - A) + 10);elseiColumn = static_cast(cColumn - a) + 10);if(+ != acBoardiRowiColumn)cout此处已有棋子!endl;return -1;if(!bJudge)acBoardiRowiColumn = static_cast(1);elseacBoardiRowiColumn = static_cast(2);return 0;/检查坐标输入是否合法int GOBANG:CheckIndexInput(char cRow,char cSeparator,char cColumn)if(
6、!cRow | !cSeparator | !cColumn)return -1;if(!(cRow = 0 & cRow = A & cRow = static_cast(A + WIDE_AND_LONG - 10 - 1) | (cRow = a & cRow = static_cast(a + WIDE_AND_LONG - 10 - 1)return -1;if( != cSeparator & t != cSeparator & , != cSeparator)return -1;if(!(cColumn = 0 & cColumn = A & cColumn = static_c
7、ast(A + WIDE_AND_LONG - 10 - 1) | (cColumn = a & cColumn = static_cast(a + WIDE_AND_LONG - 10 - 1)return -1;return 0;/获取玩家的昵称int GOBANG:GetPlayerName()fflush(stdin);char cTemp = 0;int iNameLen = 0;cout是否自己定义昵称?是(Y),否(任意键):;cTemp = getch();coutendl;if(y != cTemp) & (Y != cTemp)return 0;fflush(stdin);
8、memset(acPlayerOneName,0,sizeof(acPlayerOneName);memset(acPlayerTwoName,0,sizeof(acPlayerTwoName);cout请玩家一输入昵称:;while(n != (cTemp = getchar()if(-1 = CheckInput(cTemp,iNameLen)fflush(stdin);cout请玩家一输入昵称:;continue;acPlayerOneNameiNameLen = cTemp;iNameLen+;if(0 = iNameLen)strcpy(acPlayerOneName,玩家一);iN
9、ameLen = 0;fflush(stdin);cout请玩家二输入昵称:;while(n != (cTemp = getchar()if(-1 = CheckInput(cTemp,iNameLen)fflush(stdin);cout请玩家二输入昵称:;continue;acPlayerTwoNameiNameLen = cTemp;iNameLen+;if(0 = iNameLen)strcpy(acPlayerTwoName,玩家一);return 0;/初始化棋盘int GOBANG:InitBoard()int iRow = 0;int iColumn = 0;for(iRow
10、= 0; iRow WIDE_AND_LONG; iRow+)for(iColumn = 0; iColumn WIDE_AND_LONG; iColumn+)acBoardiRowiColumn = +;return 0;/检查棋盘横向是否存在五子连珠int GOBANG:CheckRow()int iRow = 0;int iColumn = 0;int iPlayerOneLen = 0;int iPlayerTwoLen = 0;for(iRow = 0; iRow WIDE_AND_LONG; iRow +)while(iColumn WIDE_AND_LONG)if(static_
11、cast(1) = acBoardiRowiColumn)iPlayerOneLen +;iPlayerTwoLen = 0;else if(static_cast(2) = acBoardiRowiColumn)iPlayerTwoLen +;iPlayerOneLen = 0;elseiPlayerTwoLen = 0;iPlayerOneLen = 0;iColumn +;if(iPlayerOneLen = 5)return 1;if(iPlayerTwoLen = 5)return 2;iPlayerOneLen = 0;iPlayerTwoLen = 0;iColumn = 0;r
12、eturn 0;/检查棋盘竖向是否存在五子连珠int GOBANG:CheckColumn()int iRow = 0;int iColumn = 0;int iPlayerOneLen = 0;int iPlayerTwoLen = 0;for(iColumn = 0; iColumn WIDE_AND_LONG; iColumn +)while(iRow WIDE_AND_LONG)if(static_cast(1) = acBoardiRowiColumn)iPlayerOneLen +;iPlayerTwoLen = 0;else if(static_cast(2) = acBoard
13、iRowiColumn)iPlayerTwoLen +;iPlayerOneLen = 0;elseiPlayerTwoLen = 0;iPlayerOneLen = 0;iRow +;if(iPlayerOneLen = 5)return 1;if(iPlayerTwoLen = 5)return 2;iPlayerOneLen = 0;iPlayerTwoLen = 0;iRow = 0;return 0;/检查棋盘左上方(包括对角线)是否存在五子连珠int GOBANG:CheckTopLeft()int iRow = 0;int iTempRow = 0;int iColumn = 0
14、;int iPlayerOneLen = 0;int iPlayerTwoLen = 0;for(iRow = 4; iRow = 0)if(static_cast(1) = acBoardiTempRowiColumn)iPlayerOneLen +;iPlayerTwoLen = 0;if(iPlayerOneLen = 5)return 1;else if(static_cast(2) = acBoardiTempRowiColumn)iPlayerTwoLen +;iPlayerOneLen = 0;if(iPlayerTwoLen = 5)return 2;elseiPlayerTw
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 五子棋
限制150内