From 8bcb604e86d61883e0cf8332c97df094822e7a75 Mon Sep 17 00:00:00 2001 From: Francisco Salgueiro Date: Tue, 7 Nov 2023 00:45:52 +0000 Subject: [PATCH] analyze first position as well --- src-tauri/src/chess.rs | 24 ++++++++++++------------ src/utils/treeReducer.ts | 4 ++-- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src-tauri/src/chess.rs b/src-tauri/src/chess.rs index 583ec2b6..eb661287 100644 --- a/src-tauri/src/chess.rs +++ b/src-tauri/src/chess.rs @@ -439,30 +439,30 @@ pub async fn analyze_game( let mut analysis: Vec = Vec::new(); let (mut process, mut reader) = EngineProcess::new(path)?; + let fen = Fen::from_ascii(options.fen.as_bytes())?; - let mut chess: Chess = fen.into_position(CastlingMode::Standard)?; + let mut chess: Chess = fen.clone().into_position(CastlingMode::Standard)?; + let mut fens: Vec = vec![fen]; - let len_moves = moves.len(); + moves.iter().for_each(|m| { + let san = San::from_ascii(m.as_bytes()).unwrap(); + let m = san.to_move(&chess).unwrap(); + chess.play_unchecked(&m); + fens.push(Fen::from_position(chess.clone(), EnPassantMode::Legal)); + }); let mut novelty_found = false; - for (i, m) in moves.iter().enumerate() { + for (i, fen) in fens.iter().enumerate() { app.emit_all( "report_progress", ProgressPayload { - progress: (i as f64 / len_moves as f64) * 100.0, + progress: (i as f64 / fens.len() as f64) * 100.0, id: 0, finished: false, }, )?; - let san = San::from_ascii(m.as_bytes())?; - let m = san.to_move(&chess)?; - chess.play_unchecked(&m); - if chess.is_game_over() { - break; - } - let fen = Fen::from_position(chess.clone(), EnPassantMode::Legal); let query = PositionQuery::exact_from_fen(&fen.to_string())?; process @@ -480,7 +480,7 @@ pub async fn analyze_game( while let Ok(Some(line)) = reader.next_line().await { match parse_one(&line) { UciMessage::Info(attrs) => { - if let Ok(best_moves) = parse_uci_attrs(attrs, &fen) { + if let Ok(best_moves) = parse_uci_attrs(attrs, fen) { current_analysis.best = best_moves; } } diff --git a/src/utils/treeReducer.ts b/src/utils/treeReducer.ts index 5d368c39..7960d649 100644 --- a/src/utils/treeReducer.ts +++ b/src/utils/treeReducer.ts @@ -381,7 +381,7 @@ export const getNodeAtPath = (node: TreeNode, path: number[]): TreeNode => { }; function addAnalysis(state: TreeState, analysis: { best: BestMoves, novelty: boolean }[]) { - let cur = state.root.children[0]; + let cur = state.root; let i = 0; while (cur !== undefined && i < analysis.length) { cur.score = analysis[i].best.score; @@ -394,7 +394,7 @@ function addAnalysis(state: TreeState, analysis: { best: BestMoves, novelty: boo prevScore = analysis[i - 1].best.score; } const curScore = analysis[i].best.score; - const color = i % 2 === 0 ? "w" : "b"; + const color = i % 2 === 1 ? "w" : "b"; cur.annotation = getAnnotation(prevScore, curScore, color); cur = cur.children[0]; i++;