From 205e943bcbe215be1c80b77226feeadee72ba873 Mon Sep 17 00:00:00 2001 From: LHY20 <3364451258@qq.com> Date: Mon, 22 Sep 2025 22:53:14 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E9=A1=B9=E7=9B=AE=E7=BB=93?= =?UTF-8?q?=E6=9E=84=EF=BC=9A=E5=B0=86=E6=BA=90=E6=96=87=E4=BB=B6=E5=92=8C?= =?UTF-8?q?=E5=A4=B4=E6=96=87=E4=BB=B6=E5=88=86=E5=88=AB=E7=A7=BB=E5=8A=A8?= =?UTF-8?q?=E5=88=B0src=E5=92=8Cinclude=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MD/AI_Enhancement_Guide.md | 568 +++++---------------------- Makefile | 12 +- compile.bat | 8 +- ai.h => include/ai.h | 0 config.h => include/config.h | 0 game_mode.h => include/game_mode.h | 17 +- globals.h => include/globals.h | 0 gobang.h => include/gobang.h | 0 gui.h => include/gui.h | 0 init_board.h => include/init_board.h | 0 network.h => include/network.h | 0 record.h => include/record.h | 0 type.h => include/type.h | 0 ui.h => include/ui.h | 0 ai.c => src/ai.c | 0 config.c => src/config.c | 0 game_mode.c => src/game_mode.c | 46 +++ globals.c => src/globals.c | 10 +- gobang.c => src/gobang.c | 0 gui.c => src/gui.c | 0 init_board.c => src/init_board.c | 0 main.c => src/main.c | 28 +- network.c => src/network.c | 0 record.c => src/record.c | 0 ui.c => src/ui.c | 0 25 files changed, 185 insertions(+), 504 deletions(-) rename ai.h => include/ai.h (100%) rename config.h => include/config.h (100%) rename game_mode.h => include/game_mode.h (89%) rename globals.h => include/globals.h (100%) rename gobang.h => include/gobang.h (100%) rename gui.h => include/gui.h (100%) rename init_board.h => include/init_board.h (100%) rename network.h => include/network.h (100%) rename record.h => include/record.h (100%) rename type.h => include/type.h (100%) rename ui.h => include/ui.h (100%) rename ai.c => src/ai.c (100%) rename config.c => src/config.c (100%) rename game_mode.c => src/game_mode.c (96%) rename globals.c => src/globals.c (88%) rename gobang.c => src/gobang.c (100%) rename gui.c => src/gui.c (100%) rename init_board.c => src/init_board.c (100%) rename main.c => src/main.c (71%) rename network.c => src/network.c (100%) rename record.c => src/record.c (100%) rename ui.c => src/ui.c (100%) diff --git a/MD/AI_Enhancement_Guide.md b/MD/AI_Enhancement_Guide.md index 6684e42..1b6a793 100644 --- a/MD/AI_Enhancement_Guide.md +++ b/MD/AI_Enhancement_Guide.md @@ -1,463 +1,105 @@ -# 🚀 五子棋AI增强指南 (v8.0) - -## 📋 概述 - -本文档详细介绍了提升五子棋AI水平的各种方法和实现策略,基于当前项目的代码架构(v8.0),提供从简单参数调优到复杂算法改进的完整方案。v8.0版本新增了GUI界面支持,为AI可视化分析和用户交互提供了更好的平台。 - -## 🎯 当前AI分析 - -### 现有优势 -- ✅ 模块化设计良好 -- ✅ 基础Minimax + α-β剪枝算法 -- ✅ 完整的棋型评估系统 -- ✅ 威胁检测机制 -- ✅ 防御优先策略 -- ✅ SDL3图形化界面支持AI可视化分析 -- ✅ 双版本架构(控制台+GUI)提供更好的调试环境 - -### 改进空间 -- 🔄 搜索深度有限(当前3层) -- 🔄 评估函数相对简单 -- 🔄 缺乏开局库和残局库 -- 🔄 没有学习机制 -- 🔄 搜索效率可优化 - -## 🛠️ 改进方案 - -### 1. 立即可实施的改进(难度:⭐) - -#### 1.1 参数调优 - -**修改 `config.h` 中的关键参数:** - -```c -// 增加搜索深度 -#define DEFAULT_AI_DEPTH 5 // 从3提升到5 - -// 优化防守系数 -#define DEFAULT_DEFENSE_COEFFICIENT 1.5 // 从1.2提升到1.5 - -// 扩大搜索范围 -#define AI_NEARBY_RANGE 3 // 从2扩大到3 - -// 降低搜索范围限制阈值 -#define AI_SEARCH_RANGE_THRESHOLD 8 // 从10降低到8 -``` - -#### 1.2 评分系统优化 - -**在 `config.h` 中添加新的评分项:** - -```c -// 组合棋型评分 -#define AI_SCORE_DOUBLE_THREE 50000 // 双三 -#define AI_SCORE_FOUR_THREE 200000 // 四三 -#define AI_SCORE_THREAT_SEQUENCE 80000 // 威胁序列 -#define AI_SCORE_POTENTIAL_FIVE 300000 // 潜在五连 -``` - -### 2. 短期改进(1-2周,难度:⭐⭐) - -#### 2.1 移动排序优化 - -**在 `ai.c` 中添加移动排序函数:** - -```c -// 移动排序结构 -typedef struct { - int x, y; - int score; -} ScoredMove; - -// 移动排序函数 -int compare_moves(const void *a, const void *b) { - ScoredMove *moveA = (ScoredMove*)a; - ScoredMove *moveB = (ScoredMove*)b; - return moveB->score - moveA->score; -} - -// 生成并排序候选移动 -int generate_candidate_moves(ScoredMove *moves, int player) { - int count = 0; - for (int i = 0; i < BOARD_SIZE; i++) { - for (int j = 0; j < BOARD_SIZE; j++) { - if (board[i][j] == EMPTY && is_near_stones(i, j)) { - moves[count].x = i; - moves[count].y = j; - moves[count].score = evaluate_move(i, j); - count++; - } - } - } - qsort(moves, count, sizeof(ScoredMove), compare_moves); - return count; -} -``` - -#### 2.2 威胁检测增强 - -**添加多层次威胁检测:** - -```c -// 威胁类型枚举 -typedef enum { - THREAT_NONE = 0, - THREAT_WIN = 5, // 直接获胜 - THREAT_FOUR = 4, // 活四/冲四 - THREAT_THREE = 3, // 活三 - THREAT_DOUBLE = 2, // 双威胁 - THREAT_POTENTIAL = 1 // 潜在威胁 -} ThreatLevel; - -// 威胁检测函数 -ThreatLevel detect_threat(int x, int y, int player) { - // 实现威胁检测逻辑 - // ... -} -``` - -### 3. 中期改进(1-2月,难度:⭐⭐⭐) - -#### 3.1 置换表实现 - -**添加置换表缓存系统:** - -```c -// 置换表项 -typedef struct { - uint64_t hash_key; // 棋盘哈希值 - int score; // 评估分数 - int depth; // 搜索深度 - int best_x, best_y; // 最佳移动 - int flag; // 节点类型(精确值/上界/下界) -} TranspositionEntry; - -// 置换表 -#define TT_SIZE 1048576 // 2^20 -TranspositionEntry transposition_table[TT_SIZE]; - -// 棋盘哈希函数 -uint64_t zobrist_hash() { - // 实现Zobrist哈希 - // ... -} -``` - -#### 3.2 开局库系统 - -**创建开局库数据结构:** - -```c -// 开局库项 -typedef struct { - int moves[20][2]; // 开局移动序列 - int move_count; // 移动数量 - int win_rate; // 胜率 - char name[50]; // 开局名称 -} OpeningEntry; - -// 开局库 -OpeningEntry opening_book[] = { - // 天元开局 - {{{7,7}, {6,6}, {8,8}, {6,8}, {8,6}}, 5, 65, "天元开局"}, - // 花月开局 - {{{7,7}, {6,7}, {8,7}, {7,6}, {7,8}}, 5, 70, "花月开局"}, - // 更多开局... -}; -``` - -#### 3.3 改进的评估函数 - -**实现更复杂的位置评估:** - -```c -// 高级评估函数 -int advanced_evaluate_pos(int x, int y, int player) { - int score = 0; - - // 1. 基础棋型评分 - score += basic_pattern_score(x, y, player); - - // 2. 组合棋型评分 - score += combination_pattern_score(x, y, player); - - // 3. 位置价值评分 - score += positional_value_score(x, y); - - // 4. 威胁序列评分 - score += threat_sequence_score(x, y, player); - - // 5. 防守价值评分 - score += defensive_value_score(x, y, player); - - return score; -} -``` - -### 4. 长期改进(3-6月,难度:⭐⭐⭐⭐) - -#### 4.1 蒙特卡洛树搜索(MCTS) - -**MCTS节点结构:** - -```c -// MCTS节点 -typedef struct MCTSNode { - int x, y; // 移动位置 - int visits; // 访问次数 - double wins; // 胜利次数 - struct MCTSNode *parent; // 父节点 - struct MCTSNode **children; // 子节点数组 - int child_count; // 子节点数量 - bool fully_expanded; // 是否完全展开 -} MCTSNode; - -// MCTS主函数 -int mcts_search(int simulations) { - MCTSNode *root = create_node(-1, -1, NULL); - - for (int i = 0; i < simulations; i++) { - MCTSNode *node = selection(root); - node = expansion(node); - double result = simulation(node); - backpropagation(node, result); - } - - return best_child(root); -} -``` - -#### 4.2 神经网络集成 - -**神经网络评估接口:** - -```c -// 神经网络评估函数 -float neural_network_evaluate(int board[BOARD_SIZE][BOARD_SIZE]) { - // 调用训练好的神经网络模型 - // 返回位置评估值 - // ... -} - -// 混合评估函数 -int hybrid_evaluate(int x, int y, int player) { - // 传统评估 - int traditional_score = evaluate_pos(x, y, player); - - // 神经网络评估 - float nn_score = neural_network_evaluate(board); - - // 加权组合 - return (int)(0.7 * traditional_score + 0.3 * nn_score * 10000); -} -``` - -## 📊 性能优化策略 - -### 1. 搜索优化 - -#### 1.1 迭代加深搜索 -```c -int iterative_deepening_search(int max_depth, int time_limit) { - int best_move = -1; - clock_t start_time = clock(); - - for (int depth = 1; depth <= max_depth; depth++) { - if ((clock() - start_time) * 1000 / CLOCKS_PER_SEC > time_limit) { - break; - } - best_move = alpha_beta_search(depth); - } - - return best_move; -} -``` - -#### 1.2 空窗搜索 -```c -int null_window_search(int depth, int beta) { - return alpha_beta_search(depth, beta - 1, beta); -} -``` - -### 2. 内存优化 - -#### 2.1 位棋盘表示 -```c -// 使用位操作优化棋盘表示 -typedef struct { - uint64_t player1_board[4]; // 玩家1的棋盘(4个64位整数) - uint64_t player2_board[4]; // 玩家2的棋盘 -} BitBoard; -``` - -## 🎮 实战策略 - -### 1. 自适应难度系统 - -```c -// 难度等级 -typedef enum { - DIFFICULTY_EASY = 1, - DIFFICULTY_NORMAL = 2, - DIFFICULTY_HARD = 3, - DIFFICULTY_EXPERT = 4, - DIFFICULTY_MASTER = 5 -} DifficultyLevel; - -// 根据难度调整AI参数 -void adjust_ai_parameters(DifficultyLevel level) { - switch (level) { - case DIFFICULTY_EASY: - ai_depth = 2; - defense_coefficient = 1.0; - break; - case DIFFICULTY_NORMAL: - ai_depth = 3; - defense_coefficient = 1.2; - break; - case DIFFICULTY_HARD: - ai_depth = 4; - defense_coefficient = 1.5; - break; - // ... - } -} -``` - -### 2. 学习系统 - -```c -// 对局记录 -typedef struct { - int moves[MAX_STEPS][2]; - int move_count; - int winner; - int ai_mistakes; - float game_quality; -} GameRecord; - -// 学习函数 -void learn_from_game(GameRecord *record) { - // 分析对局,更新评估参数 - // 识别AI的错误决策 - // 调整相关参数 -} -``` - -## 📈 测试与评估 - -### 1. 性能测试 - -```c -// 性能测试函数 -void performance_test() { - clock_t start = clock(); - - // 执行1000次AI决策 - for (int i = 0; i < 1000; i++) { - ai_move(DEFAULT_AI_DEPTH); - // 重置棋盘 - } - - clock_t end = clock(); - double time_taken = ((double)(end - start)) / CLOCKS_PER_SEC; - printf("平均决策时间: %.2f ms\n", time_taken); -} -``` - -### 2. 棋力测试 - -```c -// 自我对弈测试 -int self_play_test(int games) { - int wins = 0; - - for (int i = 0; i < games; i++) { - // AI vs AI(不同版本或参数) - int result = play_game(); - if (result == 1) wins++; - } - - return (wins * 100) / games; // 胜率 -} -``` - -## 🚀 实施路线图 - -### 阶段1:基础优化(1周) -- [ ] 调整搜索深度和评分参数 -- [ ] 优化移动排序 -- [ ] 增强威胁检测 - -### 阶段2:算法改进(2-4周) -- [ ] 实现置换表 -- [ ] 添加开局库 -- [ ] 改进评估函数 - -### 阶段3:高级功能(1-2月) -- [ ] 实现MCTS -- [ ] 添加学习机制 -- [ ] 性能优化 - -### 阶段4:AI增强(2-3月) -- [ ] 神经网络集成 -- [ ] 并行搜索 -- [ ] 完整的自适应系统 - -## 📚 参考资源 - -### 算法资料 -- 《人工智能:一种现代方法》- Stuart Russell -- 《游戏编程中的人工智能技术》- Mat Buckland -- AlphaGo论文系列 - -### 开源项目 -- Stockfish(国际象棋引擎) -- Leela Zero(围棋AI) -- Gomoku AI项目 - -### 在线资源 -- Chess Programming Wiki -- Computer Olympiad -- AI游戏编程社区 - -## 🏗️ 代码架构优化 (v7.0新增) - -### 配置管理统一化 - -在v7.0版本中,我们完成了重要的代码架构重构: - -#### 配置参数集中管理 -- **统一配置文件**:所有AI相关参数现在集中在`config.h`中定义 -- **参数分类管理**:AI参数按功能分组(搜索深度、评分权重、时间限制等) -- **配置文件支持**:AI参数可通过`gobang_config.ini`文件动态调整 -- **运行时修改**:支持游戏过程中实时调整AI难度和参数 - -#### 代码模块化优化 -- **清晰的模块分离**:AI逻辑与游戏逻辑完全分离 -- **接口标准化**:统一的AI接口设计,便于算法替换和升级 -- **全局变量管理**:AI相关全局变量集中在`globals`模块中 -- **类型定义统一**:所有数据结构定义集中在`type.h`中 - -#### 维护性提升 -- **宏定义优化**:消除重复定义,提高代码一致性 -- **注释规范化**:完善的代码注释和文档 -- **错误处理统一**:标准化的错误处理机制 -- **调试支持增强**:更好的调试信息和日志记录 - -这些架构优化为后续的AI算法改进奠定了坚实的基础,使得实施复杂的AI增强方案变得更加容易和可靠。 - -## 💡 总结 - -通过系统性的改进,可以将当前的五子棋AI从业余水平提升到接近专业水平。关键是要循序渐进,先实施简单的改进,再逐步引入复杂的算法。每个阶段都要进行充分的测试和评估,确保改进的有效性。 - -建议按照优先级顺序实施: - -1. **短期目标**:参数调优 + 开局库 + 棋型优化 -2. **中期目标**:搜索算法优化 + 评估函数改进 -3. **长期目标**:机器学习集成 + MCTS实现 - -v7.0的架构重构为所有这些改进提供了更好的代码基础。 - -记住:**好的AI不仅要算得深,更要算得准!** \ No newline at end of file +## 🎯 项目优势 +### ✅ 已经做得很好的方面 +- 📚 代码质量优秀 :注释覆盖率30.1%,使用Doxygen格式文档 +- 🏗️ 架构设计合理 :模块化设计,职责分离清晰 +- 🎮 功能完整丰富 :支持人机对战、双人对战、网络对战、GUI界面 +- 🔧 技术栈现代化 :SDL3图形库、双版本架构、专业安装包 +- 📋 文档完善 :详细的README、AI增强指南、架构重构指南 +- 🌐 跨平台考虑 :已有条件编译支持Windows/Linux +## 🚀 改进建议 +### 1. 🧪 测试体系建设(高优先级) +当前状况 :项目缺乏自动化测试 + +建议改进 : + +- 添加单元测试框架(如Unity或自制简单测试框架) +- 为核心模块编写测试用例:AI算法、棋盘逻辑、网络通信 +- 添加集成测试验证各模块协作 +- 创建性能基准测试 +### 2. 🛡️ 错误处理和安全性增强(高优先级) +当前状况 :基础错误处理存在,但可以更完善 + +建议改进 : + +- 增强输入验证,防止缓冲区溢出 +- 添加网络通信的安全验证机制 +- 完善内存管理,添加内存泄漏检测 +- 增加异常恢复机制 +### 3. 🎯 AI算法优化(中优先级) +当前状况 :基础Minimax+α-β剪枝,搜索深度3层 + +建议改进 : + +- 实现置换表缓存系统 +- 添加开局库和残局库 +- 实现迭代加深搜索 +- 增加威胁检测优化 +- 支持多线程并行搜索 +### 4. 🔧 构建和部署优化(中优先级) +当前状况 :编译脚本功能完善,但可以更自动化 + +建议改进 : + +- 添加CMake构建系统支持 +- 实现持续集成/持续部署(CI/CD) +- 添加依赖管理和版本控制 +- 完善跨平台构建脚本 +### 5. 📊 性能监控和分析(中优先级) +建议添加 : + +- AI决策时间统计 +- 内存使用监控 +- 网络延迟分析 +- 性能瓶颈识别工具 +### 6. 🎮 用户体验提升(低优先级) +建议改进 : + +- 添加音效和动画效果 +- 实现主题和皮肤系统 +- 增加游戏统计和成就系统 +- 支持自定义快捷键 +### 7. 🌐 网络功能增强(低优先级) +建议改进 : + +- 实现房间系统和匹配机制 +- 添加观战功能 +- 支持断线重连 +- 实现聊天系统 +## 📋 具体实施建议 +### 立即可实施(1-2周) +1. 1. + 创建基础测试框架 +2. 2. + 添加输入验证和边界检查 +3. 3. + 完善错误日志记录 +4. 4. + 优化编译警告处理 +### 短期目标(1-2月) +1. 1. + 完善单元测试覆盖 +2. 2. + 实现置换表和开局库 +3. 3. + 添加性能监控 +4. 4. + 增强网络安全性 +### 长期规划(3-6月) +1. 1. + 实现神经网络AI +2. 2. + 完整跨平台支持 +3. 3. + 添加数据库支持 +4. 4. + 实现云端对战 +## 🏆 总体评价 +你的项目已经达到了 专业级别的开源项目标准 !代码架构清晰、功能完整、文档详尽。主要的改进空间在于测试覆盖和一些高级特性的添加。 + +项目亮点 : + +- 从v7.0的架构重构到v8.2的编译优化,版本迭代思路清晰 +- SDL3图形化界面实现现代化 +- 网络对战功能完整 +- 代码注释和文档非常专业 +这是一个非常值得继续完善和推广的优秀项目!🎉 \ No newline at end of file diff --git a/Makefile b/Makefile index 0e0fa29..86b5f61 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ # 编译器设置 CC = gcc -CFLAGS = -Wall -Wextra -std=c17 -O2 +CFLAGS = -Wall -Wextra -std=c17 -O2 -Iinclude LDFLAGS = -lws2_32 # SDL3路径设置 @@ -12,14 +12,14 @@ SDL3_INCLUDE = -I$(SDL3_PATH)/include SDL3_LIBS = -L$(SDL3_PATH)/lib -lSDL3 -lmingw32 # 源文件 -COMMON_SOURCES = main.c gobang.c ai.c config.c game_mode.c globals.c \ - init_board.c network.c record.c ui.c gui.c type.h +COMMON_SOURCES = src/main.c src/gobang.c src/ai.c src/config.c src/game_mode.c src/globals.c \ + src/init_board.c src/network.c src/record.c src/ui.c src/gui.c GUI_SOURCES = $(COMMON_SOURCES) CONSOLE_SOURCES = $(COMMON_SOURCES) # 目标文件 -COMMON_OBJECTS = $(patsubst %.c,%.o,$(filter %.c,$(COMMON_SOURCES))) +COMMON_OBJECTS = $(patsubst src/%.c,src/%.o,$(filter %.c,$(COMMON_SOURCES))) # 可执行文件 CONSOLE_TARGET = gobang_console.exe @@ -37,12 +37,12 @@ $(GUI_TARGET): $(COMMON_OBJECTS) $(CC) $(CFLAGS) $(SDL3_INCLUDE) -o $@ $^ $(SDL3_LIBS) $(LDFLAGS) # 通用目标文件编译规则(包含SDL3头文件路径,因为多个文件包含gui.h) -%.o: %.c +src/%.o: src/%.c $(CC) $(CFLAGS) $(SDL3_INCLUDE) -c -o $@ $< # 清理规则 clean: - del /Q *.o *.exe 2>nul || true + del /Q src\*.o *.exe 2>nul || true # 只编译控制台版本 console: $(CONSOLE_TARGET) diff --git a/compile.bat b/compile.bat index 80aea60..c936aa2 100644 --- a/compile.bat +++ b/compile.bat @@ -21,7 +21,7 @@ exit /b 1 echo. echo Compiling console version... echo. -gcc -std=c17 -o gobang_console.exe *.c -ID:\settings\SDL\SDL3-3.2.22\x86_64-w64-mingw32\include -LD:\settings\SDL\SDL3-3.2.22\x86_64-w64-mingw32\lib -lSDL3 -lws2_32 +gcc -std=c17 -o gobang_console.exe src/*.c -Iinclude -ID:\settings\SDL\SDL3-3.2.22\x86_64-w64-mingw32\include -LD:\settings\SDL\SDL3-3.2.22\x86_64-w64-mingw32\lib -lSDL3 -lws2_32 if %ERRORLEVEL% EQU 0 ( echo. echo Compilation successful! Generated: gobang_console.exe @@ -44,7 +44,7 @@ if not exist "D:\settings\SDL\SDL3-3.2.22\x86_64-w64-mingw32\include" ( echo Please ensure SDL3 is extracted to: D:\settings\SDL\SDL3-3.2.22\ goto :end ) -gcc -std=c17 -o gobang_gui.exe *.c -ID:\settings\SDL\SDL3-3.2.22\x86_64-w64-mingw32\include -LD:\settings\SDL\SDL3-3.2.22\x86_64-w64-mingw32\lib -lSDL3 -lws2_32 +gcc -std=c17 -o gobang_gui.exe src/*.c -Iinclude -ID:\settings\SDL\SDL3-3.2.22\x86_64-w64-mingw32\include -LD:\settings\SDL\SDL3-3.2.22\x86_64-w64-mingw32\lib -lSDL3 -lws2_32 if %ERRORLEVEL% EQU 0 ( echo. echo Compilation successful! Generated: gobang_gui.exe @@ -68,7 +68,7 @@ echo. echo Compiling all versions... echo. echo [1/2] Compiling console version... -gcc -std=c17 -o gobang_console.exe *.c -ID:\settings\SDL\SDL3-3.2.22\x86_64-w64-mingw32\include -LD:\settings\SDL\SDL3-3.2.22\x86_64-w64-mingw32\lib -lSDL3 -lws2_32 +gcc -std=c17 -o gobang_console.exe src/*.c -Iinclude -ID:\settings\SDL\SDL3-3.2.22\x86_64-w64-mingw32\include -LD:\settings\SDL\SDL3-3.2.22\x86_64-w64-mingw32\lib -lSDL3 -lws2_32 if %ERRORLEVEL% EQU 0 ( echo Console version compilation successful! ) else ( @@ -80,7 +80,7 @@ if not exist "D:\settings\SDL\SDL3-3.2.22\x86_64-w64-mingw32\include" ( echo Error: SDL3 library not found! Skipping GUI version compilation goto :end ) -gcc -std=c17 -o gobang_gui.exe *.c -ID:\settings\SDL\SDL3-3.2.22\x86_64-w64-mingw32\include -LD:\settings\SDL\SDL3-3.2.22\x86_64-w64-mingw32\lib -lSDL3 -lws2_32 +gcc -std=c17 -o gobang_gui.exe src/*.c -Iinclude -ID:\settings\SDL\SDL3-3.2.22\x86_64-w64-mingw32\include -LD:\settings\SDL\SDL3-3.2.22\x86_64-w64-mingw32\lib -lSDL3 -lws2_32 if %ERRORLEVEL% EQU 0 ( echo GUI version compilation successful! copy "D:\settings\SDL\SDL3-3.2.22\x86_64-w64-mingw32\bin\SDL3.dll" . >nul 2>&1 diff --git a/ai.h b/include/ai.h similarity index 100% rename from ai.h rename to include/ai.h diff --git a/config.h b/include/config.h similarity index 100% rename from config.h rename to include/config.h diff --git a/game_mode.h b/include/game_mode.h similarity index 89% rename from game_mode.h rename to include/game_mode.h index 636aa41..b8a8618 100644 --- a/game_mode.h +++ b/include/game_mode.h @@ -86,8 +86,23 @@ bool handle_network_player_turn(int current_player, bool is_local_turn); /** * @brief 网络游戏主循环 * @return true 游戏正常结束 - * @return false 网络错误或异常退出 + * @return false 网络错误 */ bool network_game_loop(); +/** + * @brief 显示游戏规则 + */ +void show_game_rules(); + +/** + * @brief 显示关于游戏信息 + */ +void show_about_game(); + +/** + * @brief 启动图形化界面 + */ +void run_gui_mode(); + #endif // GAME_MODE_H \ No newline at end of file diff --git a/globals.h b/include/globals.h similarity index 100% rename from globals.h rename to include/globals.h diff --git a/gobang.h b/include/gobang.h similarity index 100% rename from gobang.h rename to include/gobang.h diff --git a/gui.h b/include/gui.h similarity index 100% rename from gui.h rename to include/gui.h diff --git a/init_board.h b/include/init_board.h similarity index 100% rename from init_board.h rename to include/init_board.h diff --git a/network.h b/include/network.h similarity index 100% rename from network.h rename to include/network.h diff --git a/record.h b/include/record.h similarity index 100% rename from record.h rename to include/record.h diff --git a/type.h b/include/type.h similarity index 100% rename from type.h rename to include/type.h diff --git a/ui.h b/include/ui.h similarity index 100% rename from ui.h rename to include/ui.h diff --git a/ai.c b/src/ai.c similarity index 100% rename from ai.c rename to src/ai.c diff --git a/config.c b/src/config.c similarity index 100% rename from config.c rename to src/config.c diff --git a/game_mode.c b/src/game_mode.c similarity index 96% rename from game_mode.c rename to src/game_mode.c index fbb9be7..e57f958 100644 --- a/game_mode.c +++ b/src/game_mode.c @@ -16,6 +16,7 @@ #include "config.h" #include "network.h" #include "ui.h" +#include "gui.h" #include "globals.h" #include #include @@ -706,6 +707,51 @@ void run_network_game() pause_for_input("按任意键返回主菜单..."); } +/** + * @brief 显示游戏规则 + */ +void show_game_rules() +{ + clear_screen(); + display_game_rules(); + pause_for_input("\n按任意键返回主菜单..."); +} + +/** + * @brief 显示关于游戏信息 + */ +void show_about_game() +{ + clear_screen(); + display_about(); + pause_for_input("\n按任意键返回主菜单..."); +} + +/** + * @brief 启动图形化界面 + */ +void run_gui_mode() +{ + if (init_gui() == 0) + { + printf("启动图形化界面...\n"); + printf("图形化界面已启动,窗口应该可见\n"); + printf("如果看不到窗口,请检查任务栏或按Alt+Tab切换\n"); + while (gui_running && handle_events()) + { + render_game(); + SDL_Delay(16); // 约60FPS + } + printf("退出图形化界面\n"); + cleanup_gui(); + } + else + { + printf("图形化界面启动失败!请检查SDL3库是否正确安装。\n"); + pause_for_input("按任意键返回主菜单..."); + } +} + /** * @brief 处理网络玩家回合 */ diff --git a/globals.c b/src/globals.c similarity index 88% rename from globals.c rename to src/globals.c index bff3f45..9007f83 100644 --- a/globals.c +++ b/src/globals.c @@ -31,14 +31,14 @@ NetworkGameState network_state = {0}; // 网络游戏 // ==================== GUI相关变量定义 ==================== SDL_Window* window = NULL; // SDL窗口指针 -SDL_Renderer* renderer = NULL; // SDL渲染器指针 +SDL_Renderer* renderer = NULL; // SDL渲染器指针 int gui_running = 1; // GUI运行状态标志 int current_player_gui = PLAYER; // GUI当前玩家 int game_over = 0; // 游戏结束标志 -char status_message[256] = "五子棋游戏 - 黑子先行"; // 状态消息 +char status_message[256] = "五子棋游戏 - 黑子先行"; // 状态消息 // ==================== 记录相关变量定义 ==================== -int player1_final_score = 0; // 玩家1最终得分 -int player2_final_score = 0; // 玩家2最终得分 +int player1_final_score = 0; // 玩家1最终得分 +int player2_final_score = 0; // 玩家2最终得分 int scores_calculated = 0; // 评分计算标志 -char winner_info[50] = "平局或未完成"; // 存储胜负信息 \ No newline at end of file +char winner_info[50] = "平局或未完成"; // 存储胜负信息 \ No newline at end of file diff --git a/gobang.c b/src/gobang.c similarity index 100% rename from gobang.c rename to src/gobang.c diff --git a/gui.c b/src/gui.c similarity index 100% rename from gui.c rename to src/gui.c diff --git a/init_board.c b/src/init_board.c similarity index 100% rename from init_board.c rename to src/init_board.c diff --git a/main.c b/src/main.c similarity index 71% rename from main.c rename to src/main.c index 577f6d5..936f066 100644 --- a/main.c +++ b/src/main.c @@ -21,7 +21,6 @@ #include "game_mode.h" #include "ui.h" -#include "gui.h" #include "config.h" #include #ifdef _WIN32 @@ -73,36 +72,15 @@ int main(int argc, char *argv[]) break; // 6. 游戏规则 case 6: - clear_screen(); - display_game_rules(); - pause_for_input("\n按任意键返回主菜单..."); + show_game_rules(); break; // 7. 关于游戏 case 7: - clear_screen(); - display_about(); - pause_for_input("\n按任意键返回主菜单..."); + show_about_game(); break; // 8. 图形化界面 case 8: - if (init_gui() == 0) - { - printf("启动图形化界面...\n"); - printf("图形化界面已启动,窗口应该可见\n"); - printf("如果看不到窗口,请检查任务栏或按Alt+Tab切换\n"); - while (gui_running && handle_events()) - { - render_game(); - SDL_Delay(16); // 约60FPS - } - printf("退出图形化界面\n"); - cleanup_gui(); - } - else - { - printf("图形化界面启动失败!请检查SDL3库是否正确安装。\n"); - pause_for_input("按任意键返回主菜单..."); - } + run_gui_mode(); break; // 0. 退出游戏 case 0: diff --git a/network.c b/src/network.c similarity index 100% rename from network.c rename to src/network.c diff --git a/record.c b/src/record.c similarity index 100% rename from record.c rename to src/record.c diff --git a/ui.c b/src/ui.c similarity index 100% rename from ui.c rename to src/ui.c