mirror of
https://github.com/LHY0125/Gobang-Game.git
synced 2026-05-10 02:19:46 +08:00
Add files via upload
This commit is contained in:
@@ -1,146 +1,159 @@
|
|||||||
# ? 五子棋人机对战AI
|
# ? 五子棋人机对战AI
|
||||||
|
|
||||||

|

|
||||||

|

|
||||||

|

|
||||||
|
|
||||||
## 目录
|
## 目录
|
||||||
- [? 五子棋人机对战AI](#-五子棋人机对战ai)
|
- [? 五子棋人机对战AI](#-五子棋人机对战ai)
|
||||||
- [目录](#目录)
|
- [目录](#目录)
|
||||||
- [项目简介](#项目简介)
|
- [项目简介](#项目简介)
|
||||||
- [? 功能特性](#-功能特性)
|
- [功能特性](#功能特性)
|
||||||
- [? 快速开始](#-快速开始)
|
- [快速开始](#快速开始)
|
||||||
- [编译程序](#编译程序)
|
- [编译程序](#编译程序)
|
||||||
- [运行游戏](#运行游戏)
|
- [运行游戏](#运行游戏)
|
||||||
- [? 游戏玩法](#-游戏玩法)
|
- [游戏玩法](#游戏玩法)
|
||||||
- [? 开发环境](#-开发环境)
|
- [开发环境](#开发环境)
|
||||||
- [?? 常见问题](#?-常见问题)
|
- [常见问题](#常见问题)
|
||||||
- [权限问题](#权限问题)
|
- [权限问题](#权限问题)
|
||||||
- [中文显示问题](#中文显示问题)
|
- [中文显示问题](#中文显示问题)
|
||||||
- [?? 技术实现](#?-技术实现)
|
- [技术实现](#技术实现)
|
||||||
- [核心决策算法](#核心决策算法)
|
- [核心决策算法](#核心决策算法)
|
||||||
- [启发式评估函数](#启发式评估函数)
|
- [启发式评估函数](#启发式评估函数)
|
||||||
- [? 代码结构](#-代码结构)
|
- [代码结构](#代码结构)
|
||||||
- [? 许可证](#-许可证)
|
- [许可证](#许可证)
|
||||||
- [? 反馈与贡献](#-反馈与贡献)
|
- [反馈与贡献](#反馈与贡献)
|
||||||
- [? 未来计划](#-未来计划)
|
- [未来计划](#未来计划)
|
||||||
|
|
||||||
## 项目简介
|
## 项目简介
|
||||||
基于C语言实现的五子棋人机对战系统,采用α-β剪枝优化的极小极大算法,支持自定义棋盘大小、游戏复盘和实时评分。
|
基于C语言实现的五子棋人机对战系统,采用α-β剪枝优化的极小极大算法,支持自定义棋盘大小、游戏复盘和实时评分。
|
||||||
|
|
||||||
## ? 功能特性
|
## 功能特性
|
||||||
- ? 人机对战模式
|
- 人机对战模式
|
||||||
- ?? 可调棋盘尺寸(5x5到25x25)
|
- 可调棋盘尺寸(5x5到25x25)
|
||||||
- ? 智能AI决策(1-5级难度)
|
- 智能AI决策(1-5级难度)
|
||||||
- ? 完整游戏复盘功能
|
- 完整游戏复盘功能
|
||||||
- ? 实时对局评分系统
|
- 实时对局评分系统
|
||||||
- ?? 悔棋功能(可撤销上一步)
|
- 悔棋功能(可撤销上一步)
|
||||||
- ?? 清晰的终端界面显示
|
- 清晰的终端界面显示
|
||||||
- ?? 健壮的输入验证(确保所有数字输入都在有效范围内)
|
- 健壮的输入验证(确保所有数字输入都在有效范围内)
|
||||||
- ?? 可选的回合计时器
|
- 可选的回合计时器
|
||||||
- ? 自动游戏记录保存
|
- 自动游戏记录保存
|
||||||
|
|
||||||
## ? 快速开始
|
## 快速开始
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
### 编译程序
|
### 编译程序
|
||||||
```bash
|
```bash
|
||||||
gcc 五子棋.c gobang.c game_mode.c -o output/五子棋.exe
|
gcc 五子棋.c gobang.c game_mode.c ai.c -o gobang.exe
|
||||||
```
|
```
|
||||||
|
|
||||||
### 运行游戏
|
### 运行游戏
|
||||||
```bash
|
```bash
|
||||||
.\output\五子棋.exe
|
.\gobang.exe
|
||||||
```
|
```
|
||||||
|
|
||||||
## ? 游戏玩法
|
## 游戏玩法
|
||||||
1. 启动后设置棋盘大小(默认15x15)
|
1. 启动后设置棋盘大小(默认15x15)
|
||||||
2. 选择AI难度级别(1-5)
|
2. 选择AI难度级别(1-5)
|
||||||
3. 输入坐标进行游戏(格式:行 列)
|
3. 输入坐标进行游戏(格式:行 列)
|
||||||
- 输入R/r可悔棋
|
- 输入R/r可悔棋
|
||||||
4. 游戏结束可查看完整复盘和评分
|
4. 游戏结束可查看完整复盘和评分
|
||||||
|
|
||||||
## ? 开发环境
|
## 开发环境
|
||||||
- 操作系统: Windows (当前版本使用了Windows特有的 `_kbhit()` 和 `Sleep()` 函数,因此仅限Windows平台)
|
- 操作系统: Windows (当前版本使用了Windows特有的 `_kbhit()` 和 `Sleep()` 函数,因此仅限Windows平台)
|
||||||
- 编译器: GCC (MinGW(gcc为14.2.0) on Windows)
|
- 编译器: GCC (MinGW(gcc为14.2.0) on Windows)
|
||||||
- 终端: 支持UTF-8编码的终端
|
- 终端: 支持UTF-8编码的终端
|
||||||
|
|
||||||
> **跨平台兼容性说明:**
|
> **跨平台兼容性说明:**
|
||||||
>
|
>
|
||||||
> 为了未来能在Linux或macOS等其他操作系统上运行,需要将平台特定的代码(如 `_kbhit()`)替换为跨平台的实现,或使用条件编译(`#ifdef _WIN32`)进行隔离。
|
> 为了未来能在Linux或macOS等其他操作系统上运行,需要将平台特定的代码(如 `_kbhit()`)替换为跨平台的实现,或使用条件编译(`#ifdef _WIN32`)进行隔离。
|
||||||
|
|
||||||
## ?? 常见问题
|
## 常见问题
|
||||||
|
|
||||||
### 权限问题
|
### 权限问题
|
||||||
如果程序在保存游戏记录时提示“无法创建文件”或类似错误,通常是由于缺少写入权限。请尝试以下解决方案:
|
如果程序在保存游戏记录时提示“无法创建文件”或类似错误,通常是由于缺少写入权限。请尝试以下解决方案:
|
||||||
|
|
||||||
1. **以管理员身份运行**:右键点击 `五子棋.exe` 或在管理员权限的终端中运行程序。
|
1. **以管理员身份运行**:右键点击 `五子棋.exe` 或在管理员权限的终端中运行程序。
|
||||||
2. **检查目录权限**:确保程序所在目录不是系统保护目录(如 `C:\Program Files`)。建议将项目放在用户目录下(如 `D:\Code`)。
|
2. **检查目录权限**:确保程序所在目录不是系统保护目录(如 `C:\Program Files`)。建议将项目放在用户目录下(如 `D:\Code`)。
|
||||||
3. **手动创建 `records` 目录**:如果 `records` 目录不存在,请在 `output` 目录下手动创建一个。
|
3. **手动创建 `records` 目录**:如果 `records` 目录不存在,请在 `output` 目录下手动创建一个。
|
||||||
|
|
||||||
### 中文显示问题
|
### 中文显示问题
|
||||||
如果在Windows终端中遇到中文字符显示为乱码,是由于终端编码页不匹配导致的。请在运行程序前执行以下命令:
|
如果在Windows终端中遇到中文字符显示为乱码,是由于终端编码页不匹配导致的。请在运行程序前执行以下命令:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
chcp 65001
|
chcp 65001
|
||||||
```
|
```
|
||||||
|
|
||||||
该命令会将当前终端的编码页切换到UTF-8,从而正确显示中文字符。为方便起见,你可以创建一个启动脚本(`.bat` 文件)来自动执行此操作:
|
该命令会将当前终端的编码页切换到UTF-8,从而正确显示中文字符。为方便起见,你可以创建一个启动脚本(`.bat` 文件)来自动执行此操作:
|
||||||
|
|
||||||
**start_game.bat**
|
**start_game.bat**
|
||||||
```batch
|
```batch
|
||||||
@echo off
|
@echo off
|
||||||
chcp 65001
|
chcp 65001
|
||||||
.\output\五子棋.exe
|
.\output\五子棋.exe
|
||||||
```
|
```
|
||||||
|
|
||||||
## ?? 技术实现
|
## 技术实现
|
||||||
|
|
||||||
项目的AI主要基于以下技术实现:
|
项目的AI主要基于以下技术实现:
|
||||||
|
|
||||||
### 核心决策算法
|
### 核心决策算法
|
||||||
|
|
||||||
- **极小极大算法 (Minimax)**:作为决策的基础,模拟对弈双方的每一步,选择对我方最有利的走法。
|
- **极小极大算法 (Minimax)**:作为决策的基础,模拟对弈双方的每一步,选择对我方最有利的走法。
|
||||||
- **α-β 剪枝 (Alpha-Beta Pruning)**:对极小极大算法的關鍵優化,通过剪掉不可能影响最终决策的搜索分支,大幅提升AI的计算效率,使其能够在有限时间内达到更深的搜索深度。
|
- **α-β 剪枝 (Alpha-Beta Pruning)**:对极小极大算法的關鍵優化,通过剪掉不可能影响最终决策的搜索分支,大幅提升AI的计算效率,使其能够在有限时间内达到更深的搜索深度。
|
||||||
- **搜索深度**:AI的思考深度,默认为3层,并可根据难度设置进行调整。搜索深度越深,AI的棋力越强,但计算耗时也越长。
|
- **搜索深度**:AI的思考深度,默认为3层,并可根据难度设置进行调整。搜索深度越深,AI的棋力越强,但计算耗时也越长。
|
||||||
|
|
||||||
### 启发式评估函数
|
### 启发式评估函数
|
||||||
|
|
||||||
为了判断棋局的优劣,AI使用了一套复杂的启发式评估函数,主要包括:
|
为了判断棋局的优劣,AI使用了一套复杂的启发式评估函数,主要包括:
|
||||||
|
|
||||||
- **棋型识别 (Pattern Recognition)**:能够识别并评估多种关键棋型,如“活四”、“冲四”、“活三”、“眠三”等,并为每种棋型赋予不同权重。
|
- **棋型识别 (Pattern Recognition)**:能够识别并评估多种关键棋型,如“活四”、“冲四”、“活三”、“眠三”等,并为每种棋型赋予不同权重。
|
||||||
- **位置权重 (Positional Value)**:棋盘上不同位置的战略价值不同,中心位置通常比边缘位置更有优势。评估函数会为中心区域的落子给予额外加分。
|
- **位置权重 (Positional Value)**:棋盘上不同位置的战略价值不同,中心位置通常比边缘位置更有优势。评估函数会为中心区域的落子给予额外加分。
|
||||||
- **威胁检测 (Threat Detection)**:优先搜索能够直接形成制胜威胁的棋步,如“连五”或“活四”,从而快速响应对手的进攻或抓住制胜机会。
|
- **威胁检测 (Threat Detection)**:优先搜索能够直接形成制胜威胁的棋步,如“连五”或“活四”,从而快速响应对手的进攻或抓住制胜机会。
|
||||||
- **双向延伸评估**:评估一个棋子在特定方向上是否有足够的空间形成连续棋型,避免在被封堵的位置浪费棋步。
|
- **双向延伸评估**:评估一个棋子在特定方向上是否有足够的空间形成连续棋型,避免在被封堵的位置浪费棋步。
|
||||||
|
|
||||||
## ? 代码结构
|
## 代码结构
|
||||||
- `五子棋.c` - 主程序入口,负责初始化和模式选择
|
|
||||||
- `gobang.c` - 核心游戏逻辑,包括棋盘操作、胜负判断、AI算法等
|
|
||||||
- `gobang.h` - `gobang.c` 的头文件,定义核心数据结构和函数原型
|
|
||||||
- `game_mode.c` - 各种游戏模式的实现,如人机对战、双人对战和复盘模式
|
|
||||||
- `game_mode.h` - `game_mode.c` 的头文件,定义游戏模式相关函数原型
|
|
||||||
|
|
||||||
## ? 许可证
|
项目采用模块化设计,各个文件的职责如下:
|
||||||
|
|
||||||
该项目采用 [MIT 许可证](https://opensource.org/licenses/MIT)进行授权。
|
- `五子棋.c` - 主程序入口,负责初始化和模式选择
|
||||||
|
- `gobang.h` - 定义核心数据结构(如棋盘、步骤记录等)和基础函数原型
|
||||||
|
- `gobang.c` - 实现基础的棋盘操作(如落子、显示棋盘等)和胜负判断
|
||||||
|
- `ai.h` - 定义 AI 相关的函数原型
|
||||||
|
- `ai.c` - 实现 AI 的核心算法,包括:
|
||||||
|
- `evaluate_pos`: 启发式评估函数,为每个可能的落子位置打分
|
||||||
|
- `dfs`: 带 α-β 剪枝的极小极大搜索算法
|
||||||
|
- `ai_move`: AI 决策的主函数,实现防守和进攻的两阶段决策
|
||||||
|
- `game_mode.c` - 实现各种游戏模式(如人机对战、复盘等)和用户交互
|
||||||
|
- `game_mode.h` - 定义游戏模式相关的函数原型
|
||||||
|
|
||||||
简单来说,你可以自由地使用、复制、修改、合并、出版、分发、再授权和/或销售本软件的副本,只需在你的项目中包含原始的版权和许可声明即可。
|
这种模块化的结构使得:
|
||||||
|
1. 代码职责划分更加清晰
|
||||||
|
2. AI 算法相关代码被独立出来,便于维护和优化
|
||||||
|
3. 游戏逻辑和 AI 逻辑解耦,有利于后续扩展(如添加新的 AI 算法或游戏模式)
|
||||||
|
|
||||||
## ? 反馈与贡献
|
## 许可证
|
||||||
|
|
||||||
我们非常欢迎任何形式的反馈和贡献!如果你发现了Bug、有功能建议,或者希望改进代码,请随时通过以下方式参与:
|
该项目采用 [MIT 许可证](https://opensource.org/licenses/MIT)进行授权。
|
||||||
|
|
||||||
- **提交 Issue**:如果你遇到问题或有新的想法,请在 [GitHub Issues](https://github.com/LHY0125/Gobang-Game/issues) 页面提交详细描述。
|
简单来说,你可以自由地使用、复制、修改、合并、出版、分发、再授权和/或销售本软件的副本,只需在你的项目中包含原始的版权和许可声明即可。
|
||||||
- **发起 Pull Request**:如果你对代码进行了改进,欢迎提交 Pull Request。请确保你的代码风格与项目保持一致,并附上清晰的改动说明。
|
|
||||||
|
|
||||||
你的每一次贡献都将使这个项目变得更好!
|
## 反馈与贡献
|
||||||
|
|
||||||
## ? 未来计划
|
我们非常欢迎任何形式的反馈和贡献!如果你发现了Bug、有功能建议,或者希望改进代码,请随时通过以下方式参与:
|
||||||
|
|
||||||
为了让这个项目变得更完善,我们计划在未来实现以下功能:
|
- **提交 Issue**:如果你遇到问题或有新的想法,请在 [GitHub Issues](https://github.com/LHY0125/Gobang-Game/issues) 页面提交详细描述。
|
||||||
|
- **发起 Pull Request**:如果你对代码进行了改进,欢迎提交 Pull Request。请确保你的代码风格与项目保持一致,并附上清晰的改动说明。
|
||||||
|
|
||||||
- [ ] **图形用户界面 (GUI)**:使用 `SDL2` 或 `Qt` 等库,将当前的终端界面升级为图形化界面,提升用户体验。
|
你的每一次贡献都将使这个项目变得更好!
|
||||||
- [ ] **网络对战功能**:增加一个在线对战模式,允许两名玩家通过网络进行对战。
|
|
||||||
- [ ] **棋谱库集成**:引入开局库,使AI在游戏初期能够选择更优的开局走法。
|
## 未来计划
|
||||||
- [ ] **代码重构与优化**:持续优化现有代码,提高模块化程度和运行效率,并实现完全的跨平台兼容性。
|
|
||||||
|
为了让这个项目变得更完善,我们计划在未来实现以下功能:
|
||||||
|
|
||||||
|
- [ ] **图形用户界面 (GUI)**:使用 `SDL2` 或 `Qt` 等库,将当前的终端界面升级为图形化界面,提升用户体验。
|
||||||
|
- [ ] **网络对战功能**:增加一个在线对战模式,允许两名玩家通过网络进行对战。
|
||||||
|
- [ ] **棋谱库集成**:引入开局库,使AI在游戏初期能够选择更优的开局走法。
|
||||||
|
- [ ] **代码重构与优化**:持续优化现有代码,提高模块化程度和运行效率,并实现完全的跨平台兼容性。
|
||||||
@@ -10,94 +10,94 @@ extern const int direction[4][2];
|
|||||||
extern Step steps[MAX_STEPS];
|
extern Step steps[MAX_STEPS];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 评估特定位置对当前玩家的战略价值。
|
* @brief ?????????????????????????
|
||||||
* 此函数通过模拟在该位置落子,然后分析形成的棋型来为该位置打分。
|
* ??????????????????????????????????????????????????
|
||||||
* 分数越高,代表该位置对指定玩家越有利。
|
* ????????????????????????????????
|
||||||
* @param x 要评估的行坐标 (0-based)。
|
* @param x ????????????? (0-based)??
|
||||||
* @param y 要评估的列坐标 (0-based)。
|
* @param y ????????????? (0-based)??
|
||||||
* @param player 玩家标识 (PLAYER 或 AI),代表为哪一方进行评估。
|
* @param player ????? (PLAYER ?? AI)??????????????????????
|
||||||
* @return int 返回该位置的综合评估分数。
|
* @return int ???????????????????????
|
||||||
* @note 评分系统设计:
|
* @note ??????????
|
||||||
* - 核心思想是为不同的棋型赋予不同的权重,棋型越接近胜利,权重越高。
|
* - ??????????????????????M???????????????????????????
|
||||||
* - “活”棋型(两端无阻挡)比“眠”棋型(一端有阻挡)或“死”棋型(两端被阻挡)得分高得多,
|
* - ?????????????????\????????????????????\????????????????????\?????????
|
||||||
* 因为它们有更大的发展潜力。
|
* ?????????????????????
|
||||||
* - 评分标准 (仅为示例,可调整以优化AI行为):
|
* - ?????? (??????????????????AI???):
|
||||||
* - 连五: 1,000,000 (胜利)
|
* - ????: 1,000,000 (???)
|
||||||
* - 活四: 100,000 (下一步胜利)
|
* - ????: 100,000 (????????)
|
||||||
* - 冲四: 10,000 (下一步可能胜利)
|
* - ????: 10,000 (????????????)
|
||||||
* - 活三: 5,000 (潜力巨大)
|
* - ????: 5,000 (??????)
|
||||||
* - 眠三: 1,000
|
* - ????: 1,000
|
||||||
* - 活二: 500
|
* - ???: 500
|
||||||
* - 眠二: 100
|
* - ???: 100
|
||||||
* - 其他: 更低的分数
|
* - ????: ????????
|
||||||
* - 位置奖励:棋盘中心区域通常具有更高的战略价值,因此会给予额外加分,鼓励AI占据中心。
|
* - ?????????????????????????????????????????????????????????AI????????
|
||||||
*/
|
*/
|
||||||
int evaluate_pos(int x, int y, int player)
|
int evaluate_pos(int x, int y, int player)
|
||||||
{
|
{
|
||||||
// 保存原始值用于还原
|
// ?????????????
|
||||||
int original = board[x][y];
|
int original = board[x][y];
|
||||||
// 模拟在该位置落子
|
// ??????????????
|
||||||
board[x][y] = player;
|
board[x][y] = player;
|
||||||
|
|
||||||
int total_score = 0; // 总分
|
int total_score = 0; // ???
|
||||||
int line_scores[4] = {0}; // 四个方向的得分
|
int line_scores[4] = {0}; // ???????????
|
||||||
|
|
||||||
// 遍历四个方向进行评估
|
// ??????????????????
|
||||||
for (int i = 0; i < 4; i++)
|
for (int i = 0; i < 4; i++)
|
||||||
{
|
{
|
||||||
int dx = direction[i][0], dy = direction[i][1];
|
int dx = direction[i][0], dy = direction[i][1];
|
||||||
// 获取当前方向上的棋型信息
|
// ????????????????????
|
||||||
DirInfo info = count_specific_direction(x, y, dx, dy, player);
|
DirInfo info = count_specific_direction(x, y, dx, dy, player);
|
||||||
|
|
||||||
// 直接形成五连珠为必胜
|
// ?????????????????
|
||||||
if (info.continuous_chess >= 5)
|
if (info.continuous_chess >= 5)
|
||||||
{
|
{
|
||||||
board[x][y] = original; // 还原棋盘
|
board[x][y] = original; // ???????
|
||||||
return 1000000; // 返回最大分
|
return 1000000; // ????????
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据连续棋子数评分
|
// ??????????????????
|
||||||
switch (info.continuous_chess)
|
switch (info.continuous_chess)
|
||||||
{
|
{
|
||||||
case 4: // 四连珠
|
case 4: // ??????
|
||||||
if (info.check_start && info.check_end) // 活四(两端开放)
|
if (info.check_start && info.check_end) // ????(???????)
|
||||||
line_scores[i] = 100000;
|
line_scores[i] = 100000;
|
||||||
else if (info.check_start || info.check_end) // 冲四(一端开放)
|
else if (info.check_start || info.check_end) // ????(??????)
|
||||||
line_scores[i] = 10000;
|
line_scores[i] = 10000;
|
||||||
else // 死四(两端封闭)
|
else // ????(??????)
|
||||||
line_scores[i] = 500;
|
line_scores[i] = 500;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 3: // 三连珠
|
case 3: // ??????
|
||||||
if (info.check_start && info.check_end) // 活三
|
if (info.check_start && info.check_end) // ????
|
||||||
line_scores[i] = 5000;
|
line_scores[i] = 5000;
|
||||||
else if (info.check_start || info.check_end) // 眠三
|
else if (info.check_start || info.check_end) // ????
|
||||||
line_scores[i] = 1000;
|
line_scores[i] = 1000;
|
||||||
else // 死三
|
else // ????
|
||||||
line_scores[i] = 50;
|
line_scores[i] = 50;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 2: // 二连珠
|
case 2: // ??????
|
||||||
if (info.check_start && info.check_end) // 活二
|
if (info.check_start && info.check_end) // ???
|
||||||
line_scores[i] = 500;
|
line_scores[i] = 500;
|
||||||
else if (info.check_start || info.check_end) // 眠二
|
else if (info.check_start || info.check_end) // ???
|
||||||
line_scores[i] = 100;
|
line_scores[i] = 100;
|
||||||
else // 死二
|
else // ????
|
||||||
line_scores[i] = 10;
|
line_scores[i] = 10;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 1: // 单子
|
case 1: // ????
|
||||||
if (info.check_start && info.check_end) // 开放位置
|
if (info.check_start && info.check_end) // ????????
|
||||||
line_scores[i] = 50;
|
line_scores[i] = 50;
|
||||||
else if (info.check_start || info.check_end) // 半开放位置
|
else if (info.check_start || info.check_end) // ????????
|
||||||
line_scores[i] = 10;
|
line_scores[i] = 10;
|
||||||
else // 封闭位置
|
else // ???????
|
||||||
line_scores[i] = 1;
|
line_scores[i] = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算总分(最高方向分+其他方向分加权)
|
// ???????????????+?????????????
|
||||||
int max_score = 0;
|
int max_score = 0;
|
||||||
int sum_score = 0;
|
int sum_score = 0;
|
||||||
for (int i = 0; i < 4; i++)
|
for (int i = 0; i < 4; i++)
|
||||||
@@ -106,49 +106,49 @@ int evaluate_pos(int x, int y, int player)
|
|||||||
max_score = line_scores[i];
|
max_score = line_scores[i];
|
||||||
sum_score += line_scores[i];
|
sum_score += line_scores[i];
|
||||||
}
|
}
|
||||||
total_score = max_score * 10 + sum_score; // 主方向权重更高
|
total_score = max_score * 10 + sum_score; // ????????????
|
||||||
|
|
||||||
// 位置奖励:越靠近中心分数越高
|
// ????????????????????????
|
||||||
int center_x = BOARD_SIZE / 2;
|
int center_x = BOARD_SIZE / 2;
|
||||||
int center_y = BOARD_SIZE / 2;
|
int center_y = BOARD_SIZE / 2;
|
||||||
int distance = abs(x - center_x) + abs(y - center_y); // 曼哈顿距离
|
int distance = abs(x - center_x) + abs(y - center_y); // ?????????
|
||||||
int position_bonus = 50 * (BOARD_SIZE - distance); // 距离中心越近奖励越高
|
int position_bonus = 50 * (BOARD_SIZE - distance); // ??????????????????
|
||||||
|
|
||||||
board[x][y] = original; // 还原棋盘状态
|
board[x][y] = original; // ?????????
|
||||||
return total_score + position_bonus; // 返回总评估分
|
return total_score + position_bonus; // ????????????
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 使用带α-β剪枝的深度优先搜索(Minimax算法)来寻找最佳落子点。
|
* @brief ??????-???????????????????Minimax?????????????????
|
||||||
* 该函数递归地探索未来的几步棋,评估不同选择的优劣,并选择最优策略。
|
* ??????????????????????????????????????????????????????
|
||||||
* @param x 上一步落子的行坐标。
|
* @param x ????????????????
|
||||||
* @param y 上一步落子的列坐标。
|
* @param y ????????????????
|
||||||
* @param player 当前轮到的玩家 (PLAYER 或 AI)。
|
* @param player ??????????? (PLAYER ?? AI)??
|
||||||
* @param depth 剩余的搜索深度。深度越大,AI看得越远,但计算量也越大。
|
* @param depth ??????????????????AI????????????????????
|
||||||
* @param alpha α值,极大化玩家(AI)当前能确保的最好结果(下界)。
|
* @param alpha ?????????????AI?????????????????????????
|
||||||
* @param beta β值,极小化玩家(对手)当前能确保的最好结果(上界)。
|
* @param beta ???????????????????????????????????????????
|
||||||
* @param is_maximizing 布尔值,true表示当前是极大化玩家(AI)的回合,false表示是极小化玩家(对手)的回合。
|
* @param is_maximizing ???????true???????????????AI???????false????????????????????????
|
||||||
* @return int 返回在当前分支下的最佳评估分数。
|
* @return int ???????????????????????????
|
||||||
* @note 算法核心思想:
|
* @note ?????????:
|
||||||
* 1. **递归终止条件**: 当游戏出现胜负、平局,或达到预设的搜索深度时,停止递归,并返回当前局面的静态评估分数。
|
* 1. **??????????**: ?????????????????????????????????????????????????????????????????
|
||||||
* 2. **极大化玩家 (AI)**: 尝试所有可能的落子,并选择能使其得分最大化的那一步。此过程中,会不断更新alpha值。
|
* 2. **??????? (AI)**: ??????????????????????????????????????????????????????????alpha???
|
||||||
* 3. **极小化玩家 (对手)**: 模拟对手的走法,并假设对手会选择能使AI得分最小化的那一步。此过程中,会不断更新beta值。
|
* 3. **????????? (????)**: ????????????????????????????AI???????????????????????????????????beta???
|
||||||
* 4. **α-β剪枝**: 这是对Minimax算法的关键优化。
|
* 4. **??-????**: ?????Minimax???????????
|
||||||
* - **α剪枝**: 在极小化玩家的回合中,如果发现一个走法得到的分数比alpha还低,那么这个分支可以被剪掉,
|
* - **?????**: ????????????????????????????????????????alpha?????????????????????????
|
||||||
* 因为极大化玩家绝不会选择进入这个分支(他有更好的选择)。(if beta <= alpha)
|
* ??????????????????????????????????????????(if beta <= alpha)
|
||||||
* - **β剪枝**: 在极大化玩家的回ah合中,如果发现一个走法得到的分数比beta还高,那么这个分支也可以被剪掉,
|
* - **????**: ??????????ah?????????????????????????????beta??????????????????????????
|
||||||
* 因为极小化玩家绝不会让游戏进入这个分支(他有更好的选择来阻止)。(if beta <= alpha)
|
* ???????????????????????????????????????????????????????(if beta <= alpha)
|
||||||
* 通过剪枝,可以避免对大量无效分支的搜索,极大地提高了AI的决策效率。
|
* ????????????????????????????????????????????AI??????????
|
||||||
*/
|
*/
|
||||||
int dfs(int x, int y, int player, int depth, int alpha, int beta, bool is_maximizing)
|
int dfs(int x, int y, int player, int depth, int alpha, int beta, bool is_maximizing)
|
||||||
{
|
{
|
||||||
// 检查当前落子是否获胜
|
// ???n??????????
|
||||||
if (check_win(x, y, player))
|
if (check_win(x, y, player))
|
||||||
{
|
{
|
||||||
return (player == AI) ? 1000000 + depth : -1000000 - depth;
|
return (player == AI) ? 1000000 + depth : -1000000 - depth;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 达到搜索深度或平局
|
// ?????????????
|
||||||
if (depth == 0 || step_count >= BOARD_SIZE * BOARD_SIZE)
|
if (depth == 0 || step_count >= BOARD_SIZE * BOARD_SIZE)
|
||||||
{
|
{
|
||||||
return evaluate_pos(x, y, AI) - evaluate_pos(x, y, PLAYER);
|
return evaluate_pos(x, y, AI) - evaluate_pos(x, y, PLAYER);
|
||||||
@@ -156,7 +156,7 @@ int dfs(int x, int y, int player, int depth, int alpha, int beta, bool is_maximi
|
|||||||
|
|
||||||
int best_score = is_maximizing ? -1000000 : 1000000;
|
int best_score = is_maximizing ? -1000000 : 1000000;
|
||||||
|
|
||||||
// 遍历所有可能落子位置
|
// ????????????????????
|
||||||
for (int i = 0; i < BOARD_SIZE; i++)
|
for (int i = 0; i < BOARD_SIZE; i++)
|
||||||
{
|
{
|
||||||
for (int j = 0; j < BOARD_SIZE; j++)
|
for (int j = 0; j < BOARD_SIZE; j++)
|
||||||
@@ -164,34 +164,34 @@ int dfs(int x, int y, int player, int depth, int alpha, int beta, bool is_maximi
|
|||||||
if (board[i][j] != EMPTY)
|
if (board[i][j] != EMPTY)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// 模拟当前玩家落子
|
// ???????????
|
||||||
board[i][j] = player;
|
board[i][j] = player;
|
||||||
step_count++;
|
step_count++;
|
||||||
|
|
||||||
// 递归搜索(切换玩家和搜索深度)
|
// ???????(???????????????)
|
||||||
int current_score = dfs(i, j, (player == AI) ? PLAYER : AI, depth - 1, alpha, beta, !is_maximizing);
|
int current_score = dfs(i, j, (player == AI) ? PLAYER : AI, depth - 1, alpha, beta, !is_maximizing);
|
||||||
|
|
||||||
// 撤销落子
|
// ????????
|
||||||
board[i][j] = EMPTY;
|
board[i][j] = EMPTY;
|
||||||
step_count--;
|
step_count--;
|
||||||
|
|
||||||
// 极大值玩家(AI)逻辑
|
// ????????(AI)???
|
||||||
if (is_maximizing)
|
if (is_maximizing)
|
||||||
{
|
{
|
||||||
best_score = (current_score > best_score) ? current_score : best_score;
|
best_score = (current_score > best_score) ? current_score : best_score;
|
||||||
alpha = (best_score > alpha) ? best_score : alpha;
|
alpha = (best_score > alpha) ? best_score : alpha;
|
||||||
// α剪枝
|
// ?????
|
||||||
if (beta <= alpha)
|
if (beta <= alpha)
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 极小值玩家(人类)逻辑
|
// ????????(????)???
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
best_score = (current_score < best_score) ? current_score : best_score;
|
best_score = (current_score < best_score) ? current_score : best_score;
|
||||||
beta = (best_score < beta) ? best_score : beta;
|
beta = (best_score < beta) ? best_score : beta;
|
||||||
// β剪枝
|
// ????
|
||||||
if (beta <= alpha)
|
if (beta <= alpha)
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
@@ -200,7 +200,7 @@ int dfs(int x, int y, int player, int depth, int alpha, int beta, bool is_maximi
|
|||||||
}
|
}
|
||||||
if ((is_maximizing && best_score >= beta) || (!is_maximizing && best_score <= alpha))
|
if ((is_maximizing && best_score >= beta) || (!is_maximizing && best_score <= alpha))
|
||||||
{
|
{
|
||||||
break; // 提前退出外层循环
|
break; // ????????????
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -208,18 +208,18 @@ int dfs(int x, int y, int player, int depth, int alpha, int beta, bool is_maximi
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief AI决策主函数,使用评估函数和搜索算法选择最佳落子位置
|
* @brief AI?????????????????????????????????????????????
|
||||||
* @note 采用两阶段决策逻辑:
|
* @note ??????????????????
|
||||||
* 1. 防御阶段:检查并阻止玩家即将获胜的位置(活四、冲四、活三)
|
* 1. ????????????????????????????????????????????????
|
||||||
* 2. 进攻阶段:若无紧急防御需求,使用DFS评估选择最佳进攻位置
|
* 2. ???????????????????????????DFS?????????????????
|
||||||
* @note 实现细节:
|
* @note ???????
|
||||||
* - 优先处理玩家活四、冲四等危险局面
|
* - ??????????????????????????
|
||||||
* - 步数>10时缩小搜索范围到已有棋子附近2格
|
* - ????>10??????????????????????????2??
|
||||||
* - 使用中心位置优先策略
|
* - ??????????????????
|
||||||
*/
|
*/
|
||||||
void ai_move(int depth)
|
void ai_move(int depth)
|
||||||
{
|
{
|
||||||
// 1. 首先检查是否需要阻止玩家的四子连棋或三子活棋
|
// 1. ???????????????????????????????????
|
||||||
for (int i = 0; i < BOARD_SIZE; i++)
|
for (int i = 0; i < BOARD_SIZE; i++)
|
||||||
{
|
{
|
||||||
for (int j = 0; j < BOARD_SIZE; j++)
|
for (int j = 0; j < BOARD_SIZE; j++)
|
||||||
@@ -227,23 +227,23 @@ void ai_move(int depth)
|
|||||||
if (board[i][j] != EMPTY)
|
if (board[i][j] != EMPTY)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// 模拟玩家在此位置落子
|
// ?????????????????
|
||||||
board[i][j] = PLAYER;
|
board[i][j] = PLAYER;
|
||||||
bool need_block = false;
|
bool need_block = false;
|
||||||
|
|
||||||
// 检查四个方向
|
// ??????????
|
||||||
for (int k = 0; k < 4; k++)
|
for (int k = 0; k < 4; k++)
|
||||||
{
|
{
|
||||||
DirInfo info = count_specific_direction(i, j, direction[k][0], direction[k][1], PLAYER);
|
DirInfo info = count_specific_direction(i, j, direction[k][0], direction[k][1], PLAYER);
|
||||||
|
|
||||||
// 如果玩家能形成四子连棋且至少一端开放
|
// ????????????????????????????????
|
||||||
if (info.continuous_chess >= 4 && (info.check_start || info.check_end))
|
if (info.continuous_chess >= 4 && (info.check_start || info.check_end))
|
||||||
{
|
{
|
||||||
need_block = true;
|
need_block = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果玩家能形成三子活棋且两端开放
|
// ????????????????????????????
|
||||||
if (info.continuous_chess == 3 && info.check_start && info.check_end)
|
if (info.continuous_chess == 3 && info.check_start && info.check_end)
|
||||||
{
|
{
|
||||||
need_block = true;
|
need_block = true;
|
||||||
@@ -251,24 +251,24 @@ void ai_move(int depth)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
board[i][j] = EMPTY; // 恢复棋盘
|
board[i][j] = EMPTY; // ???????
|
||||||
|
|
||||||
if (need_block)
|
if (need_block)
|
||||||
{
|
{
|
||||||
// 必须在此位置落子阻止
|
// ??????????????????
|
||||||
board[i][j] = AI;
|
board[i][j] = AI;
|
||||||
steps[step_count++] = (Step){AI, i, j};
|
steps[step_count++] = (Step){AI, i, j};
|
||||||
printf("AI落子(%d, %d)\n", i + 1, j + 1);
|
printf("AI????(%d, %d)\n", i + 1, j + 1);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. 如果没有需要立即阻止的情况,则正常评估
|
// 2. ?????????????????????????????????
|
||||||
int best_score = -1000000;
|
int best_score = -1000000;
|
||||||
int best_x = -1, best_y = -1;
|
int best_x = -1, best_y = -1;
|
||||||
|
|
||||||
// 遍历棋盘所有空位
|
// ????????????????
|
||||||
for (int i = 0; i < BOARD_SIZE; i++)
|
for (int i = 0; i < BOARD_SIZE; i++)
|
||||||
{
|
{
|
||||||
for (int j = 0; j < BOARD_SIZE; j++)
|
for (int j = 0; j < BOARD_SIZE; j++)
|
||||||
@@ -276,7 +276,7 @@ void ai_move(int depth)
|
|||||||
if (board[i][j] != EMPTY)
|
if (board[i][j] != EMPTY)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// 只考虑已有棋子附近(2格范围内)
|
// ????????????????(2??????)
|
||||||
bool has_nearby_stone = false;
|
bool has_nearby_stone = false;
|
||||||
for (int di = -2; di <= 2; di++)
|
for (int di = -2; di <= 2; di++)
|
||||||
{
|
{
|
||||||
@@ -300,12 +300,12 @@ void ai_move(int depth)
|
|||||||
if (!has_nearby_stone && step_count > 10)
|
if (!has_nearby_stone && step_count > 10)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// 模拟AI落子
|
// ???AI????
|
||||||
board[i][j] = AI;
|
board[i][j] = AI;
|
||||||
int current_score = dfs(i, j, PLAYER, depth, -1000000, 1000000, false);
|
int current_score = dfs(i, j, PLAYER, depth, -1000000, 1000000, false);
|
||||||
board[i][j] = EMPTY;
|
board[i][j] = EMPTY;
|
||||||
|
|
||||||
// 更新最佳位置
|
// ???????????
|
||||||
if (current_score > best_score)
|
if (current_score > best_score)
|
||||||
{
|
{
|
||||||
best_score = current_score;
|
best_score = current_score;
|
||||||
@@ -315,11 +315,11 @@ void ai_move(int depth)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 执行最佳落子
|
// ??????????
|
||||||
if (best_x != -1 && best_y != -1)
|
if (best_x != -1 && best_y != -1)
|
||||||
{
|
{
|
||||||
board[best_x][best_y] = AI;
|
board[best_x][best_y] = AI;
|
||||||
steps[step_count++] = (Step){AI, best_x, best_y};
|
steps[step_count++] = (Step){AI, best_x, best_y};
|
||||||
printf("AI落子(%d, %d)\n", best_x + 1, best_y + 1);
|
printf("AI????(%d, %d)\n", best_x + 1, best_y + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,30 +4,30 @@
|
|||||||
#include "gobang.h"
|
#include "gobang.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 评估特定位置对当前玩家的价值
|
* @brief ????????????????????
|
||||||
* @param x 行坐标(0-base)
|
* @param x ??????(0-base)
|
||||||
* @param y 列坐标(0-base)
|
* @param y ??????(0-base)
|
||||||
* @param player 玩家标识(PLAYER/AI)
|
* @param player ?????(PLAYER/AI)
|
||||||
* @return int 位置评估分数(越高越好)
|
* @return int ????????????(??????)
|
||||||
*/
|
*/
|
||||||
int evaluate_pos(int x, int y, int player);
|
int evaluate_pos(int x, int y, int player);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 带α-β剪枝的深度优先搜索(极小极大算法)
|
* @brief ????-?????????????????(??????????)
|
||||||
* @param x 当前行坐标
|
* @param x ?????????
|
||||||
* @param y 当前列坐标
|
* @param y ?????????
|
||||||
* @param player 当前玩家
|
* @param player ??????
|
||||||
* @param depth 搜索深度
|
* @param depth ???????
|
||||||
* @param alpha α值(当前最大值)
|
* @param alpha ???(???????)
|
||||||
* @param beta β值(当前最小值)
|
* @param beta ???(????????)
|
||||||
* @param is_maximizing 是否为极大化玩家
|
* @param is_maximizing ???????????
|
||||||
* @return int 最佳评估分数
|
* @return int ???????????
|
||||||
*/
|
*/
|
||||||
int dfs(int x, int y, int player, int depth, int alpha, int beta, bool is_maximizing);
|
int dfs(int x, int y, int player, int depth, int alpha, int beta, bool is_maximizing);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief AI落子决策函数
|
* @brief AI??????????
|
||||||
* 使用评估函数和搜索算法选择最佳落子位置
|
* ?????????????????????????????????
|
||||||
*/
|
*/
|
||||||
void ai_move(int depth);
|
void ai_move(int depth);
|
||||||
|
|
||||||
|
|||||||
+61
-67
@@ -12,12 +12,12 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 从用户获取整数输入
|
* @brief ????????????????
|
||||||
*
|
*
|
||||||
* @param prompt 提示信息
|
* @param prompt ??????
|
||||||
* @param min 最小值
|
* @param min ?????
|
||||||
* @param max 最大值
|
* @param max ????
|
||||||
* @return int 用户输入的整数
|
* @return int ????????????
|
||||||
*/
|
*/
|
||||||
int get_integer_input(const char *prompt, int min, int max)
|
int get_integer_input(const char *prompt, int min, int max)
|
||||||
{
|
{
|
||||||
@@ -32,23 +32,23 @@ int get_integer_input(const char *prompt, int min, int max)
|
|||||||
|
|
||||||
if (result == 1 && value >= min && value <= max)
|
if (result == 1 && value >= min && value <= max)
|
||||||
{
|
{
|
||||||
// 清除输入缓冲区中剩余的字符
|
// ????????????????????
|
||||||
while ((ch = getchar()) != '\n' && ch != EOF)
|
while ((ch = getchar()) != '\n' && ch != EOF)
|
||||||
;
|
;
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// 清除无效输入
|
// ???????????
|
||||||
while ((ch = getchar()) != '\n' && ch != EOF)
|
while ((ch = getchar()) != '\n' && ch != EOF)
|
||||||
;
|
;
|
||||||
printf("输入无效,请输入一个介于 %d 和 %d 之间的整数。\n", min, max);
|
printf("??????????????????????? %d ?? %d ??????????\n", min, max);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 处理玩家回合
|
* @brief ?????????
|
||||||
*
|
*
|
||||||
* @param current_player
|
* @param current_player
|
||||||
* @return true
|
* @return true
|
||||||
@@ -65,70 +65,65 @@ bool parse_player_input(int *x, int *y)
|
|||||||
scanf("%s", input);
|
scanf("%s", input);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
Sleep(100); // a small delay to prevent high CPU usage
|
Sleep(100);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sscanf(input, "%d", x) == 1)
|
if (sscanf(input, "%d", x) == 1)
|
||||||
{
|
{
|
||||||
// Successfully parsed the first number, now parse the second
|
|
||||||
if (scanf("%d", y) != 1)
|
if (scanf("%d", y) != 1)
|
||||||
{
|
{
|
||||||
// Check for special commands if second number is not available
|
|
||||||
if (*x == INPUT_UNDO)
|
if (*x == INPUT_UNDO)
|
||||||
{
|
{
|
||||||
int steps_to_undo;
|
int steps_to_undo;
|
||||||
printf("请输入要悔棋的步数(双方各退一步): ");
|
printf("???????????????(??????????): ");
|
||||||
steps_to_undo = get_integer_input("", 1, step_count / 2);
|
steps_to_undo = get_integer_input("", 1, step_count / 2);
|
||||||
if (return_move(steps_to_undo * 2))
|
if (return_move(steps_to_undo * 2))
|
||||||
{
|
{
|
||||||
printf("成功悔棋,双方各退 %d 步!\n", steps_to_undo);
|
printf("??????????????? %d ????\n", steps_to_undo);
|
||||||
print_board();
|
print_board();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("无法悔棋!\n");
|
printf("????????\n");
|
||||||
}
|
}
|
||||||
return false; // Special command
|
return false;
|
||||||
}
|
}
|
||||||
else if (*x == INPUT_SAVE)
|
else if (*x == INPUT_SAVE)
|
||||||
{
|
{
|
||||||
// ... handle save ...
|
return false;
|
||||||
return false; // Special command
|
|
||||||
}
|
}
|
||||||
else if (*x == INPUT_EXIT)
|
else if (*x == INPUT_EXIT)
|
||||||
{
|
{
|
||||||
// ... handle exit ...
|
return false;
|
||||||
return false; // Special command
|
|
||||||
}
|
}
|
||||||
printf("无效输入,请输入两个数字坐标。");
|
printf("??????????????????????????");
|
||||||
while (getchar() != '\n')
|
while (getchar() != '\n')
|
||||||
;
|
;
|
||||||
return false; // Invalid input
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// sscanf failed, check for 'r' or 'R'
|
|
||||||
if (input[0] == 'r' || input[0] == 'R')
|
if (input[0] == 'r' || input[0] == 'R')
|
||||||
{
|
{
|
||||||
int steps_to_undo;
|
int steps_to_undo;
|
||||||
printf("请输入要悔棋的步数(双方各退一步): ");
|
printf("???????????????(??????????): ");
|
||||||
steps_to_undo = get_integer_input("", 1, step_count / 2);
|
steps_to_undo = get_integer_input("", 1, step_count / 2);
|
||||||
if (return_move(steps_to_undo * 2))
|
if (return_move(steps_to_undo * 2))
|
||||||
{
|
{
|
||||||
printf("成功悔棋,双方各退 %d 步!\n", steps_to_undo);
|
printf("??????????????? %d ????\n", steps_to_undo);
|
||||||
print_board();
|
print_board();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("无法悔棋!\n");
|
printf("????????\n");
|
||||||
}
|
}
|
||||||
return false; // Special command
|
return false;
|
||||||
}
|
}
|
||||||
printf("无效输入,请输入数字坐标或'r'悔棋。");
|
printf("???????????????????????'r'????^");
|
||||||
return false; // Invalid input
|
return false;
|
||||||
}
|
}
|
||||||
return true; // Valid coordinates
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool handle_player_turn(int current_player)
|
bool handle_player_turn(int current_player)
|
||||||
@@ -140,7 +135,7 @@ bool handle_player_turn(int current_player)
|
|||||||
time(&start_time);
|
time(&start_time);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("\n玩家%d, 请输入落子坐标(行 列,1~%d),或输入R/r悔棋:", current_player, BOARD_SIZE);
|
printf("\n???%d, ??????????????(?? ????1~%d)????????R/r????:", current_player, BOARD_SIZE);
|
||||||
|
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
@@ -149,18 +144,17 @@ bool handle_player_turn(int current_player)
|
|||||||
time(&end_time);
|
time(&end_time);
|
||||||
if (difftime(end_time, start_time) > time_limit)
|
if (difftime(end_time, start_time) > time_limit)
|
||||||
{
|
{
|
||||||
printf("\n玩家%d超时, 对方获胜!\n", current_player);
|
printf("\n???%d???, ????????\n", current_player);
|
||||||
return false; // Timeout
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (parse_player_input(&x, &y))
|
if (parse_player_input(&x, &y))
|
||||||
{
|
{
|
||||||
break; // Valid input received
|
break;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Special command or invalid input handled, just continue the loop
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -170,33 +164,33 @@ bool handle_player_turn(int current_player)
|
|||||||
|
|
||||||
if (!player_move(x, y, current_player))
|
if (!player_move(x, y, current_player))
|
||||||
{
|
{
|
||||||
printf("坐标无效!请重新输入。\n");
|
printf("????????????????????\n");
|
||||||
return true; // 坐标无效,但回合继续
|
return true; // ??????????????????
|
||||||
}
|
}
|
||||||
print_board();
|
print_board();
|
||||||
|
|
||||||
if (check_win(x, y, current_player))
|
if (check_win(x, y, current_player))
|
||||||
{
|
{
|
||||||
printf("\n玩家%d获胜!\n", current_player);
|
printf("\n???%d?????\n", current_player);
|
||||||
return false; // 游戏结束
|
return false; // ???????
|
||||||
}
|
}
|
||||||
|
|
||||||
return true; // 成功落子
|
return true; // ???????
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 运行AI游戏
|
* @brief ????AI???
|
||||||
* @note 从文件中加载历史记录并进行复盘
|
* @note ???????????????????????????
|
||||||
* @param AI_DEPTH AI的搜索深度
|
* @param AI_DEPTH AI?????????
|
||||||
*/
|
*/
|
||||||
void run_ai_game()
|
void run_ai_game()
|
||||||
{
|
{
|
||||||
setup_game_options();
|
setup_game_options();
|
||||||
|
|
||||||
// AI对战模式
|
// AI?????
|
||||||
setup_board_size();
|
setup_board_size();
|
||||||
int AI_DEPTH = 3;
|
int AI_DEPTH = 3;
|
||||||
AI_DEPTH = get_integer_input("请选择AI难度(1~5), 数字越大越强,注意数字越大AI思考时间越长!):", 1, 5);
|
AI_DEPTH = get_integer_input("?????AI???(1~5), ?????????????????????AI???????????):", 1, 5);
|
||||||
|
|
||||||
empty_board();
|
empty_board();
|
||||||
int current_player = determine_first_player(PLAYER, AI);
|
int current_player = determine_first_player(PLAYER, AI);
|
||||||
@@ -209,7 +203,7 @@ void run_ai_game()
|
|||||||
int old_step_count = step_count;
|
int old_step_count = step_count;
|
||||||
if (!handle_player_turn(current_player))
|
if (!handle_player_turn(current_player))
|
||||||
{
|
{
|
||||||
break; // 游戏结束或超时
|
break; // ??????????
|
||||||
}
|
}
|
||||||
if (step_count > old_step_count)
|
if (step_count > old_step_count)
|
||||||
{
|
{
|
||||||
@@ -218,7 +212,7 @@ void run_ai_game()
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("\nAI思考中...\n");
|
printf("\nAI?????...\n");
|
||||||
time_t start_time, end_time;
|
time_t start_time, end_time;
|
||||||
if (use_timer)
|
if (use_timer)
|
||||||
{
|
{
|
||||||
@@ -232,7 +226,7 @@ void run_ai_game()
|
|||||||
time(&end_time);
|
time(&end_time);
|
||||||
if (difftime(end_time, start_time) > time_limit)
|
if (difftime(end_time, start_time) > time_limit)
|
||||||
{
|
{
|
||||||
printf("\nAI超时, 玩家获胜!\n");
|
printf("\nAI???, ???????\n");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -240,7 +234,7 @@ void run_ai_game()
|
|||||||
Step last_step = steps[step_count - 1];
|
Step last_step = steps[step_count - 1];
|
||||||
if (check_win(last_step.x, last_step.y, AI))
|
if (check_win(last_step.x, last_step.y, AI))
|
||||||
{
|
{
|
||||||
printf("\nAI获胜!\n");
|
printf("\nAI?????\n");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
current_player = PLAYER;
|
current_player = PLAYER;
|
||||||
@@ -248,13 +242,13 @@ void run_ai_game()
|
|||||||
|
|
||||||
if (step_count == BOARD_SIZE * BOARD_SIZE)
|
if (step_count == BOARD_SIZE * BOARD_SIZE)
|
||||||
{
|
{
|
||||||
printf("\n平局!\n");
|
printf("\n????\n");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
printf("===== 游戏结束 =====\n");
|
printf("===== ??????? =====\n");
|
||||||
int review_choice;
|
int review_choice;
|
||||||
review_choice = get_integer_input("是否要复盘本局比赛? (1-是, 0-否): ", 0, 1);
|
review_choice = get_integer_input("??????????????? (1-??, 0-??): ", 0, 1);
|
||||||
if (review_choice == 1)
|
if (review_choice == 1)
|
||||||
{
|
{
|
||||||
review_process(1); // 1 for AI mode
|
review_process(1); // 1 for AI mode
|
||||||
@@ -263,14 +257,14 @@ void run_ai_game()
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 运行双人对战模式
|
* @brief ???????????
|
||||||
* @note 从文件中加载历史记录并进行复盘
|
* @note ???????????????????????????
|
||||||
*/
|
*/
|
||||||
void run_pvp_game()
|
void run_pvp_game()
|
||||||
{
|
{
|
||||||
setup_game_options();
|
setup_game_options();
|
||||||
|
|
||||||
// 双人对战模式
|
// ???????
|
||||||
setup_board_size();
|
setup_board_size();
|
||||||
empty_board();
|
empty_board();
|
||||||
int current_player = determine_first_player(PLAYER1, PLAYER2);
|
int current_player = determine_first_player(PLAYER1, PLAYER2);
|
||||||
@@ -281,12 +275,12 @@ void run_pvp_game()
|
|||||||
int old_step_count = step_count;
|
int old_step_count = step_count;
|
||||||
if (!handle_player_turn(current_player))
|
if (!handle_player_turn(current_player))
|
||||||
{
|
{
|
||||||
break; // 游戏结束或超时
|
break; // ??????????
|
||||||
}
|
}
|
||||||
|
|
||||||
if (step_count == BOARD_SIZE * BOARD_SIZE)
|
if (step_count == BOARD_SIZE * BOARD_SIZE)
|
||||||
{
|
{
|
||||||
printf("\n平局!\n");
|
printf("\n????\n");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -295,9 +289,9 @@ void run_pvp_game()
|
|||||||
current_player = (current_player == PLAYER1) ? PLAYER2 : PLAYER1;
|
current_player = (current_player == PLAYER1) ? PLAYER2 : PLAYER1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
printf("===== 游戏结束 =====\n");
|
printf("===== ??????? =====\n");
|
||||||
int review_choice;
|
int review_choice;
|
||||||
review_choice = get_integer_input("是否要复盘本局比赛? (1-是, 0-否): ", 0, 1);
|
review_choice = get_integer_input("??????????????? (1-??, 0-??): ", 0, 1);
|
||||||
if (review_choice == 1)
|
if (review_choice == 1)
|
||||||
{
|
{
|
||||||
review_process(2); // 2 for PvP mode
|
review_process(2); // 2 for PvP mode
|
||||||
@@ -306,8 +300,8 @@ void run_pvp_game()
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 运行复盘模式
|
* @brief ??????????
|
||||||
* @note 从文件中加载历史记录并进行复盘
|
* @note ???????????????????????????
|
||||||
*/
|
*/
|
||||||
void run_review_mode()
|
void run_review_mode()
|
||||||
{
|
{
|
||||||
@@ -333,14 +327,14 @@ void run_review_mode()
|
|||||||
|
|
||||||
if (file_count > 0)
|
if (file_count > 0)
|
||||||
{
|
{
|
||||||
printf("发现以下复盘文件:\n");
|
printf("??????????????:\n");
|
||||||
for (int i = 0; i < file_count; i++)
|
for (int i = 0; i < file_count; i++)
|
||||||
{
|
{
|
||||||
printf("%d. %s\n", i + 1, record_files[i]);
|
printf("%d. %s\n", i + 1, record_files[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
char prompt[150];
|
char prompt[150];
|
||||||
sprintf(prompt, "请输入复盘文件编号(1-%d),或输入0以手动输入文件名: ", file_count);
|
sprintf(prompt, "??????????????(1-%d)????????0??????????????: ", file_count);
|
||||||
int choice = get_integer_input(prompt, 0, file_count);
|
int choice = get_integer_input(prompt, 0, file_count);
|
||||||
|
|
||||||
if (choice > 0)
|
if (choice > 0)
|
||||||
@@ -349,7 +343,7 @@ void run_review_mode()
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("请输入完整文件名: ");
|
printf("???????????????: ");
|
||||||
scanf("%s", filename);
|
scanf("%s", filename);
|
||||||
int c;
|
int c;
|
||||||
while ((c = getchar()) != '\n' && c != EOF)
|
while ((c = getchar()) != '\n' && c != EOF)
|
||||||
@@ -364,7 +358,7 @@ void run_review_mode()
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("未找到任何复盘文件,请输入复盘文件地址: ");
|
printf("???????????????????????????????: ");
|
||||||
scanf("%s", filename);
|
scanf("%s", filename);
|
||||||
int c;
|
int c;
|
||||||
while ((c = getchar()) != '\n' && c != EOF)
|
while ((c = getchar()) != '\n' && c != EOF)
|
||||||
@@ -376,11 +370,11 @@ void run_review_mode()
|
|||||||
{
|
{
|
||||||
if (game_mode == 1)
|
if (game_mode == 1)
|
||||||
{
|
{
|
||||||
printf("加载AI对战模式复盘文件成功!\n");
|
printf("????AI?????????????????\n");
|
||||||
}
|
}
|
||||||
else if (game_mode == 2)
|
else if (game_mode == 2)
|
||||||
{
|
{
|
||||||
printf("加载双人对战模式复盘文件成功!\n");
|
printf("???????????????????????\n");
|
||||||
}
|
}
|
||||||
review_process(game_mode);
|
review_process(game_mode);
|
||||||
}
|
}
|
||||||
|
|||||||
+25
-25
@@ -1,16 +1,16 @@
|
|||||||
/**
|
/**
|
||||||
* @file game_mode.h
|
* @file game_mode.h
|
||||||
* @author 刘航宇(3364451258@qq.com、15236416560@163.com、lhy3364451258@outlook.com)
|
* @author ??????(3364451258@qq.com??15236416560@163.com??lhy3364451258@outlook.com)
|
||||||
* @brief 五子棋游戏框架头文件
|
* @brief ????????????????
|
||||||
* @version 3.0
|
* @version 3.0
|
||||||
* @date 2025-06-30
|
* @date 2025-06-30
|
||||||
*
|
*
|
||||||
* @copyright Copyright (c) 2025
|
* @copyright Copyright (c) 2025
|
||||||
*
|
*
|
||||||
* @note 本文件定义了五子棋游戏的三种主要模式:
|
* @note ?????????????????????????????????
|
||||||
* 1. AI对战模式
|
* 1. AI?????
|
||||||
* 2. 双人对战模式
|
* 2. ???????
|
||||||
* 3. 复盘模式
|
* 3. ??????
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef GAME_MODE_H
|
#ifndef GAME_MODE_H
|
||||||
@@ -18,53 +18,53 @@
|
|||||||
|
|
||||||
#include "gobang.h"
|
#include "gobang.h"
|
||||||
|
|
||||||
// 特殊输入命令
|
// ????????????
|
||||||
#define INPUT_UNDO -1
|
#define INPUT_UNDO -1
|
||||||
#define INPUT_SAVE -2
|
#define INPUT_SAVE -2
|
||||||
#define INPUT_EXIT -3
|
#define INPUT_EXIT -3
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 从用户获取整数输入
|
* @brief ????????????????
|
||||||
*
|
*
|
||||||
* @param prompt 提示信息
|
* @param prompt ??????
|
||||||
* @param min 最小值
|
* @param min ?????
|
||||||
* @param max 最大值
|
* @param max ????
|
||||||
* @return int 输入的整数
|
* @return int ?????????
|
||||||
*/
|
*/
|
||||||
int get_integer_input(const char *prompt, int min, int max);
|
int get_integer_input(const char *prompt, int min, int max);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 处理玩家回合
|
* @brief ?????????
|
||||||
*
|
*
|
||||||
* @param x 玩家输入的横坐标
|
* @param x ?????????????
|
||||||
* @param y 玩家输入的纵坐标
|
* @param y ??????????????
|
||||||
* @return true 输入有效
|
* @return true ????????
|
||||||
* @return false 输入无效
|
* @return false ????????
|
||||||
*/
|
*/
|
||||||
bool parse_player_input(int *x, int *y);
|
bool parse_player_input(int *x, int *y);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 处理AI回合
|
* @brief ????AI???
|
||||||
*
|
*
|
||||||
* @param current_player 当前玩家
|
* @param current_player ??????
|
||||||
*/
|
*/
|
||||||
bool handle_player_turn(int current_player);
|
bool handle_player_turn(int current_player);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief AI对战模式
|
* @brief AI?????
|
||||||
* 实现玩家与AI的对战逻辑
|
* ????????AI???????
|
||||||
*/
|
*/
|
||||||
void run_ai_game();
|
void run_ai_game();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 双人对战模式
|
* @brief ???????
|
||||||
* 实现两个玩家之间的对战逻辑
|
* ???????????????????
|
||||||
*/
|
*/
|
||||||
void run_pvp_game();
|
void run_pvp_game();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 复盘模式
|
* @brief ??????
|
||||||
* 加载并重现历史对局
|
* ???????????????
|
||||||
*/
|
*/
|
||||||
void run_review_mode();
|
void run_review_mode();
|
||||||
|
|
||||||
|
|||||||
@@ -5,24 +5,24 @@
|
|||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
// 全局变量定义
|
// ??????????
|
||||||
int BOARD_SIZE = 15; // 实际使用的棋盘尺寸(默认15)
|
int BOARD_SIZE = 15; // ?????????????(???15)
|
||||||
int board[MAX_BOARD_SIZE][MAX_BOARD_SIZE] = {0}; // 棋盘状态存储数组(默认棋盘全空为0)
|
int board[MAX_BOARD_SIZE][MAX_BOARD_SIZE] = {0}; // ?????????????(???????????0)
|
||||||
|
|
||||||
Step steps[MAX_STEPS]; // 存储所有落子步骤的数组
|
Step steps[MAX_STEPS]; // ???????????????????
|
||||||
const int direction[4][2] = {{1, 0}, {0, 1}, {1, 1}, {1, -1}}; // 四个方向:向下、向右、右下、左下
|
const int direction[4][2] = {{1, 0}, {0, 1}, {1, 1}, {1, -1}}; // ??????????????????????????
|
||||||
int step_count = 0; // 当前步数计数器
|
int step_count = 0; // ?????????????
|
||||||
bool use_forbidden_moves = false; // 默认不启用禁手规则
|
bool use_forbidden_moves = false; // ??????????????
|
||||||
int use_timer = 0; // 默认不启用计时器
|
int use_timer = 0; // ?????????????
|
||||||
int time_limit = 30; // 默认时间限制为30秒
|
int time_limit = 30; // ???????????30??
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 初始化棋盘为全空状态并重置步数计数器
|
* @brief ???????????????????????????????
|
||||||
* 清空棋盘数组并将所有位置设为EMPTY,同时将step_count重置为0
|
* ?????????????????????????EMPTY??????step_count?????0
|
||||||
*/
|
*/
|
||||||
void empty_board()
|
void empty_board()
|
||||||
{
|
{
|
||||||
// 初始化棋盘状态为全空
|
// ???????????????
|
||||||
for (int i = 0; i < BOARD_SIZE; i++)
|
for (int i = 0; i < BOARD_SIZE; i++)
|
||||||
{
|
{
|
||||||
for (int j = 0; j < BOARD_SIZE; j++)
|
for (int j = 0; j < BOARD_SIZE; j++)
|
||||||
@@ -30,87 +30,87 @@ void empty_board()
|
|||||||
board[i][j] = EMPTY;
|
board[i][j] = EMPTY;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
step_count = 0; // 重置步数计数器
|
step_count = 0; // ??????????????
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 打印当前棋盘状态
|
* @brief ????????????
|
||||||
* 以可读格式输出棋盘,包括行列号和棋子状态
|
* ????????????????????????????????
|
||||||
* 玩家棋子显示为'x',AI棋子显示为'○',空位显示为'·'
|
* ???????????'x'??AI????????'??'??????????'??'
|
||||||
*/
|
*/
|
||||||
void print_board()
|
void print_board()
|
||||||
{
|
{
|
||||||
// 打印列号(1-BOARD_SIZE显示)
|
// ????????1-BOARD_SIZE?????
|
||||||
printf("\n ");
|
printf("\n ");
|
||||||
for (int i = 0; i < BOARD_SIZE; i++)
|
for (int i = 0; i < BOARD_SIZE; i++)
|
||||||
{
|
{
|
||||||
printf("%2d", i + 1);
|
printf("%2d", i + 1);
|
||||||
if (i + 1 == 9) // 处理列号9和10+的对齐
|
if (i + 1 == 9) // ????????9??10+?????
|
||||||
printf(" ");
|
printf(" ");
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
// 逐行打印棋盘内容
|
// ???????????????
|
||||||
for (int i = 0; i < BOARD_SIZE; i++)
|
for (int i = 0; i < BOARD_SIZE; i++)
|
||||||
{
|
{
|
||||||
printf("%2d ", i + 1); // 打印行号(1-BOARD_SIZE)
|
printf("%2d ", i + 1); // ????????1-BOARD_SIZE??
|
||||||
for (int j = 0; j < BOARD_SIZE; j++)
|
for (int j = 0; j < BOARD_SIZE; j++)
|
||||||
{
|
{
|
||||||
if (board[i][j] == PLAYER)
|
if (board[i][j] == PLAYER)
|
||||||
printf("x "); // 玩家棋子
|
printf("x "); // ???????
|
||||||
else if (board[i][j] == AI)
|
else if (board[i][j] == AI)
|
||||||
printf("○ "); // AI棋子(使用○显示)
|
printf("?? "); // AI????(????????)
|
||||||
else
|
else
|
||||||
printf("· "); // 空位
|
printf("?? "); // ????
|
||||||
}
|
}
|
||||||
printf("\n"); // 每行结束换行
|
printf("\n"); // ???????????
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 检查指定位置是否有效且为空
|
* @brief ??????????????????????
|
||||||
* @param x 行坐标(0-base)
|
* @param x ??????(0-base)
|
||||||
* @param y 列坐标(0-base)
|
* @param y ??????(0-base)
|
||||||
* @return true 位置有效且为空
|
* @return true ?????????????
|
||||||
* @return false 位置无效或已被占用
|
* @return false ????????????????
|
||||||
*/
|
*/
|
||||||
bool have_space(int x, int y)
|
bool have_space(int x, int y)
|
||||||
{
|
{
|
||||||
// 校验坐标是否在范围内且该位置为空
|
// ????????????????????????????
|
||||||
return (x >= 0 && x < BOARD_SIZE && y >= 0 && y < BOARD_SIZE && board[x][y] == EMPTY);
|
return (x >= 0 && x < BOARD_SIZE && y >= 0 && y < BOARD_SIZE && board[x][y] == EMPTY);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 配置棋盘大小
|
* @brief ???????????
|
||||||
*
|
*
|
||||||
* @param player1 玩家1
|
* @param player1 ???1
|
||||||
* @param player2 玩家2
|
* @param player2 ???2
|
||||||
*/
|
*/
|
||||||
void setup_board_size()
|
void setup_board_size()
|
||||||
{
|
{
|
||||||
printf("通常棋盘大小分为休闲棋盘(13X13)、标准棋盘(15X15)和特殊棋盘(19X19)\n");
|
printf("?????????????????????(13X13)?????????(15X15)??????????(19X19)\n");
|
||||||
char prompt[100];
|
char prompt[100];
|
||||||
sprintf(prompt, "请输入棋盘大小(5~%d)(默认为标准棋盘):\n", MAX_BOARD_SIZE);
|
sprintf(prompt, "?????????????(5~%d)(???????????):\n", MAX_BOARD_SIZE);
|
||||||
BOARD_SIZE = get_integer_input(prompt, 5, MAX_BOARD_SIZE);
|
BOARD_SIZE = get_integer_input(prompt, 5, MAX_BOARD_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Set the up game options object
|
* @brief Set the up game options object
|
||||||
* 配置游戏选项,包括禁手规则、计时器和时间限制
|
* ??????????????????????????????????
|
||||||
*/
|
*/
|
||||||
void setup_game_options()
|
void setup_game_options()
|
||||||
{
|
{
|
||||||
use_forbidden_moves = get_integer_input("是否启用禁手规则 (1-是, 0-否): ", 0, 1);
|
use_forbidden_moves = get_integer_input("????????????? (1-??, 0-??): ", 0, 1);
|
||||||
|
|
||||||
use_timer = get_integer_input("是否启用计时器 (1-是, 0-否): ", 0, 1);
|
use_timer = get_integer_input("???????????? (1-??, 0-??): ", 0, 1);
|
||||||
if (use_timer)
|
if (use_timer)
|
||||||
{
|
{
|
||||||
time_limit = get_integer_input("请输入每回合的时间限制 (1~60分钟): ", 1, 60) * 60;
|
time_limit = get_integer_input("?????????????????? (1~60????): ", 1, 60) * 60;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 确定先手玩家
|
* @brief ??????????
|
||||||
*
|
*
|
||||||
* @param player1
|
* @param player1
|
||||||
* @param player2
|
* @param player2
|
||||||
@@ -119,7 +119,7 @@ void setup_game_options()
|
|||||||
int determine_first_player(int player1, int player2)
|
int determine_first_player(int player1, int player2)
|
||||||
{
|
{
|
||||||
char prompt[100];
|
char prompt[100];
|
||||||
sprintf(prompt, "请选择先手方 (1 for Player %d, 2 for Player %d): ", player1, player2);
|
sprintf(prompt, "?????????? (1 for Player %d, 2 for Player %d): ", player1, player2);
|
||||||
int first_player_choice = get_integer_input(prompt, 1, 2);
|
int first_player_choice = get_integer_input(prompt, 1, 2);
|
||||||
if (first_player_choice == 1)
|
if (first_player_choice == 1)
|
||||||
{
|
{
|
||||||
@@ -132,7 +132,7 @@ int determine_first_player(int player1, int player2)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 检查是否为禁手
|
* @brief ???????????
|
||||||
*
|
*
|
||||||
* @param x
|
* @param x
|
||||||
* @param y
|
* @param y
|
||||||
@@ -163,7 +163,7 @@ bool is_forbidden_move(int x, int y, int player)
|
|||||||
if (info.continuous_chess > 5)
|
if (info.continuous_chess > 5)
|
||||||
{
|
{
|
||||||
board[x][y] = EMPTY;
|
board[x][y] = EMPTY;
|
||||||
return true; // 长连禁手
|
return true; // ????????
|
||||||
}
|
}
|
||||||
if (info.continuous_chess == 3 && info.check_start && info.check_end)
|
if (info.continuous_chess == 3 && info.check_start && info.check_end)
|
||||||
{
|
{
|
||||||
@@ -179,77 +179,77 @@ bool is_forbidden_move(int x, int y, int player)
|
|||||||
|
|
||||||
if (three_count >= 2 || four_count >= 2)
|
if (three_count >= 2 || four_count >= 2)
|
||||||
{
|
{
|
||||||
return true; // 三三或四四禁手
|
return true; // ?????????????
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 执行玩家落子操作
|
* @brief ?????????????
|
||||||
* @param x 行坐标(0-base)
|
* @param x ??????(0-base)
|
||||||
* @param y 列坐标(0-base)
|
* @param y ??????(0-base)
|
||||||
* @return true 落子成功
|
* @return true ??????
|
||||||
* @return false 落子失败(位置无效)
|
* @return false ???????(????????)
|
||||||
*/
|
*/
|
||||||
bool player_move(int x, int y, int player)
|
bool player_move(int x, int y, int player)
|
||||||
{
|
{
|
||||||
// 位置无效则返回false
|
// ???????????false
|
||||||
if (!have_space(x, y))
|
if (!have_space(x, y))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (is_forbidden_move(x, y, player))
|
if (is_forbidden_move(x, y, player))
|
||||||
{
|
{
|
||||||
printf("禁手!请选择其他位置。\n");
|
printf("????????????????????\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新棋盘状态
|
// ??????????
|
||||||
board[x][y] = player;
|
board[x][y] = player;
|
||||||
// 记录落子步骤:玩家标识和坐标
|
// ??????????s???????????
|
||||||
steps[step_count++] = (Step){player, x, y};
|
steps[step_count++] = (Step){player, x, y};
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 计算特定方向上连续同色棋子数量
|
* @brief ???????????????????????????
|
||||||
* @param x 起始行坐标
|
* @param x ?????????
|
||||||
* @param y 起始列坐标
|
* @param y ?????????
|
||||||
* @param dx 行方向增量(-1,0,1)
|
* @param dx ??????????(-1,0,1)
|
||||||
* @param dy 列方向增量(-1,0,1)
|
* @param dy ??????????(-1,0,1)
|
||||||
* @param player 玩家标识(PLAYER/AI)
|
* @param player ?????(PLAYER/AI)
|
||||||
* @return DirInfo 包含连续棋子数和方向开放状态的结构体
|
* @return DirInfo ???????????????????????????
|
||||||
* @note 检查正反两个方向,统计连续棋子数并判断端点是否开放
|
* @note ????????????????????????????????????????
|
||||||
*/
|
*/
|
||||||
DirInfo count_specific_direction(int x, int y, int dx, int dy, int player)
|
DirInfo count_specific_direction(int x, int y, int dx, int dy, int player)
|
||||||
{
|
{
|
||||||
DirInfo info;
|
DirInfo info;
|
||||||
info.continuous_chess = 1; // 起始位置已经有一个棋子
|
info.continuous_chess = 1; // ???????????????????
|
||||||
info.check_start = false; // 起点方向是否开放
|
info.check_start = false; // ?????????
|
||||||
info.check_end = false; // 终点方向是否开放
|
info.check_end = false; // ?????????
|
||||||
|
|
||||||
// 检查正方向(dx, dy)
|
// ?????????dx, dy??
|
||||||
int nx = x + dx, ny = y + dy;
|
int nx = x + dx, ny = y + dy;
|
||||||
while (nx >= 0 && nx < BOARD_SIZE && ny >= 0 && ny < BOARD_SIZE && board[nx][ny] == player)
|
while (nx >= 0 && nx < BOARD_SIZE && ny >= 0 && ny < BOARD_SIZE && board[nx][ny] == player)
|
||||||
{
|
{
|
||||||
info.continuous_chess++; // 连续棋子计数增加
|
info.continuous_chess++; // ???????????????
|
||||||
nx += dx; // 沿当前方向前进
|
nx += dx; // ???????????
|
||||||
ny += dy;
|
ny += dy;
|
||||||
}
|
}
|
||||||
// 判断正方向端点是否开放(遇到空位)
|
// ???????????????????????????
|
||||||
if (nx >= 0 && nx < BOARD_SIZE && ny >= 0 && ny < BOARD_SIZE)
|
if (nx >= 0 && nx < BOARD_SIZE && ny >= 0 && ny < BOARD_SIZE)
|
||||||
if (board[nx][ny] == EMPTY)
|
if (board[nx][ny] == EMPTY)
|
||||||
info.check_end = true;
|
info.check_end = true;
|
||||||
|
|
||||||
// 检查反方向(-dx, -dy)
|
// ????????-dx, -dy??
|
||||||
nx = x - dx, ny = y - dy;
|
nx = x - dx, ny = y - dy;
|
||||||
while (nx >= 0 && nx < BOARD_SIZE && ny >= 0 && ny < BOARD_SIZE && board[nx][ny] == player)
|
while (nx >= 0 && nx < BOARD_SIZE && ny >= 0 && ny < BOARD_SIZE && board[nx][ny] == player)
|
||||||
{
|
{
|
||||||
info.continuous_chess++; // 连续棋子计数增加
|
info.continuous_chess++; // ???????????????
|
||||||
nx -= dx; // 沿相反方向前进
|
nx -= dx; // ???????????
|
||||||
ny -= dy;
|
ny -= dy;
|
||||||
}
|
}
|
||||||
// 判断反方向端点是否开放(遇到空位)
|
// ??????????????????????????
|
||||||
if (nx >= 0 && nx < BOARD_SIZE && ny >= 0 && ny < BOARD_SIZE)
|
if (nx >= 0 && nx < BOARD_SIZE && ny >= 0 && ny < BOARD_SIZE)
|
||||||
if (board[nx][ny] == EMPTY)
|
if (board[nx][ny] == EMPTY)
|
||||||
info.check_start = true;
|
info.check_start = true;
|
||||||
@@ -259,115 +259,115 @@ DirInfo count_specific_direction(int x, int y, int dx, int dy, int player)
|
|||||||
|
|
||||||
bool check_win(int x, int y, int player)
|
bool check_win(int x, int y, int player)
|
||||||
{
|
{
|
||||||
// 检查四个方向是否存在五连珠
|
// ??????????????????????
|
||||||
for (int i = 0; i < 4; i++)
|
for (int i = 0; i < 4; i++)
|
||||||
{
|
{
|
||||||
DirInfo info = count_specific_direction(x, y, direction[i][0], direction[i][1], player);
|
DirInfo info = count_specific_direction(x, y, direction[i][0], direction[i][1], player);
|
||||||
if (info.continuous_chess >= 5) // 连续棋子>=5即获胜
|
if (info.continuous_chess >= 5) // ????????>=5?????
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false; // 四个方向都没有五连珠
|
return false; // ????????????????
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 复盘游戏全过程并展示评分
|
* @brief ???????????????????
|
||||||
* @note 实现流程:
|
* @note ???????:
|
||||||
* 1. 初始化临时复盘棋盘
|
* 1. ????????????????
|
||||||
* 2. 按步数顺序逐步重现每个落子
|
* 2. ??????????????????????
|
||||||
* 3. 每步显示:
|
* 3. ??????:
|
||||||
* - 当前步数/总步数
|
* - ???????/?????
|
||||||
* - 落子方(玩家/AI)
|
* - ?????(???/AI)
|
||||||
* - 落子位置(1-based坐标)
|
* - ????????(1-based????)
|
||||||
* - 当前棋盘状态
|
* - ?????????
|
||||||
* 4. 通过用户按Enter键控制步骤前进
|
* 4. ????????Enter????????????
|
||||||
* 5. 复盘结束后自动进入评分环节:
|
* 5. ???????????????????????:
|
||||||
* - 评估双方表现
|
* - ???????????
|
||||||
* - 显示得分
|
* - ???????
|
||||||
* - 评选MVP
|
* - ???MVP
|
||||||
* @note 技术细节:
|
* @note ???????:
|
||||||
* - 使用独立临时棋盘避免影响主游戏状态
|
* - ??????????????????????????
|
||||||
* - 坐标显示转换为1-based方便用户理解
|
* - ???????????1-based???????????
|
||||||
* - 包含输入缓冲区清理防止意外输入
|
* - ???????????????????????????
|
||||||
* - 评分环节调用evaluate_performance()函数
|
* - ??????????evaluate_performance()????
|
||||||
*/
|
*/
|
||||||
void review_process(int game_mode)
|
void review_process(int game_mode)
|
||||||
{
|
{
|
||||||
printf("\n===== 复盘记录(总步数:%d) =====\n", step_count);
|
printf("\n===== ??????(???????%d) =====\n", step_count);
|
||||||
// 清空输入缓冲区
|
// ???????????
|
||||||
int c;
|
int c;
|
||||||
while ((c = getchar()) != '\n' && c != EOF)
|
while ((c = getchar()) != '\n' && c != EOF)
|
||||||
;
|
;
|
||||||
|
|
||||||
// 创建临时复盘棋盘
|
// ???????????????
|
||||||
int temp_board[MAX_BOARD_SIZE][MAX_BOARD_SIZE];
|
int temp_board[MAX_BOARD_SIZE][MAX_BOARD_SIZE];
|
||||||
memset(temp_board, EMPTY, sizeof(temp_board)); // 初始化为空棋盘
|
memset(temp_board, EMPTY, sizeof(temp_board)); // ????????????
|
||||||
|
|
||||||
// 逐步重现游戏过程
|
// ?????????????
|
||||||
for (int i = 0; i < step_count; i++)
|
for (int i = 0; i < step_count; i++)
|
||||||
{
|
{
|
||||||
Step s = steps[i]; // 获取当前步骤
|
Step s = steps[i]; // ??????????
|
||||||
temp_board[s.x][s.y] = s.player; // 在临时棋盘上落子
|
temp_board[s.x][s.y] = s.player; // ???????????????
|
||||||
|
|
||||||
// 打印当前步骤信息
|
// ?????????????
|
||||||
// 根据游戏模式显示不同的标题和玩家信息
|
// ???????????????????????????
|
||||||
if (game_mode == 1)
|
if (game_mode == 1)
|
||||||
{
|
{
|
||||||
// 人机对战
|
// ??????
|
||||||
printf("\n===== 五子棋人机对战(%dX%d棋盘) =====", BOARD_SIZE, BOARD_SIZE);
|
printf("\n===== ????????????(%dX%d????) =====", BOARD_SIZE, BOARD_SIZE);
|
||||||
printf("\n 第%d步/%d步: %s 落子于(%d, %d)\n",
|
printf("\n ??%d??/%d??: %s ??????(%d, %d)\n",
|
||||||
i + 1, step_count,
|
i + 1, step_count,
|
||||||
(s.player == PLAYER) ? "玩家" : "AI",
|
(s.player == PLAYER) ? "???" : "AI",
|
||||||
s.x + 1, s.y + 1);
|
s.x + 1, s.y + 1);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// 双人对战
|
// ?????
|
||||||
printf("\n===== 五子棋双人对战(%dX%d棋盘) =====", BOARD_SIZE, BOARD_SIZE);
|
printf("\n===== ???????????(%dX%d????) =====", BOARD_SIZE, BOARD_SIZE);
|
||||||
printf("\n 第%d步/%d步: %s 落子于(%d, %d)\n",
|
printf("\n ??%d??/%d??: %s ??????(%d, %d)\n",
|
||||||
i + 1, step_count,
|
i + 1, step_count,
|
||||||
(s.player == PLAYER1) ? "玩家1(黑棋)" : "玩家2(白棋)",
|
(s.player == PLAYER1) ? "???1(????)" : "???2(????)",
|
||||||
s.x + 1, s.y + 1);
|
s.x + 1, s.y + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 打印当前复盘棋盘
|
// ??????????????
|
||||||
printf(" ");
|
printf(" ");
|
||||||
for (int col = 0; col < BOARD_SIZE; col++)
|
for (int col = 0; col < BOARD_SIZE; col++)
|
||||||
printf("%2d", col + 1); // 列号
|
printf("%2d", col + 1); // ????
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
||||||
for (int row = 0; row < BOARD_SIZE; row++)
|
for (int row = 0; row < BOARD_SIZE; row++)
|
||||||
{
|
{
|
||||||
printf("%2d ", row + 1); // 行号
|
printf("%2d ", row + 1); // ????
|
||||||
for (int col = 0; col < BOARD_SIZE; col++)
|
for (int col = 0; col < BOARD_SIZE; col++)
|
||||||
{
|
{
|
||||||
if (temp_board[row][col] == PLAYER || temp_board[row][col] == PLAYER1)
|
if (temp_board[row][col] == PLAYER || temp_board[row][col] == PLAYER1)
|
||||||
printf("x ");
|
printf("x ");
|
||||||
else if (temp_board[row][col] == AI || temp_board[row][col] == PLAYER2)
|
else if (temp_board[row][col] == AI || temp_board[row][col] == PLAYER2)
|
||||||
printf("○ ");
|
printf("?? ");
|
||||||
else
|
else
|
||||||
printf("· ");
|
printf("?? ");
|
||||||
}
|
}
|
||||||
printf("\n"); // 行结束换行
|
printf("\n"); // ??????????
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果不是最后一步,等待用户按键继续
|
// ?????????????????????????????
|
||||||
if (i < step_count - 1)
|
if (i < step_count - 1)
|
||||||
{
|
{
|
||||||
printf("\n按Enter继续下一步...");
|
printf("\n??Enter?????????...");
|
||||||
while (getchar() != '\n')
|
while (getchar() != '\n')
|
||||||
; // 等待回车
|
; // ??????
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
printf("\n复盘结束!按Enter查看评分...");
|
printf("\n???????????Enter??????...");
|
||||||
getchar(); // 等待用户按键
|
getchar(); // ??????????
|
||||||
|
|
||||||
// 评估双方表现
|
// ???????????
|
||||||
printf("\n===== 对局评分 =====\n");
|
printf("\n===== ??????? =====\n");
|
||||||
int player1_score = 0, player2_score = 0;
|
int player1_score = 0, player2_score = 0;
|
||||||
|
|
||||||
// 遍历所有步数,累积每一步的得分
|
// ??????????????????????????
|
||||||
for (int i = 0; i < step_count; i++)
|
for (int i = 0; i < step_count; i++)
|
||||||
{
|
{
|
||||||
if (steps[i].player == PLAYER || steps[i].player == PLAYER1)
|
if (steps[i].player == PLAYER || steps[i].player == PLAYER1)
|
||||||
@@ -386,16 +386,16 @@ void review_process(int game_mode)
|
|||||||
{
|
{
|
||||||
if (game_mode == 1)
|
if (game_mode == 1)
|
||||||
{
|
{
|
||||||
printf("玩家得分: %d, 占比: %.2f%%\n",
|
printf("??????: %d, ???: %.2f%%\n",
|
||||||
player1_score, (double)player1_score * 100.0 / sum_score);
|
player1_score, (double)player1_score * 100.0 / sum_score);
|
||||||
printf("AI得分: %d, 占比: %.2f%%\n",
|
printf("AI????: %d, ???: %.2f%%\n",
|
||||||
player2_score, (double)player2_score * 100.0 / sum_score);
|
player2_score, (double)player2_score * 100.0 / sum_score);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("玩家1(黑棋)得分: %d, 占比: %.2f%%\n",
|
printf("???1(????)????: %d, ???: %.2f%%\n",
|
||||||
player1_score, (double)player1_score * 100.0 / sum_score);
|
player1_score, (double)player1_score * 100.0 / sum_score);
|
||||||
printf("玩家2(白棋)得分: %d, 占比: %.2f%%\n",
|
printf("???2(????)????: %d, ???: %.2f%%\n",
|
||||||
player2_score, (double)player2_score * 100.0 / sum_score);
|
player2_score, (double)player2_score * 100.0 / sum_score);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -403,43 +403,43 @@ void review_process(int game_mode)
|
|||||||
{
|
{
|
||||||
if (game_mode == 1)
|
if (game_mode == 1)
|
||||||
{
|
{
|
||||||
printf("玩家得分: %d\n", player1_score);
|
printf("??????: %d\n", player1_score);
|
||||||
printf("AI得分: %d\n", player2_score);
|
printf("AI????: %d\n", player2_score);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("玩家1(黑棋)得分: %d\n", player1_score);
|
printf("???1(????)????: %d\n", player1_score);
|
||||||
printf("玩家2(白棋)得分: %d\n", player2_score);
|
printf("???2(????)????: %d\n", player2_score);
|
||||||
}
|
}
|
||||||
printf("注: 双方得分均为0,无法计算占比\n");
|
printf("?: ?????????0????????????\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 评选MVP
|
// ???MVP
|
||||||
if (player1_score > player2_score)
|
if (player1_score > player2_score)
|
||||||
{
|
{
|
||||||
printf("\nMVP: %s (领先 %d 分)\n", (game_mode == 1) ? "玩家" : "玩家1(黑棋)", player1_score - player2_score);
|
printf("\nMVP: %s (???? %d ??)\n", (game_mode == 1) ? "???" : "???1(????)", player1_score - player2_score);
|
||||||
}
|
}
|
||||||
else if (player2_score > player1_score)
|
else if (player2_score > player1_score)
|
||||||
{
|
{
|
||||||
printf("\nMVP: %s (领先 %d 分)\n", (game_mode == 1) ? "AI" : "玩家2(白棋)", player2_score - player1_score);
|
printf("\nMVP: %s (???? %d ??)\n", (game_mode == 1) ? "AI" : "???2(????)", player2_score - player1_score);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("\n双方势均力敌!\n");
|
printf("\n????????????\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
getchar();
|
getchar();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 处理游戏结束后的记录保存
|
* @brief ????????????????????
|
||||||
* @return int 保存状态码(0-成功, 1-目录创建失败, 2-文件打开失败, 3-文件写入失败)
|
* @return int ????????(0-???, 1-?????????, 2-????????, 3-??????????)
|
||||||
*/
|
*/
|
||||||
void handle_save_record(int game_mode)
|
void handle_save_record(int game_mode)
|
||||||
{
|
{
|
||||||
int save_choice = 0;
|
int save_choice = 0;
|
||||||
printf("===== 游戏结束 =====\n");
|
printf("===== ??????? =====\n");
|
||||||
printf("是否保存游戏记录? (1-是, 0-否): ");
|
printf("??????????? (1-??, 0-??): ");
|
||||||
scanf("%d", &save_choice);
|
scanf("%d", &save_choice);
|
||||||
|
|
||||||
if (save_choice == 1)
|
if (save_choice == 1)
|
||||||
@@ -452,35 +452,35 @@ void handle_save_record(int game_mode)
|
|||||||
int save_status = save_game_to_file(filename, game_mode);
|
int save_status = save_game_to_file(filename, game_mode);
|
||||||
switch (save_status)
|
switch (save_status)
|
||||||
{
|
{
|
||||||
case 0: // 成功
|
case 0: // ???
|
||||||
printf("\n游戏记录已成功保存至: %s\n", filename);
|
printf("\n????????????????: %s\n", filename);
|
||||||
printf("您可以使用以下命令进行复盘: .\\gobang.exe -l %s\n", filename);
|
printf("????????????????????????: .\\gobang.exe -l %s\n", filename);
|
||||||
break;
|
break;
|
||||||
case 1: // 目录创建失败
|
case 1: // ?????????
|
||||||
printf("\n游戏记录保存失败: 无法创建 'records' 目录。\n");
|
printf("\n?????????????: ??????? 'records' ????\n");
|
||||||
printf("请检查程序是否具有足够的写入权限或磁盘空间是否充足。\n");
|
printf("????????????????????????????????????\n");
|
||||||
break;
|
break;
|
||||||
case 2: // 文件打开失败
|
case 2: // ????????
|
||||||
printf("\n游戏记录保存失败: 无法在路径 '%s' 创建文件。\n", filename);
|
printf("\n?????????????: ????????? '%s' ?????????\n", filename);
|
||||||
printf("请检查路径是否有效以及程序是否具有写入权限。\n");
|
printf("????????????????????????????????????\n");
|
||||||
break;
|
break;
|
||||||
case 3: // 文件写入失败
|
case 3: // ??????????
|
||||||
printf("\n游戏记录保存失败: 写入文件时发生错误。\n");
|
printf("\n?????????????: ????????????????\n");
|
||||||
printf("请检查磁盘空间是否已满。\n");
|
printf("??????????????????\n");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
printf("\n游戏记录保存失败: 发生未知错误。\n");
|
printf("\n?????????????: ???????????\n");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 悔棋功能实现
|
* @brief ??????????
|
||||||
*
|
*
|
||||||
* @param steps_to_undo 要悔棋的步数
|
* @param steps_to_undo ?????????
|
||||||
* @return true 悔棋成功
|
* @return true ??????
|
||||||
* @return false 悔棋失败(步数不足)
|
* @return false ???????(????????)
|
||||||
*/
|
*/
|
||||||
bool return_move(int steps_to_undo)
|
bool return_move(int steps_to_undo)
|
||||||
{
|
{
|
||||||
@@ -502,65 +502,65 @@ bool return_move(int steps_to_undo)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 评估玩家在整盘棋局中的表现
|
* @brief ???????????????????????
|
||||||
* @param player 要评估的玩家(PLAYER/AI)
|
* @param player ??????????(PLAYER/AI)
|
||||||
* @return int 总分(已考虑方向重复计算)
|
* @return int ???(???????????????)
|
||||||
* @note 评分标准:
|
* @note ??????:
|
||||||
* - 五连:2500
|
* - ????:2500
|
||||||
* - 活四:1000 冲四:500 死四:250
|
* - ????:1000 ????:500 ????:250
|
||||||
* - 活三:250 眠三:100 死三:50
|
* - ????:250 ????:100 ????:50
|
||||||
* - 活二:50 眠二:20 死二:10
|
* - ???:50 ???:20 ????:10
|
||||||
* - 开放单子:10 半开放单子:5 封闭单子:1
|
* - ???????:10 ???????:5 ??????:1
|
||||||
* @note 实现细节:
|
* @note ??????:
|
||||||
* 1. 遍历棋盘所有位置
|
* 1. ????????????????
|
||||||
* 2. 对每个棋子检查四个方向
|
* 2. ??????????????????
|
||||||
* 3. 统计所有连子情况并评分
|
* 3. ????????????????????
|
||||||
* 4. 最终分数除以4(消除方向重复计算影响)
|
* 4. ???????????4(??????????????????)
|
||||||
*/
|
*/
|
||||||
int calculate_step_score(int x, int y, int player)
|
int calculate_step_score(int x, int y, int player)
|
||||||
{
|
{
|
||||||
int step_score = 0;
|
int step_score = 0;
|
||||||
// 检查四个方向
|
// ??????????
|
||||||
for (int k = 0; k < 4; k++)
|
for (int k = 0; k < 4; k++)
|
||||||
{
|
{
|
||||||
DirInfo info = count_specific_direction(x, y, direction[k][0], direction[k][1], player);
|
DirInfo info = count_specific_direction(x, y, direction[k][0], direction[k][1], player);
|
||||||
// 根据连子数评分
|
// ??????????????
|
||||||
switch (info.continuous_chess)
|
switch (info.continuous_chess)
|
||||||
{
|
{
|
||||||
case 5:
|
case 5:
|
||||||
step_score += 2500;
|
step_score += 2500;
|
||||||
break; // 五连
|
break; // ????
|
||||||
case 4:
|
case 4:
|
||||||
if (info.check_start && info.check_end)
|
if (info.check_start && info.check_end)
|
||||||
step_score += 1000; // 活四
|
step_score += 1000; // ????
|
||||||
else if (info.check_start || info.check_end)
|
else if (info.check_start || info.check_end)
|
||||||
step_score += 500; // 冲四
|
step_score += 500; // ????
|
||||||
else
|
else
|
||||||
step_score += 250; // 死四
|
step_score += 250; // ????
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
if (info.check_start && info.check_end)
|
if (info.check_start && info.check_end)
|
||||||
step_score += 250; // 活三
|
step_score += 250; // ????
|
||||||
else if (info.check_start || info.check_end)
|
else if (info.check_start || info.check_end)
|
||||||
step_score += 100; // 眠三
|
step_score += 100; // ????
|
||||||
else
|
else
|
||||||
step_score += 50; // 死三
|
step_score += 50; // ????
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
if (info.check_start && info.check_end)
|
if (info.check_start && info.check_end)
|
||||||
step_score += 50; // 活二
|
step_score += 50; // ???
|
||||||
else if (info.check_start || info.check_end)
|
else if (info.check_start || info.check_end)
|
||||||
step_score += 20; // 眠二
|
step_score += 20; // ???
|
||||||
else
|
else
|
||||||
step_score += 10; // 死二
|
step_score += 10; // ????
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
if (info.check_start && info.check_end)
|
if (info.check_start && info.check_end)
|
||||||
step_score += 10; // 开放单子
|
step_score += 10; // ???????
|
||||||
else if (info.check_start || info.check_end)
|
else if (info.check_start || info.check_end)
|
||||||
step_score += 5; // 半开放单子
|
step_score += 5; // ???????
|
||||||
else
|
else
|
||||||
step_score += 1; // 封闭单子
|
step_score += 1; // ??????
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -568,26 +568,26 @@ int calculate_step_score(int x, int y, int player)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 评估玩家在整盘棋局中的表现
|
* @brief ???????????????????????
|
||||||
* @param player 要评估的玩家(PLAYER/AI)
|
* @param player ??????????(PLAYER/AI)
|
||||||
* @return int 总分(已考虑方向重复计算)
|
* @return int ???(???????????????)
|
||||||
* @note 评分标准:
|
* @note ??????:
|
||||||
* - 五连:2500
|
* - ????:2500
|
||||||
* - 活四:1000 冲四:500 死四:250
|
* - ????:1000 ????:500 ????:250
|
||||||
* - 活三:250 眠三:100 死三:50
|
* - ????:250 ????:100 ????:50
|
||||||
* - 活二:50 眠二:20 死二:10
|
* - ???:50 ???:20 ????:10
|
||||||
* - 开放单子:10 半开放单子:5 封闭单子:1
|
* - ???????:10 ???????:5 ??????:1
|
||||||
* @note 实现细节:
|
* @note ??????:
|
||||||
* 1. 遍历棋盘所有位置
|
* 1. ????????????????
|
||||||
* 2. 对每个棋子检查四个方向
|
* 2. ??????????????????
|
||||||
* 3. 统计所有连子情况并评分
|
* 3. ????????????????????
|
||||||
* 4. 最终分数除以4(消除方向重复计算影响)
|
* 4. ???????????4(??????????????????)
|
||||||
*/
|
*/
|
||||||
int evaluate_performance(int player)
|
int evaluate_performance(int player)
|
||||||
{
|
{
|
||||||
int total_score = 0;
|
int total_score = 0;
|
||||||
|
|
||||||
// 遍历棋盘所有位置
|
// ????????????????
|
||||||
for (int i = 0; i < BOARD_SIZE; i++)
|
for (int i = 0; i < BOARD_SIZE; i++)
|
||||||
{
|
{
|
||||||
for (int j = 0; j < BOARD_SIZE; j++)
|
for (int j = 0; j < BOARD_SIZE; j++)
|
||||||
@@ -598,86 +598,86 @@ int evaluate_performance(int player)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return total_score / 4; // 每个方向都计算了,需要除以4
|
return total_score / 4; // ????????????????????4
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 将当前游戏记录保存到文件
|
* @brief ???????????????????
|
||||||
* @param filename 要保存的文件名
|
* @param filename ???????????
|
||||||
* @return int 错误码:
|
* @return int ??????:
|
||||||
* 0: 成功
|
* 0: ???
|
||||||
* 1: 目录创建失败
|
* 1: ?????????
|
||||||
* 2: 文件打开失败
|
* 2: ????????
|
||||||
* 3: 文件写入失败
|
* 3: ??????????
|
||||||
*/
|
*/
|
||||||
int save_game_to_file(const char *filename, int game_mode)
|
int save_game_to_file(const char *filename, int game_mode)
|
||||||
{
|
{
|
||||||
// 创建records目录(如果不存在)
|
// ????records??(?????????)
|
||||||
struct stat st = {0};
|
struct stat st = {0};
|
||||||
if (stat("records", &st) == -1)
|
if (stat("records", &st) == -1)
|
||||||
{
|
{
|
||||||
if (mkdir("records") != 0)
|
if (mkdir("records") != 0)
|
||||||
{
|
{
|
||||||
// 检查是否目录已存在(多线程情况下可能被其他线程创建)
|
// ?????????????(????????????????????????)
|
||||||
if (stat("records", &st) == -1)
|
if (stat("records", &st) == -1)
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
printf("错误:无法创建records目录\n");
|
printf("???????????records??\n");
|
||||||
printf("可能原因:\n");
|
printf("???????\n");
|
||||||
printf("1. 没有写入权限 - 请尝试以管理员身份运行\n");
|
printf("1. ?????????? - ??????????????????\n");
|
||||||
printf("2. 防病毒软件阻止 - 请检查安全软件设置\n");
|
printf("2. ????????????? - ??????????????\n");
|
||||||
printf("3. 路径无效 - 请检查工作目录\n");
|
printf("3. ???????? - ?????????\n");
|
||||||
#else
|
#else
|
||||||
perror("创建目录失败");
|
perror("?????????");
|
||||||
#endif
|
#endif
|
||||||
return 1; // 目录创建失败
|
return 1; // ?????????
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 打开文件
|
// ?????
|
||||||
char fullpath[256];
|
char fullpath[256];
|
||||||
snprintf(fullpath, sizeof(fullpath), "records/%s", filename);
|
snprintf(fullpath, sizeof(fullpath), "records/%s", filename);
|
||||||
FILE *file = fopen(fullpath, "w");
|
FILE *file = fopen(fullpath, "w");
|
||||||
if (!file)
|
if (!file)
|
||||||
{
|
{
|
||||||
return 2; // 文件打开失败
|
return 2; // ????????
|
||||||
}
|
}
|
||||||
|
|
||||||
// 写入游戏模式和棋盘大小
|
// ??????????????????
|
||||||
if (fprintf(file, "%d\n%d\n", game_mode, BOARD_SIZE) < 0)
|
if (fprintf(file, "%d\n%d\n", game_mode, BOARD_SIZE) < 0)
|
||||||
{
|
{
|
||||||
fclose(file);
|
fclose(file);
|
||||||
return 3; // 文件写入失败
|
return 3; // ??????????
|
||||||
}
|
}
|
||||||
|
|
||||||
// 写入所有落子步骤
|
// ???????????????
|
||||||
for (int i = 0; i < step_count; i++)
|
for (int i = 0; i < step_count; i++)
|
||||||
{
|
{
|
||||||
if (fprintf(file, "%d %d %d\n", steps[i].player, steps[i].x, steps[i].y) < 0)
|
if (fprintf(file, "%d %d %d\n", steps[i].player, steps[i].x, steps[i].y) < 0)
|
||||||
{
|
{
|
||||||
fclose(file);
|
fclose(file);
|
||||||
return 3; // 文件写入失败
|
return 3; // ??????????
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fclose(file) != 0)
|
if (fclose(file) != 0)
|
||||||
{
|
{
|
||||||
return 3; // 文件关闭/写入失败
|
return 3; // ??????/???????
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0; // 成功
|
return 0; // ???
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 从文件加载游戏记录
|
* @brief ???????????????
|
||||||
* @param filename 要加载的文件名
|
* @param filename ???????????
|
||||||
* @return true 加载成功
|
* @return true ??????
|
||||||
* @return false 加载失败
|
* @return false ???????
|
||||||
*/
|
*/
|
||||||
int load_game_from_file(const char *filename)
|
int load_game_from_file(const char *filename)
|
||||||
{
|
{
|
||||||
// 打开文件
|
// ?????
|
||||||
char fullpath[256];
|
char fullpath[256];
|
||||||
snprintf(fullpath, sizeof(fullpath), "records/%s", filename);
|
snprintf(fullpath, sizeof(fullpath), "records/%s", filename);
|
||||||
FILE *file = fopen(fullpath, "r");
|
FILE *file = fopen(fullpath, "r");
|
||||||
@@ -686,12 +686,12 @@ int load_game_from_file(const char *filename)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 读取游戏模式和棋盘大小
|
// ?????????????????
|
||||||
int game_mode, size;
|
int game_mode, size;
|
||||||
if (fscanf(file, "%d", &game_mode) != 1 || (game_mode != 1 && game_mode != 2))
|
if (fscanf(file, "%d", &game_mode) != 1 || (game_mode != 1 && game_mode != 2))
|
||||||
{
|
{
|
||||||
fclose(file);
|
fclose(file);
|
||||||
return 0; // 无效的游戏模式
|
return 0; // ???????????
|
||||||
}
|
}
|
||||||
if (fscanf(file, "%d", &size) != 1 || size < 5 || size > MAX_BOARD_SIZE)
|
if (fscanf(file, "%d", &size) != 1 || size < 5 || size > MAX_BOARD_SIZE)
|
||||||
{
|
{
|
||||||
@@ -699,11 +699,11 @@ int load_game_from_file(const char *filename)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 初始化棋盘
|
// ?????????
|
||||||
BOARD_SIZE = size;
|
BOARD_SIZE = size;
|
||||||
empty_board();
|
empty_board();
|
||||||
|
|
||||||
// 读取所有落子步骤
|
// ??????????????
|
||||||
step_count = 0;
|
step_count = 0;
|
||||||
while (fscanf(file, "%d %d %d", &steps[step_count].player, &steps[step_count].x, &steps[step_count].y) == 3)
|
while (fscanf(file, "%d %d %d", &steps[step_count].player, &steps[step_count].x, &steps[step_count].y) == 3)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,21 +1,21 @@
|
|||||||
/**
|
/**
|
||||||
* @file gobang.h
|
* @file gobang.h
|
||||||
* @author 刘航宇(3364451258@qq.com、15236416560@163.com、lhy3364451258@outlook.com)
|
* @author ??????(3364451258@qq.com??15236416560@163.com??lhy3364451258@outlook.com)
|
||||||
* @brief 五子棋游戏头文件
|
* @brief ?????????????
|
||||||
* @version 3.0
|
* @version 3.0
|
||||||
* @date 2025-06-30
|
* @date 2025-06-30
|
||||||
*
|
*
|
||||||
* @copyright Copyright (c) 2025
|
* @copyright Copyright (c) 2025
|
||||||
*
|
*
|
||||||
* @note 本文件为gobang.c的接口文件,提供游戏所需的所有函数声明
|
* @note ??????gobang.c???????????????????????????????
|
||||||
* @note 设计要点:
|
* @note ??????
|
||||||
* 1. 支持5x5到25x25的可变棋盘尺寸
|
* 1. ???5x5??25x25??????????
|
||||||
* 2. 使用极小极大算法实现AI决策
|
* 2. ????????????????AI????
|
||||||
* 3. 提供完整的游戏过程复盘功能
|
* 3. ?????????????????????
|
||||||
* 4. 所有坐标采用0-base索引
|
* 4. ???????????0-base????
|
||||||
* 5. 使用全局变量简化状态管理
|
* 5. ?????????????????
|
||||||
* 6. 实现了非阻塞的超时检测,超时后能立刻结束回合
|
* 6. ??????????????????????????????????
|
||||||
* 7. 计时单位为分钟,方便用户设置
|
* 7. ????????????????????????
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef GO_BANG_H
|
#ifndef GO_BANG_H
|
||||||
@@ -28,205 +28,205 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
// 宏定义
|
// ????
|
||||||
/**
|
/**
|
||||||
* @brief 最大支持棋盘尺寸
|
* @brief ????????????
|
||||||
* @note 25x25是性能与实用性的平衡点,更大的棋盘会显著降低AI响应速度
|
* @note 25x25????????????????????????????????????AI??????
|
||||||
*/
|
*/
|
||||||
#define MAX_BOARD_SIZE 25 // 最大支持棋盘尺寸(5x5到25x25)
|
#define MAX_BOARD_SIZE 25 // ????????????(5x5??25x25)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 玩家标识符
|
* @brief ???????
|
||||||
* @note 使用1/2而非字符标识,便于扩展为多玩家游戏
|
* @note ???1/2????????????????????????????
|
||||||
*/
|
*/
|
||||||
#define PLAYER 1 // 玩家棋子标识符
|
#define PLAYER 1 // ???????????
|
||||||
#define AI 2 // AI棋子标识符
|
#define AI 2 // AI????????
|
||||||
#define PLAYER1 1 // 玩家1棋子标识符
|
#define PLAYER1 1 // ???1????????
|
||||||
#define PLAYER2 2 // 玩家2棋子标识符
|
#define PLAYER2 2 // ???2????????
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 空位置标识符
|
* @brief ??????????
|
||||||
* @note 必须与PLAYER/AI的值不同
|
* @note ??????PLAYER/AI??????
|
||||||
*/
|
*/
|
||||||
#define EMPTY 0 // 空位置标识符
|
#define EMPTY 0 // ??????????
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 最大步数限制
|
* @brief ?????????
|
||||||
* @note 等于棋盘总格数,确保不会数组越界
|
* @note ?????????????????????????????
|
||||||
*/
|
*/
|
||||||
#define MAX_STEPS (MAX_BOARD_SIZE * MAX_BOARD_SIZE) // 最大步数(棋盘总格数)
|
#define MAX_STEPS (MAX_BOARD_SIZE * MAX_BOARD_SIZE) // ??????????????????
|
||||||
|
|
||||||
// 全局变量声明
|
// ??????????
|
||||||
extern int BOARD_SIZE; // 实际使用的棋盘尺寸(默认15)
|
extern int BOARD_SIZE; // ?????????????(???15)
|
||||||
extern int board[MAX_BOARD_SIZE][MAX_BOARD_SIZE]; // 棋盘状态存储数组
|
extern int board[MAX_BOARD_SIZE][MAX_BOARD_SIZE]; // ?????????????
|
||||||
extern int step_count; // 当前步数计数器
|
extern int step_count; // ?????????????
|
||||||
extern bool use_forbidden_moves; // 是否启用禁手规则
|
extern bool use_forbidden_moves; // ?????????????
|
||||||
extern int use_timer; // 是否启用计时器
|
extern int use_timer; // ????????????
|
||||||
extern int time_limit; // 时间限制(秒)
|
extern int time_limit; // ??????????
|
||||||
extern const int direction[4][2];
|
extern const int direction[4][2];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 落子步骤记录结构体
|
* @brief ?????????????
|
||||||
* @note 用于存储游戏历史记录和AI决策树
|
* @note ?????????????????AI??????
|
||||||
* @note 字段说明:
|
* @note ??????:
|
||||||
* - player: 标识落子方(PLAYER/AI)
|
* - player: ????????(PLAYER/AI)
|
||||||
* - x/y: 0-based坐标位置
|
* - x/y: 0-based????????
|
||||||
*/
|
*/
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
int player; // 落子方标识
|
int player; // ????????
|
||||||
int x, y; // 坐标位置
|
int x, y; // ????????
|
||||||
} Step;
|
} Step;
|
||||||
|
|
||||||
extern Step steps[MAX_STEPS]; // 存储所有落子步骤的数组
|
extern Step steps[MAX_STEPS]; // ???????????????????
|
||||||
|
|
||||||
bool handle_player_turn(int current_player);
|
bool handle_player_turn(int current_player);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 连子检测信息结构体
|
* @brief ?????????????
|
||||||
* @note 用于五子连珠判断和棋局评估
|
* @note ????????????????????????
|
||||||
* @note 字段说明:
|
* @note ??????:
|
||||||
* - continuous_chess: 连续同色棋子数量
|
* - continuous_chess: ??????????????
|
||||||
* - check_start: 序列起点方向是否有空位(可发展性)
|
* - check_start: ??????????????????(??????)
|
||||||
* - check_end: 序列终点方向是否有空位(可发展性)
|
* - check_end: ??????????????????(??????)
|
||||||
*/
|
*/
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
int continuous_chess; // 连续棋子数量
|
int continuous_chess; // ????????????
|
||||||
bool check_start; // 序列起点方向是否开放(空位)
|
bool check_start; // ????????????????????
|
||||||
bool check_end; // 序列终点方向是否开放(空位)
|
bool check_end; // ????????????????????
|
||||||
} DirInfo;
|
} DirInfo;
|
||||||
|
|
||||||
// 函数声明
|
// ????????
|
||||||
/**
|
/**
|
||||||
* @brief 初始化棋盘为全空状态
|
* @brief ???????????????
|
||||||
*/
|
*/
|
||||||
void empty_board();
|
void empty_board();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 打印当前棋盘状态
|
* @brief ????????????
|
||||||
*/
|
*/
|
||||||
void print_board();
|
void print_board();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 检查指定位置是否为空且有效
|
* @brief ??????????????????????
|
||||||
* @param x 行坐标(0-base)
|
* @param x ??????(0-base)
|
||||||
* @param y 列坐标(0-base)
|
* @param y ??????(0-base)
|
||||||
* @return true 位置有效且为空
|
* @return true ?????????????
|
||||||
* @return false 位置无效或已被占用
|
* @return false ????????????????
|
||||||
*/
|
*/
|
||||||
bool have_space(int x, int y);
|
bool have_space(int x, int y);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 设置棋盘大小
|
* @brief ???????????
|
||||||
*/
|
*/
|
||||||
void setup_board_size();
|
void setup_board_size();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 设置游戏选项
|
* @brief ??????????
|
||||||
* @note 包括禁手规则、计时器和时间限制
|
* @note ????????????????????????
|
||||||
*/
|
*/
|
||||||
void setup_game_options();
|
void setup_game_options();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 决定先手玩家
|
* @brief ???????????
|
||||||
* @param player1 玩家1的标识
|
* @param player1 ???1????
|
||||||
* @param player2 玩家2的标识
|
* @param player2 ???2????
|
||||||
* @return int 先手玩家的标识
|
* @return int ??????????
|
||||||
*/
|
*/
|
||||||
int determine_first_player(int player1, int player2);
|
int determine_first_player(int player1, int player2);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 检查是否为禁手
|
* @brief ???????????
|
||||||
* @param x 行坐标(0-base)
|
* @param x ??????(0-base)
|
||||||
* @param y 列坐标(0-base)
|
* @param y ??????(0-base)
|
||||||
* @param player 玩家标识
|
* @param player ?????
|
||||||
* @return true 是禁手
|
* @return true ?????
|
||||||
* @return false 不是禁手
|
* @return false ???????
|
||||||
*/
|
*/
|
||||||
bool is_forbidden_move(int x, int y, int player);
|
bool is_forbidden_move(int x, int y, int player);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 玩家落子操作
|
* @brief ??????????
|
||||||
* @param x 行坐标(0-base)
|
* @param x ??????(0-base)
|
||||||
* @param y 列坐标(0-base)
|
* @param y ??????(0-base)
|
||||||
* @return true 落子成功
|
* @return true ??????
|
||||||
* @return false 落子失败(位置无效)
|
* @return false ???????(????????)
|
||||||
*/
|
*/
|
||||||
bool player_move(int x, int y, int player);
|
bool player_move(int x, int y, int player);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 计算特定方向上连续同色棋子数量
|
* @brief ???????????????????????????
|
||||||
* @param x 起始行坐标
|
* @param x ?????????
|
||||||
* @param y 起始列坐标
|
* @param y ?????????
|
||||||
* @param dx 行方向增量(-1,0,1)
|
* @param dx ??????????(-1,0,1)
|
||||||
* @param dy 列方向增量(-1,0,1)
|
* @param dy ??????????(-1,0,1)
|
||||||
* @param player 玩家标识(PLAYER/AI)
|
* @param player ?????(PLAYER/AI)
|
||||||
* @return DirInfo 包含连续棋子数和方向开放状态的结构体
|
* @return DirInfo ???????????????????????????
|
||||||
*/
|
*/
|
||||||
DirInfo count_specific_direction(int x, int y, int dx, int dy, int player);
|
DirInfo count_specific_direction(int x, int y, int dx, int dy, int player);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 检查特定位置落子后是否形成五连珠
|
* @brief ????????????????????????????
|
||||||
* @param x 行坐标(0-base)
|
* @param x ??????(0-base)
|
||||||
* @param y 列坐标(0-base)
|
* @param y ??????(0-base)
|
||||||
* @param player 玩家标识(PLAYER/AI)
|
* @param player ?????(PLAYER/AI)
|
||||||
* @return true 形成五连珠
|
* @return true ??????????
|
||||||
* @return false 未形成五连珠
|
* @return false ????????????
|
||||||
*/
|
*/
|
||||||
bool check_win(int x, int y, int player);
|
bool check_win(int x, int y, int player);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 悔棋功能实现
|
* @brief ??????????
|
||||||
* @return true 悔棋成功
|
* @return true ??????
|
||||||
* @return false 悔棋失败(步数不足)
|
* @return false ???????(????????)
|
||||||
* @note 会撤销玩家和AI的最后一步操作
|
* @note ????????AI????????????
|
||||||
*/
|
*/
|
||||||
bool return_move(int steps_to_undo);
|
bool return_move(int steps_to_undo);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 复盘游戏过程
|
* @brief ???????????
|
||||||
* 逐步重现游戏中的所有落子步骤
|
* ????????????????????????
|
||||||
*/
|
*/
|
||||||
void review_process(int game_mode);
|
void review_process(int game_mode);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 评估玩家在整盘棋局中的表现
|
* @brief ???????????????????????
|
||||||
* @param player 要评估的玩家(PLAYER/AI)
|
* @param player ??????????(PLAYER/AI)
|
||||||
* @return int 总分(已考虑方向重复计算)
|
* @return int ???(???????????????)
|
||||||
*/
|
*/
|
||||||
int evaluate_performance(int player);
|
int evaluate_performance(int player);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 计算一步棋的得分
|
* @brief ?????????????
|
||||||
* @param x 行坐标
|
* @param x ??????
|
||||||
* @param y 列坐标
|
* @param y ??????
|
||||||
* @param player 玩家标识
|
* @param player ?????
|
||||||
* @return int 一步棋的得分
|
* @return int ?????????
|
||||||
*/
|
*/
|
||||||
int calculate_step_score(int x, int y, int player);
|
int calculate_step_score(int x, int y, int player);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 将当前游戏记录保存到文件
|
* @brief ???????????????????
|
||||||
* @param filename 要保存的文件名
|
* @param filename ???????????
|
||||||
* @return int 错误码:
|
* @return int ??????:
|
||||||
* 0: 成功
|
* 0: ???
|
||||||
* 1: 目录创建失败
|
* 1: ?????????
|
||||||
* 2: 文件打开失败
|
* 2: ????????
|
||||||
* 3: 文件写入失败
|
* 3: ??????????
|
||||||
*/
|
*/
|
||||||
int save_game_to_file(const char *filename, int game_mode);
|
int save_game_to_file(const char *filename, int game_mode);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 处理游戏结束后的记录保存
|
* @brief ????????????????????
|
||||||
*/
|
*/
|
||||||
void handle_save_record(int game_mode);
|
void handle_save_record(int game_mode);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 从文件加载游戏记录
|
* @brief ???????????????
|
||||||
* @param filename 要加载的文件名
|
* @param filename ???????????
|
||||||
* @return true 加载成功
|
* @return true ??????
|
||||||
* @return false 加载失败
|
* @return false ???????
|
||||||
*/
|
*/
|
||||||
int load_game_from_file(const char *filename);
|
int load_game_from_file(const char *filename);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user