五子棋源代码(C++),完美版.docx

上传人(卖家):四川天地人教育 文档编号:1500677 上传时间:2021-06-21 格式:DOCX 页数:15 大小:12.53KB
下载 相关 举报
五子棋源代码(C++),完美版.docx_第1页
第1页 / 共15页
五子棋源代码(C++),完美版.docx_第2页
第2页 / 共15页
五子棋源代码(C++),完美版.docx_第3页
第3页 / 共15页
五子棋源代码(C++),完美版.docx_第4页
第4页 / 共15页
亲,该文档总共15页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

1、#in elude #in elude using n amespace std; #defi ne WIDE_AND_LONG 20 / 棋盘的长和宽 #defi ne NAME_LEN20 /输入姓名的长度 typedef class Goba ng public: int In itPlayerName(char *, char *); int Check In put(char,i nt); int Check In dex In put(char, char, char); int GetPlayerName(); int In itBoard(); int WriteBoard(c

2、har, char, bool); int Begi nOr Not(); int CheckRow(); int CheckColum n(); int CheckTopLeft(); int CheckTopRight(); int CheckDow nLeft(); int CheckDow nRight(); int CheckDraw(); int CheckFi ni sh(); int Chess(); int ShowBoard(); private: char acBoardWIDE_AND_LONGWIDE_AND_LONG; char acPlayerO neNameNA

3、ME_LEN; char acPlayerTwoNameNAME_LEN; GOBANG; /初始化姓名的缺省值 int GOBANG:I ni tPlayerName(char *pPlayerO ne,char *pPlayerTwo) strcpy(acPlayerO neName,pPlayerO ne); strcpy(acPlayerTwoName,pPlayerTwo); return 0; 检查输入姓名时是否含非法字符空格和Tab 键 int GOBANG:CheckI nput(char ch, i nt iNameLe n) if( = ch | t = ch) cout含

