五子棋AI 任务1:实现棋盘类

绪论

本篇将引导读者如何构建一个五子棋棋盘类,并且在结尾给出了已经写好关键接口的类定义,使得读者将注意力聚焦在功能的实现上。

下载代码文件

任务要求详解


对于需要填写的部分,用#define语句定义宏进行了替代,以保证通过编译,在编写代码时删掉即可。

#define QUEST_BOOL true
#define QUEST_VAL Player::NONE
#define QUEST_CODE int q = 0;

1.行动函数


该函数从外部接受落子位置的行row、列col信息,并执行以下操作:

  1. 判断落子位置是否合法:主要考虑是否出界,该位置是否为空。【任务点】
  2. 更新棋盘数据
  3. 判断游戏是否结束:参考类成员变量,思考当游戏结束时应当更改哪些数值【任务点】
  4. 更换落子玩家:实现功能【任务点】
/**
* 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.线检查函数


直线的解析式可以表示为

x0+kΔx=0

其中x0表示之险的起点,Δx为直线的方向向量,k为实数。

在线检查函数checkLine中,将起点坐标和方向向量分别作为参数传入函数。

你需要实现的功能是:分别在正、负两个方向上查找,查找规则如下:

  1. 每个方向至多查找4个格子,
  2. 查找到己方棋子时计数器+1,
  3. 查找到敌方棋子或空格时停止。
/**
* 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;
}

注意实项


在实现函数的过程中,可以多从流程图角度出发思考,为什么这么设计类,以及这些函数分别应当实现什么功能。
description

posted @   SXWisON  阅读(111)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示