From c1c64b0155478dc322c9b5af9a42a25094a5ce94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E8=88=AA=E5=AE=87?= <3364451258@qq.com> Date: Sun, 31 May 2026 13:43:25 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20AI=20=E6=90=9C=E7=B4=A2=E7=A7=BB?= =?UTF-8?q?=E5=88=B0=E7=8B=AC=E7=AB=8B=E5=90=8E=E5=8F=B0=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E9=81=BF=E5=85=8D=E9=98=BB=E5=A1=9E=20GUI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 4.7 --- core/src/ai/search.rs | 1 + gui/src/commands.rs | 23 +++++++++++++++++------ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/core/src/ai/search.rs b/core/src/ai/search.rs index def90f1..b26fb4c 100644 --- a/core/src/ai/search.rs +++ b/core/src/ai/search.rs @@ -5,6 +5,7 @@ use crate::rules; use crate::types::{Color, Position}; /// Alpha-Beta AI 引擎 +#[derive(Clone)] pub struct AlphaBetaAi { depth: usize, } diff --git a/gui/src/commands.rs b/gui/src/commands.rs index b1ba63a..8a002b1 100644 --- a/gui/src/commands.rs +++ b/gui/src/commands.rs @@ -121,13 +121,24 @@ pub fn undo(steps: u32, state: State) -> Result<(), String> { #[tauri::command] pub fn ai_move(state: State) -> Result, String> { - let board_opt = state.board.lock().map_err(|e| e.to_string())?; - let board = board_opt.as_ref().ok_or("游戏未开始")?; - let color = *state.current_color.lock().map_err(|e| e.to_string())?; - let ai = state.ai_engine.lock().map_err(|e| e.to_string())?; - let ai = ai.as_ref().ok_or("AI 未初始化")?; + let (board_clone, color, ai_clone) = { + let board_opt = state.board.lock().map_err(|e| e.to_string())?; + let board = board_opt.as_ref().ok_or("游戏未开始")?.clone(); + let color = *state.current_color.lock().map_err(|e| e.to_string())?; + let ai_guard = state.ai_engine.lock().map_err(|e| e.to_string())?; + let ai = ai_guard.as_ref().ok_or("AI 未初始化")?.clone(); + (board, color, ai) + }; - Ok(ai.best_move(board, color).map(|p| (p.x, p.y))) + let (tx, rx) = std::sync::mpsc::channel(); + std::thread::spawn(move || { + let result = ai_clone.best_move(&board_clone, color); + let _ = tx.send(result); + }); + + rx.recv_timeout(std::time::Duration::from_secs(30)) + .map_err(|_| "AI 计算超时".to_string()) + .map(|r| r.map(|p| (p.x, p.y))) } #[tauri::command]