五子棋AI 任务1:实现棋盘类
绪论
本篇将引导读者如何构建一个五子棋棋盘类,并且在结尾给出了已经写好关键接口的类定义,使得读者将注意力聚焦在功能的实现上。
任务要求详解
对于需要填写的部分,用#define语句定义宏进行了替代,以保证通过编译,在编写代码时删掉即可。
#define QUEST_BOOL true #define QUEST_VAL Player::NONE #define QUEST_CODE int q = 0;
1.行动函数
该函数从外部接受落子位置的行row
、列col
信息,并执行以下操作:
- 判断落子位置是否合法:主要考虑是否出界,该位置是否为空。【任务点】
- 更新棋盘数据
- 判断游戏是否结束:参考类成员变量,思考当游戏结束时应当更改哪些数值【任务点】
- 更换落子玩家:实现功能【任务点】
/** * Makes a move on the chessboard. * @param row The row index of the move. * @param col The column index of the move. * @return True if the move is valid and made successfully, false otherwise. */ bool ChessBoard::makeMove(int row, int col) { // Determine if the move is legal. if (QUEST_BOOL) { return false; // Invalid move } board[row][col] = currentPlayer; // check if the game is ended if (checkGameEnd(row, col)) { QUEST_CODE } currentPlayer = QUEST_VAL; // Switch players return true; }
2.检查游戏是否结束
要求:调用四次,分别判断竖直、水平、主对角线和副对角线四个方向。
难点:根据线检查函数
checkLine
的定义和功能,进行四次调用,并传入合适的参数
/** * Checks if the game has ended after making a move at the specified position. * @param row The row index of the move. * @param col The column index of the move. * @return True if the game has ended, false otherwise. */ bool ChessBoard::checkGameEnd(int row, int col) { // Check horizontal, vertical, and both diagonals for a winning line if (QUEST_BOOL) { return true; } return false; }
2.检查游戏是否结束
要求:调用四次,分别判断竖直、水平、主对角线和副对角线四个方向。
难点:根据线检查函数
checkLine
的定义和功能,进行四次调用,并传入合适的参数
/** * Checks if the game has ended after making a move at the specified position. * @param row The row index of the move. * @param col The column index of the move. * @return True if the game has ended, false otherwise. */ bool ChessBoard::checkGameEnd(int row, int col) { // Check horizontal, vertical, and both diagonals for a winning line if (QUEST_BOOL) { return true; } return false; }
3.线检查函数
直线的解析式可以表示为
其中
在线检查函数checkLine
中,将起点坐标和方向向量分别作为参数传入函数。
你需要实现的功能是:分别在正、负两个方向上查找,查找规则如下:
- 每个方向至多查找4个格子,
- 查找到己方棋子时计数器+1,
- 查找到敌方棋子或空格时停止。
/** * Checks if there is a winning line starting from a specific position in a given direction. * @param startRow The row index of the starting position. * @param startCol The column index of the starting position. * @param deltaRow The vertical direction of the line (positive for down, negative for up). * @param deltaCol The horizontal direction of the line (positive for right, negative for left). * @return True if there is a winning line in the specified direction, false otherwise. */ bool ChessBoard::checkLine(int startRow, int startCol, int deltaRow, int deltaCol) { Player player = board[startRow][startCol]; int count = 1; for (int i = 1; i < 5; ++i) { QUEST_CODE } for (int i = 1; i < 5; ++i) { QUEST_CODE } return count >= 5; // A winning line must have at least 5 consecutive pieces of the same type. }
4.重置棋盘
参考类的构造函数ChessBoard(int size)
,完善对其它成员的重置。
/** * Reset game and chessboard * @return True if reset successfully, false otherwise. */ bool ChessBoard::reset() { // resset chessBoard for (auto& row : board) { for (auto& cell : row) { cell = Player::NONE; } } // reset other segment QUEST_CODE return true; }
注意实项
在实现函数的过程中,可以多从流程图角度出发思考,为什么这么设计类,以及这些函数分别应当实现什么功能。
本文来自博客园,作者:SXWisON,转载请注明原文链接:https://www.cnblogs.com/SXWisON/p/18401861
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了