书签 分享 收藏 举报 版权申诉 / 15
上传文档赚钱

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

  • 上传人(卖家):四川天地人教育
  • 文档编号:1500677
  • 上传时间:2021-06-21
  • 格式:DOCX
  • 页数:15
  • 大小:12.53KB
  • 【下载声明】
    1. 本站全部试题类文档,若标题没写含答案,则无答案;标题注明含答案的文档,主观题也可能无答案。请谨慎下单,一旦售出,不予退换。
    2. 本站全部PPT文档均不含视频和音频,PPT中出现的音频或视频标识(或文字)仅表示流程,实际无音频或视频文件。请谨慎下单,一旦售出,不予退换。
    3. 本页资料《五子棋源代码(C++),完美版.docx》由用户(四川天地人教育)主动上传,其收益全归该用户。163文库仅提供信息存储空间,仅对该用户上传内容的表现方式做保护处理,对上传内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知163文库(点击联系客服),我们立即给予删除!
    4. 请根据预览情况,自愿下载本文。本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
    5. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007及以上版本和PDF阅读器,压缩文件请下载最新的WinRAR软件解压。
    配套讲稿:

    如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。

    特殊限制:

    部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。

    关 键  词:
    五子棋 源代码 完美
    资源描述:

    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;

    展开阅读全文
    提示  163文库所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。
    关于本文
    本文标题:五子棋源代码(C++),完美版.docx
    链接地址:https://www.163wenku.com/p-1500677.html

    Copyright@ 2017-2037 Www.163WenKu.Com  网站版权所有  |  资源地图   
    IPC备案号:蜀ICP备2021032737号  | 川公网安备 51099002000191号


    侵权投诉QQ:3464097650  资料上传QQ:3464097650
       


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

    163文库