




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
课程设计报告 -三子连珠理学院应用物理系 1 郝小龙 课题要求:1.将程序改写成类 。 2.将棋盘扩充成4*4。 3.统计输赢结果并打印输出。进一步改进程序:将if else if 语句改为 for 语句 程序的源代码: /*第2题TICTACTOE游戏-源代码及关键源代码注解如下:*/#include #include void PrintBoard(); / game boardvoid PrintInfo(); / some info about the rules of the game is printedvoid PrintStats(int, int, int); / prints the stats of the matchvoid IfError(int&, int&); / When invalid input is entered, prints the errorvoid ChoiceOfChar(char&); / Asks the users if they want to play againvoid PromptTurnO(int&, int&); / Prompts the first user for datavoid PromptTurnX(int&, int&); / Second user has to input coordinateschar board33; / array definitionint main() int ROW; / elements of 2 dimensional array! int COLUMN; int FirstPlayer; / number of winning games for first player int SecondPlayer; int Draws; / number of draws in a game FirstPlayer = 0; SecondPlayer = 0; Draws = 0; char choice; choice = Y; / Initial value is Y in order to initialize the main loop PrintInfo(); while(choice = Y) / In case the players want to play again / this is the main loop for (ROW = 0; ROW 3; ROW+) / these forloops are used to process the array for (COLUMN = 0; COLUMN 3; COLUMN+) / by row and column boardROWCOLUMN= ; / blank characters as initial values int x; / (x,y) coordinates entered by the users int y; int SpotsOnBoard; / this is the sum of spots available on the game board SpotsOnBoard = 0; while ( SpotsOnBoard = 9) / because there is only 9 characters available on the board if (SpotsOnBoard = 0) PrintBoard(); / This function draws initialy the blank game board / so it is easier for the user to pick the x and y values PromptTurnO(x, y); IfError(x, y); / If the users input is invalid, this function prints / the error message and prompts for correction boardx - 1y - 1= O; / actually the array starts from 0, NOT from 1 SpotsOnBoard+; / (as entered by the user) / so the program have to fix the input PrintBoard(); / If 3 coordintates are Os in one of 8 possible /winning combinations, then O wins if (board00= O & board11= O & board22= O) cout O player wins the game endl; FirstPlayer+; break; else if (board20= O & board11= O & board02= O) cout O player wins the game endl; FirstPlayer+; break; else if (board00= O & board10= O & board20 = O) cout O player wins the game endl; FirstPlayer+; break; else if (board01= O & board11= O & board21= O) cout O player wins the game endl; FirstPlayer+; break; else if (board02= O & board12= O & board22= O) cout O player wins the game endl; FirstPlayer+; break; else if (board00= O & board01= O & board02= O) cout O player wins the game endl; FirstPlayer+; break; else if (board10= O & board11= O & board12= O) cout O player wins the game endl; FirstPlayer+; break; else if (board20= O & board21= O & board22= O) cout O player wins the game endl; FirstPlayer+; / counts the number of won games break; / break statement is used to make sure that only one possibility / will be processed else if (SpotsOnBoard = 9) cout Draw! endl; Draws+; break; / Without this brake statement it was not working propertly PromptTurnX(x, y); IfError(x, y); boardx - 1y - 1= X; SpotsOnBoard+; PrintBoard(); if (board00= X & board11= X & board22= X) cout X player is victorious endl; SecondPlayer+; else if (board20= X & board11= X & board02= X) cout X player is victorious endl; SecondPlayer+; else if (board00= X & board10= X & board20= X) cout X player is victorious endl; SecondPlayer+; else if (board01= X & board11= X & board21= X) cout X player is victorious endl; SecondPlayer+; else if (board02= X & board12= X & board22= X) cout X player is victorious endl; SecondPlayer+; else if (board00= X & board01= X & board02= X) cout X player is victorious endl; SecondPlayer+; else if (board10= X & board11= X & board12= X) cout X player is victorious endl; SecondPlayer+; else if (board20= X & board21= X & board22= X) cout X player is victorious endl; SecondPlayer+; ChoiceOfChar(choice); PrintStats(FirstPlayer, SecondPlayer, Draws); system(PAUSE); return 0;void PrintBoard()/ Post : The Game board is printed to help with choosing the coordintates/ Either X or O will be printed on the board./ Board is updated while the game progresses cout endl; cout 1 2 3 endl; cout 1 board00 | board01 | board02 endl; cout -|-|- endl; cout 2 board10 | board11 | board12 endl; cout -|-|- endl; cout 3 board20 | board21 | board22 endl; cout endl; cout endl;void PrintInfo()/ Post : Some info about the rules of the game is printed cout First player is represented by O, second by X. endl; cout Enter the coordinates for Rows and Columns endl; cout Strike enter when Done. endl; cout endl;void PrintStats(int FirstPlayer, int SecondPlayer, int Draws)/ Pre : Players have to decide to exit the game./ Post : Returns the number of won games and draws. cout The O player have won FirstPlayer times endl; cout The X player have won SecondPlayer times endl; cout There was Draws draws endl; cout Thanks for using my program 3 | x 3 | y 1 |(O= boardx - 1y - 1 |X= boardx - 1y - 1) cout This coordinate is not allowed, try again endl; cout x; cout y; void ChoiceOfChar(char& choice)/ Pre : Entire program has been executed/ Post : User is given a chance to play again cout endl; cout Press CAPITAL Y if you want to play again. endl; cout Otherwise, press any other letter key to exit the loop. choice;void PromptTurnO(int& x, int& y)/ Post : First player enters the coordinates of a spot on a game board cout Turn of the first player (O), enter the coordinates endl; cout x; cout y;void PromptTurnX(int& x, int& y)/ Post : Second player enters the coordinates of a spot on a game bo
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- ERP系统开发合作协议
- 感受家乡变化的变迁作文(7篇)
- 产品设计创意构思与呈现工具
- 小溪旁的一天的自然景观描述作文(14篇)
- 消防知识安全网络答题题库及答案解析
- 会议记录与跟踪执行流程工具
- 企业级智能水务系统开发与维护协议
- 企业合同履行信用担保承诺函9篇
- 团队任务分配与时间管理表明确工作重点
- 企业办公文档标准化模板
- 门窗淋水试验施工方案
- 遥感原理与应用 课件 第7、8章 定量遥感、遥感技术的应用
- 干部履历表模板
- 患者隐私保护培训课件
- 《SolidWorks 2024项目教程》高职全套教学课件
- 儿童肥胖的危害和预防-培训课件
- 2025版宝鸡市房地产评估服务合同范本(含保密条款)2篇
- 《集成电路技术导论》课件
- 医疗机构药品管理法
- 弹幕游戏主播培训
- DB51∕T 990-2020 小型泵站设计规程
评论
0/150
提交评论