diff options
author | Kai Stevenson <kai@kaistevenson.com> | 2025-05-07 23:26:47 -0700 |
---|---|---|
committer | Kai Stevenson <kai@kaistevenson.com> | 2025-05-07 23:26:47 -0700 |
commit | b779e3adc0491d25d93dfe5264b7134148a8a888 (patch) | |
tree | 0adc29f94cecd77697ee037d4d914453f6220196 /tic_tac_toe | |
parent | af2bc841119a6751c240dec95dd5511d4ee31d36 (diff) |
improve debug messages
Diffstat (limited to 'tic_tac_toe')
-rw-r--r-- | tic_tac_toe/src/ai.rs | 23 | ||||
-rw-r--r-- | tic_tac_toe/src/main.rs | 4 |
2 files changed, 16 insertions, 11 deletions
diff --git a/tic_tac_toe/src/ai.rs b/tic_tac_toe/src/ai.rs index e2d8918..4071ee3 100644 --- a/tic_tac_toe/src/ai.rs +++ b/tic_tac_toe/src/ai.rs @@ -5,7 +5,6 @@ use crate::{GameState, check_state}; pub struct BestMove { pub coord: Coord, eval: i8, - depth: u32, } fn eval_for_player(board: &Board, player: Tile) -> i8 { @@ -37,7 +36,7 @@ fn eval_for_player(board: &Board, player: Tile) -> i8 { } } -pub fn get_best_move(board: &Board, player: Tile, is_tl: bool, depth: u32) -> BestMove { +pub fn get_best_move(board: &Board, player: Tile, is_tl: bool) -> BestMove { //base case //game is over, return eval let eval = eval_for_player(board, player); @@ -47,7 +46,6 @@ pub fn get_best_move(board: &Board, player: Tile, is_tl: bool, depth: u32) -> Be return BestMove { coord: (99, 99), eval, - depth, }; } @@ -64,9 +62,10 @@ pub fn get_best_move(board: &Board, player: Tile, is_tl: bool, depth: u32) -> Be let mut cur_best = BestMove { eval: i8::min_value(), coord: possible_moves[0], - depth, }; + let p_move_count = possible_moves.len(); + for p_move in possible_moves { let mut new_board = board.clone(); new_board.set_at_coord(p_move, player); @@ -79,7 +78,7 @@ pub fn get_best_move(board: &Board, player: Tile, is_tl: bool, depth: u32) -> Be Tile::Unowned => Tile::PlayerOne, }; - let response_move = get_best_move(&new_board, other_player, false, depth + 1); + let response_move = get_best_move(&new_board, other_player, false); if response_move.coord.0 != 99 { new_board.set_at_coord(response_move.coord, other_player); } @@ -87,10 +86,6 @@ pub fn get_best_move(board: &Board, player: Tile, is_tl: bool, depth: u32) -> Be //this is the inverse of the response eval let response_eval = -response_move.eval; - if is_tl { - println!("AI > If I make move {p_move:?}, the best response is {response_move:?}"); - } - //slightly bias for the centre to beat weak players let centre_bias = if p_move == (1, 1) { 1 } else { 0 }; @@ -98,12 +93,20 @@ pub fn get_best_move(board: &Board, player: Tile, is_tl: bool, depth: u32) -> Be cur_best = BestMove { coord: p_move, eval: response_eval + centre_bias, - depth: response_move.depth, }; } } if is_tl { + //log thought process + println!("AI > There are {p_move_count} moves that I can play"); + let sentiment = match cur_best.eval { + -100 | -101 => "I'll lose this game if my opponent plays right", + -9 | -10 | -11 => "If my opponent plays well, this will be a draw", + 100 | 101 => "I'll win this game", + _ => "I can't predict the outcome of this game", + }; + println!("AI > ({sentiment})"); println!("AI > I'll play {cur_best:?}") } diff --git a/tic_tac_toe/src/main.rs b/tic_tac_toe/src/main.rs index 19285a2..d1762ba 100644 --- a/tic_tac_toe/src/main.rs +++ b/tic_tac_toe/src/main.rs @@ -136,8 +136,10 @@ fn main() { } } + clear_scrn(); + //get AI move - let ai_move = get_best_move(&board, Tile::PlayerTwo, true, 0); + let ai_move = get_best_move(&board, Tile::PlayerTwo, true); board.set_at_coord(ai_move.coord, Tile::PlayerTwo); match check_state(&board) { |