mirror of
https://github.com/LHY0125/Gobang-Game.git
synced 2026-06-29 00:45:55 +08:00
feat: 集成大模型AI、重构构建系统并修复多项代码质量问题
- 构建系统:Makefile 迁移至 CMakeLists.txt,支持 cJSON 和 WinHTTP - 项目结构:src/ 按功能拆分为 core/、gui/、network/、record/、llm/ 子目录 - 新功能:集成大模型 AI(WinHTTP + cJSON,兼容 OpenAI 协议),支持异步请求 - 渲染修复:IupDraw* 替换为 Windows GDI,修复画布黑屏问题 - 网络修复:ENet 初始化幂等化,实现真实 get_local_ip() (Winsock) - 代码质量:删除死代码 (dfs/count_threats_in_direction),修复头文件守卫, sprintf→snprintf 防溢出,strncpy 安全终止,GDI 资源泄漏修复 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -32,13 +32,6 @@ int evaluate_move(int x, int y);
|
||||
*/
|
||||
int evaluate_pos(int x, int y, int player);
|
||||
|
||||
/**
|
||||
* @brief 评估棋盘价值
|
||||
*
|
||||
* @param player 玩家标识(PLAYER/AI)
|
||||
*/
|
||||
int dfs(int x, int y, int player, int depth, int alpha, int beta, bool is_maximizing);
|
||||
|
||||
/**
|
||||
* @brief AI下棋
|
||||
*
|
||||
@@ -71,13 +64,5 @@ bool is_near_stones(int x, int y);
|
||||
*/
|
||||
ThreatLevel detect_threat(int x, int y, int player);
|
||||
|
||||
/**
|
||||
* @brief 计算指定方向的威胁数量
|
||||
* @param x, y 起始位置
|
||||
* @param dx, dy 方向向量
|
||||
* @param player 玩家
|
||||
* @return 威胁数量
|
||||
*/
|
||||
int count_threats_in_direction(int x, int y, int dx, int dy, int player);
|
||||
|
||||
#endif // AI_H
|
||||
@@ -135,6 +135,17 @@
|
||||
#define CONFIG_FILE "gobang_config.ini" // 配置文件路径
|
||||
#define MAX_PATH_LENGTH 256 // 最大路径长度
|
||||
|
||||
//---------- LLM大模型参数 ----------//
|
||||
#define DEFAULT_LLM_USE 0 // 默认不使用LLM (0=算法AI, 1=大模型)
|
||||
#define DEFAULT_LLM_ENDPOINT "https://api.minimax.chat/v1/chat/completions" // 默认API地址
|
||||
#define DEFAULT_LLM_API_KEY "" // 默认API Key (需用户填写)
|
||||
#define DEFAULT_LLM_MODEL "MiniMax-Text-01" // 默认模型名
|
||||
#define MAX_LLM_ENDPOINT_LEN 256 // API地址最大长度
|
||||
#define MAX_LLM_API_KEY_LEN 128 // API Key最大长度
|
||||
#define MAX_LLM_MODEL_LEN 64 // 模型名最大长度
|
||||
#define LLM_MAX_RETRIES 3 // LLM返回非法坐标时最大重试次数
|
||||
#define LLM_TIMEOUT_MS 30000 // LLM HTTP请求超时(毫秒)
|
||||
|
||||
//---------- 配置管理函数声明 ----------//
|
||||
/**
|
||||
* @brief 加载游戏配置
|
||||
|
||||
@@ -29,6 +29,12 @@ extern int network_timeout; // 网络超时时间
|
||||
extern double defense_coefficient; // 防守系数
|
||||
extern int ai_difficulty; // AI难度 (1-5)
|
||||
|
||||
// ==================== LLM大模型相关变量 ====================
|
||||
extern int llm_use; // 是否使用LLM (0=算法AI, 1=大模型)
|
||||
extern char llm_endpoint[MAX_LLM_ENDPOINT_LEN]; // API地址
|
||||
extern char llm_api_key[MAX_LLM_API_KEY_LEN]; // API Key
|
||||
extern char llm_model[MAX_LLM_MODEL_LEN]; // 模型名
|
||||
|
||||
// ==================== 网络相关变量 ====================
|
||||
extern NetworkGameState network_state; // 网络游戏状态
|
||||
|
||||
|
||||
+3
-3
@@ -5,8 +5,8 @@
|
||||
* 它包含了游戏棋盘的表示、玩家操作、规则检查以及AI决策等功能。
|
||||
*/
|
||||
|
||||
#ifndef GO_BANG_H
|
||||
#define GO_BANG_H
|
||||
#ifndef GOBANG_H
|
||||
#define GOBANG_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
@@ -88,4 +88,4 @@ bool return_move(int steps_to_undo);
|
||||
*/
|
||||
int calculate_step_score(int x, int y, int player);
|
||||
|
||||
#endif // GO_BANG_H
|
||||
#endif // GOBANG_H
|
||||
@@ -8,14 +8,9 @@ extern Ihandle *dlg;
|
||||
extern Ihandle *board_canvas;
|
||||
extern Ihandle *lbl_player;
|
||||
extern Ihandle *lbl_status;
|
||||
extern int gui_game_mode; // 0: PvP, 1: PvE, 2: Replay
|
||||
extern int gui_game_mode; // 0: PvP, 1: PvE, 2: Replay, 3: Network
|
||||
extern int replay_total_steps; // 复盘总步数
|
||||
|
||||
// 绘图函数 (在 gui_draw.c 中定义)
|
||||
void set_draw_color(Ihandle *ih, unsigned char r, unsigned char g, unsigned char b);
|
||||
void draw_board_iup(Ihandle *ih);
|
||||
void draw_stones_iup(Ihandle *ih);
|
||||
|
||||
// 核心功能 (在 gui_core.c 中定义)
|
||||
void update_ui_labels();
|
||||
int screen_to_board(int screen_x, int screen_y, int *board_x, int *board_y);
|
||||
@@ -24,7 +19,7 @@ int screen_to_board(int screen_x, int screen_y, int *board_x, int *board_y);
|
||||
void create_game_window();
|
||||
void start_pvp_game_gui();
|
||||
void start_pve_game_gui();
|
||||
void start_network_game_gui(); // 新增:启动网络对战游戏窗口
|
||||
void start_network_game_gui();
|
||||
int action_cb(Ihandle *ih);
|
||||
int button_cb(Ihandle *ih, int button, int pressed, int x, int y, char *status);
|
||||
int k_any_cb(Ihandle *ih, int c);
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* @file llm_ai.h
|
||||
* @brief 大模型AI模块头文件
|
||||
* @note 通过OpenAI兼容API调用大模型进行五子棋对弈
|
||||
*/
|
||||
|
||||
#ifndef LLM_AI_H
|
||||
#define LLM_AI_H
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
/**
|
||||
* @brief 调用大模型获取落子坐标(同步,会阻塞)
|
||||
* @param out_x 输出:落子行坐标 (0-based)
|
||||
* @param out_y 输出:落子列坐标 (0-based)
|
||||
* @return true 获取成功,坐标合法且位置为空
|
||||
* @return false 获取失败(网络错误/坐标非法/重试耗尽)
|
||||
*/
|
||||
bool llm_ai_move(int *out_x, int *out_y);
|
||||
|
||||
/**
|
||||
* @brief 异步启动大模型思考(后台线程)
|
||||
* @note 调用后用 llm_ai_poll_result 轮询结果
|
||||
*/
|
||||
void llm_ai_start_move(void);
|
||||
|
||||
/**
|
||||
* @brief 轮询大模型结果(非阻塞)
|
||||
* @param out_x 输出:落子行坐标
|
||||
* @param out_y 输出:落子列坐标
|
||||
* @return 0 仍在思考, 1 成功获取坐标, -1 失败(应回退算法AI)
|
||||
*/
|
||||
int llm_ai_poll_result(int *out_x, int *out_y);
|
||||
|
||||
#endif // LLM_AI_H
|
||||
+2
-6
@@ -13,13 +13,9 @@
|
||||
#include "type.h"
|
||||
#include "config.h"
|
||||
#include <stdbool.h>
|
||||
#include <enet/enet.h> // 引入 ENet 头文件
|
||||
#include <enet/enet.h>
|
||||
|
||||
// 网络状态结构体在 type.h 中定义,我们需要修改 type.h 来包含 ENet 类型
|
||||
// 或者在这里重新定义(如果 type.h 中可以移除旧的定义)
|
||||
|
||||
// 全局网络状态
|
||||
extern NetworkGameState network_state;
|
||||
// network_state 的 extern 声明在 globals.h 中
|
||||
|
||||
// 函数声明
|
||||
|
||||
|
||||
Reference in New Issue
Block a user