This commit is contained in:
Simon Gardling 2025-02-07 10:31:58 -05:00
parent 366bba8426
commit bd7d4850a1
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D
2 changed files with 21 additions and 14 deletions

View File

@ -1,5 +1,3 @@
use num::CheckedDiv;
use crate::{agent::Agent, board::Board, piece::Piece};
pub struct ComplexAgent {
@ -52,12 +50,6 @@ impl Move {
captured_value + avg_next_move_value
}
fn best_move(&self, agent_color: Piece) -> Option<&Move> {
self.next_move
.iter()
.max_by_key(|m| m.value(agent_color, 1))
}
pub fn len(&self) -> u32 {
self.next_move.len() as u32 + self.next_move.iter().map(Move::len).sum::<u32>()
}
@ -105,7 +97,7 @@ fn problem_space(board: &Board, color: Piece) -> Box<dyn Iterator<Item = Move> +
impl Agent for ComplexAgent {
fn next_move(&mut self, board: &Board) -> Option<(usize, usize)> {
const LOOPS: usize = 6;
const LOOPS: usize = 5;
let curr_move: Move = self
.curr_move
@ -117,12 +109,27 @@ impl Agent for ComplexAgent {
.into_iter()
.filter(|x| &x.board == board)
.last()
.expect("other player made an invalid move?");
// handle invalid other player moves
.unwrap_or_else(|| {
println!("invalid board, rebuilding move tree...");
// rebuild move tree
// need to start with a !self.color move, so unwrap the first level
Move::bootstrap(self.color, board).next_move.remove(0)
});
other_player_move.populate_next_moves(LOOPS);
println!("depth: {}", other_player_move.len());
println!(
"(depth: {}) possible board states: {}",
LOOPS,
other_player_move.len()
);
self.curr_move = other_player_move.best_move(self.color).cloned();
// Take the best move and move it, don't clone the reference
self.curr_move = other_player_move
.next_move
.into_iter()
.max_by_key(|m| m.value(self.color, 1));
self.curr_move.as_ref().map(Move::coords)
}

View File

@ -10,9 +10,9 @@ mod piece;
fn main() {
let player1 = complexagent::ComplexAgent::new(Piece::Black);
// let player2 = complexagent::ComplexAgent::new(Piece::White);
let player2 = complexagent::ComplexAgent::new(Piece::White);
// let player2 = agent::ManualAgent::new(Piece::White);
let player2 = agent::RandomAgent::new(Piece::White);
// let player2 = agent::RandomAgent::new(Piece::White);
let mut game = Game::new(Box::new(player1), Box::new(player2));
game.game_loop();
}