4、有非法字符! NAME_LEN - 1) cout输入超出限定长度!= 0 else if(cRow = A else iRow = static_cast(cRow - a) + 10); if(cColu mn = 0 else if(cColu mn = A else iColu mn = static_cast(cCol umn - a) + 10); if(+ != acBoardiRowiColum n) cout此处已有棋子!endl; return -1; if(!bJudge) acBoardiRowiColu mn = static_cast(1); else acBoar

5、diRowiColu mn = static_cast(2); return 0; /检查坐标输入是否合法 int GOBANG:Checkl ndexl nput(char cRow,char cSeparator,char cColu mn) if(!cRow | !cSeparator | !cColu mn) return -1; if(!(cRow = O if( != cSeparator if(!(cColu mn = O return 0; 获取玩家的昵称 int GOBANG:GetPlayerName() fflush(stdi n); char cTemp = 0; in

6、t iNameLe n = 0; cout是否自己定义昵称?是(Y),否(任意键):; cTemp = getch(); coute ndl; if(y != cTemp) fflush(stdi n); memset(acPlayer On eName,0,sizeof(acPlayer On eName); memset(acPlayerTwoName,0,sizeof(acPlayerTwoName); cout请玩家一输入昵称:; while(n != (cTemp = getchar() if(-1 = Check In put(cTemp,iNameLe n) fflush(std

7、i n); cout请玩家一输入昵称:; con ti nue; acPlayerO neNameiNameLe n = cTemp; iNameLe n+; if(0 = iNameLe n) strcpy(acPlayerOneName,玩家一); iNameLe n = 0; fflush(stdi n); cout请玩家二输入昵称:; while(n != (cTemp = getchar() if(-1 = Check In put(cTemp,iNameLe n) fflush(stdi n); cout请玩家二输入昵称:; con ti nue; acPlayerTwoNamei

8、NameLe n = cTemp; iNameLe n+; if(0 = iNameLe n) strcpy(acPlayerTwoName,玩家一); return 0; /初始化棋盘 int GOBANG:I ni tBoard() int iRow = 0; int iColu mn = 0; for(iRow = 0; iRow WIDE_AND_LONG; iRow+) for(iColu mn = 0; iColu mn WIDE_AND_LONG; iColum n+) acBoardiRowiColu mn = +; return 0; /检查棋盘横向是否存在五子连珠 int

9、GOBANG:CheckRow() int iRow = 0; int iColu mn = 0; int iPlayerO neLe n = 0; int iPlayerTwoLe n = 0; for(iRow = 0; iRow WIDE_AND_LONG; iRow +) while(iColu mn WIDE_AND_LONG) if(static_cast(1) = acBoardiRowiColu mn) iPlayerO neLe n +; iPlayerTwoLe n = 0; else if(static_cast(2) = acBoardiRowiColu mn) iPl

10、ayerTwoLe n +; iPlayerO neLe n = 0; else iPlayerTwoLe n = 0; iPlayerO neLe n = 0; iColu mn +; if(iPlayerO neLe n = 5) return 1; if(iPlayerTwoLe n = 5) return 2; iPlayerO neLe n = 0; iPlayerTwoLe n = 0; iColu mn = 0; return 0; /检查棋盘竖向是否存在五子连珠 int GOBANG:CheckColum n() int iRow = 0; int iColu mn = 0;

11、int iPlayerO neLe n = 0; int iPlayerTwoLe n = 0; for(iColu mn = 0; iColu mn WIDE_AND_LONG; iColu mn +) while(iRow WIDE_AND_LONG) if(static_cast(1) = acBoardiRowiColu mn) iPlayerO neLe n +; iPlayerTwoLe n = 0; else if(static_cast(2) = acBoardiRowiColu mn) iPlayerTwoLe n +; iPlayerO neLe n = 0; else i

12、PlayerTwoLe n = 0; iPlayerO neLe n = 0; iRow +; if(iPlayerO neLe n = 5) return 1; if(iPlayerTwoLe n = 5) return 2; iPlayerO neLe n = 0; iPlayerTwoLe n = 0; iRow = 0; return 0; 检查棋盘左上方(包括对角线)是否存在五子连珠 int GOBANG:CheckTopLeft() int iRow = 0; int iTempRow = 0; int iColu mn = 0; int iPlayerO neLe n = 0;

13、int iPlayerTwoLe n = 0; for(iRow = 4; iRow = 0) if(static_cast(1) = acBoardiTempRowiColu mn) iPlayerO neLe n +; iPlayerTwoLe n = 0; if(iPlayerO neLe n = 5) return 1; else if(static_cast(2) = acBoardiTempRowiColu mn) iPlayerTwoLe n +; iPlayerO neLe n = 0; if(iPlayerTwoLe n = 5) return 2; else iPlayer

14、TwoLe n = 0; iPlayerO neLe n = 0; iTempRow -; iColu mn +; iPlayerO neLe n = 0; iPlayerTwoLe n = 0; iColu mn = 0; return 0; 检查棋盘右上方(包括对角线)是否存在五子连珠 int GOBANG:CheckTopRight() int iRow = 0; int iColu mn = 0; int iTempColu mn = 0; int iPlayerO neLe n = 0; int iPlayerTwoLe n = 0; for(iColu mn = 0; iColu

15、mn WIDE_AND_LONG - 4; iColu mn +) iTempColu mn = iColu mn; while(iTempColu mn WIDE_AND_LONG) if(static_cast(1) = acBoardiRowiTempColu mn) iPlayerO neLe n +; iPlayerTwoLe n = 0; if(iPlayerO neLe n = 5) return 1; else if(static_cast(2) = acBoardiRowiTempColu mn) iPlayerTwoLe n +; iPlayerO neLe n = 0;

16、if(iPlayerTwoLe n = 5) return 2; else iPlayerTwoLe n = 0; iPlayerO neLe n = 0; iRow +; iTempColu mn +; iPlayerO neLe n = 0; iPlayerTwoLe n = 0; iRow = 0; return 0; 检查棋盘左下方(不包括对角线)是否存在五子连珠 int GOBANG:CheckDow nLeft() int iRow = 0; int iTempRow = 0; int iColu mn = 0; int iPlayerO neLe n = 0; int iPlay

17、erTwoLe n = 0; for(iRow = 1; iRow WIDE_AND_LONG - 4; iRow +) iTempRow = iRow; while(iTempRow WIDE_AND_LONG) if(static_cast(1) = acBoardiTempRowiColu mn) iPlayerO neLe n +; iPlayerTwoLe n = 0; if(iPlayerO neLe n = 5) return 1; else if(static_cast(2) = acBoardiTempRowiColu mn) iPlayerTwoLe n +; iPlaye

18、rO neLe n = 0; if(iPlayerTwoLe n = 5) return 2; else iPlayerTwoLe n = 0; iPlayerO neLe n = 0; iTempRow +; iColu mn +; iPlayerO neLe n = 0; iPlayerTwoLe n = 0; iColu mn = 0; return 0; 检查棋盘右下方(不包括对角线)是否存在五子连珠 int GOBANG:CheckDow nRight() int iRow = 0; int iTempRow = 0; int iColu mn = WIDE_AND_LONG - 1

19、; int iPlayerO neLe n = 0; int iPlayerTwoLe n = 0; for(iRow = 1; iRow WIDE_AND_LONG - 4; iRow +) iTempRow = iRow; while(iTempRow WIDE_AND_LONG) if(static_cast(1) = acBoardiTempRowiColu mn) iPlayerO neLe n +; iPlayerTwoLe n = 0; if(iPlayerO neLe n = 5) return 1; else if(static_cast(2) = acBoardiTempR

20、owiColu mn) iPlayerTwoLe n +; iPlayerO neLe n = 0; if(iPlayerTwoLe n = 5) return 2; else iPlayerTwoLe n = 0; iPlayerO neLe n = 0; iTempRow +; iColu mn -; iPlayerO neLe n = 0; iPlayerTwoLe n = 0; iColu mn = WIDE_AND_LONG - 1; return 0; /检查是否平局 int GOBANG:CheckDraw() int iRow = 0; int iColu mn = 0; fo

21、r(iRow = 0; iRow WIDE_AND_LONG; iRow+) for(iColu mn = 0; iColu mn WIDE_AND_LONG; iColum n+) if(+ = acBoardiRowiColu mn) return 1; return 0; 检查是否达到结束的条件(五子连珠或平局) int GOBANG:CheckFi ni sh() int iJudgeRow = 0; int iJudgeColu mn = 0; int iJudgeTopLeft = 0; int iJudgeTopRight = 0; int iJudgeDow nLeft = 0

22、; int iJudgeDow nRight = 0; int iJudgeDraw = 0; iJudgeRow = CheckRow(); iJudgeColu mn = CheckColu mn(); iJudgeTopLeft = CheckTopLeft(); iJudgeTopRight = CheckTopRight(); iJudgeDow nLeft = CheckDow nLeft(); iJudgeDow nRight = CheckDow nRight(); iJudgeDraw = CheckDraw(); if(1 = iJudgeRow | 1 = iJudgeC

23、olu mn | 1 = iJudgeTopLeft | 1 = iJudgeTopRight | 1 = iJudgeDow nLeft | 1 = iJudgeDow nRight) cout恭喜玩家 acPlayerOneName 获胜!endl; return 1; if(2 = iJudgeRow | 2 = iJudgeColu mn | 2 = iJudgeTopLeft | 2 = iJudgeTopRight | 2 = iJudgeDow nLeft | 2 = iJudgeDow nRight) cout恭喜玩家 acPlayerTwoName 获胜!endl; retu

24、rn 1; if(0 = iJudgeDraw) cout平局!endl; return 1; return 0; 显示棋盘到控制台 int GOBANG:ShowBoard() int iRow = 0; int iColu mn = 0; system(cls); cout; for(iRow = 0; iRow WIDE_AND_LONG; iRow+) if(9 iRow) coutstatic_cast(A + iRow - 10); else coutiRow; coute ndl; for(iRow = 0; iRow WIDE_AND_LONG; iRow+) if(9 iRo

25、w) coutstatic_cast(A + iRow - 10); else coutiRow; for(iColu mn = 0; iColu mn WIDE_AND_LONG; iColum n+) coutacBoardiRowiColu mn ; coute ndl; return 0; 开始下棋 int GOBANG:Chess() bool bJudge = false; while(1) char cRow = 0; char cSeparator = 0; char cCol umn = 0; char cTemp = 0; int iLe n =1; fflush(stdi

26、 n); if(!bJudge) cout请acPlayerOneName 输入对应的行和列(格式:a,3 或 a 3 或 a 3):; else cout请acPlayerTwoName 输入对应的行和列(格式:a,3 或 a 3 或 a 3):; while(n != (cTemp = getchar() if(iLe n 3) cout输入有误!endl; fflush(stdi n); if(!bJudge) cout请acPlayerOneName 输入对应的行和列(格式:a,3 或 a 3 或 a 3):; else cout请acPlayerTwoName 输入对应的行和列(格式

27、:a,3 或 a 3 或 a 3):; iLe n = 1; con ti nue; if(1 = iLe n) cRow = cTemp; else if(2 = iLe n) cSeparator = cTemp; else cColu mn = cTemp; iLe n+; if(-1 = Check In dex In put(cRow,cSeparator,cCol umn) cout输入有误!endl; con ti nue; if(-1 = WriteBoard(cRow,cColum n, bJudge) con ti nue; ShowBoard(); if(1 = Chec

28、kFi ni sh() Beg in OrNot(); bJudge = !bJudge; return 0; /判断游戏结束后玩家是否选择继续还是退出 int GOBANG:Begi nOrNot() char cTemp = 0; cout是否继续?是(Y),退出(任意键):; fflush(stdi n); cTemp = getch(); if(y = cTemp | Y = cTemp) In itBoard(); ShowBoard(); Chess(); else exit(0); /程序的出口 return 0; int mai n() GOBANG goba ng; memset( gobang.lnitPlayerName(” 玩家一,玩家二); goba ng.GetPlayerName(); goba ng.ln itBoard(); goba ng.ShowBoard(); goba ng.Chess(); return 0;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 办公、行业 > 待归类文档
版权提示 | 免责声明

1,本文(五子棋源代码(C++),完美版.docx)为本站会员(四川天地人教育)主动上传,163文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。
2,用户下载本文档,所消耗的文币(积分)将全额增加到上传者的账号。
3, 若此文所含内容侵犯了您的版权或隐私,请立即通知163文库(发送邮件至3464097650@qq.com或直接QQ联系客服),我们立即给予删除!


侵权处理QQ:3464097650--上传资料QQ:3464097650

【声明】本站为“文档C2C交易模式”,即用户上传的文档直接卖给(下载)用户,本站只是网络空间服务平台,本站所有原创文档下载所得归上传人所有,如您发现上传作品侵犯了您的版权,请立刻联系我们并提供证据,我们将在3个工作日内予以改正。


163文库-Www.163Wenku.Com |网站地图|