From 7ec23f1baba50c9b4708c6b3a3b843f3eee1f2d4 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Tue, 18 Feb 2025 12:55:15 -0500 Subject: [PATCH] nits --- src/board.rs | 60 ++++++++++++++++++++++++--------------------- src/complexagent.rs | 4 ++- src/main.rs | 4 +-- 3 files changed, 37 insertions(+), 31 deletions(-) diff --git a/src/board.rs b/src/board.rs index 19f031c..3e29d29 100644 --- a/src/board.rs +++ b/src/board.rs @@ -114,11 +114,11 @@ impl fmt::Display for Board { writeln!(f, " {}", horiz_sep_line)?; // Print the current score - let (white_score, black_score) = self.get_score(); writeln!( f, "White Score: {}\nBlack Score: {}", - white_score, black_score + self.count(Piece::White), + self.count(Piece::Black) )?; Ok(()) @@ -159,11 +159,31 @@ impl Board { Self::all_positions().filter(move |&(i, j)| self.would_prop(i, j, color)) } + /// Get a reference to a backing [`BitBoard`] + pub fn board(&self, color: Piece) -> &BitBoard { + match color { + Piece::Black => &self.black_board, + Piece::White => &self.white_board, + } + } + + /// Get a mutable reference to a backing [`BitBoard`] + pub fn board_mut(&mut self, color: Piece) -> &mut BitBoard { + match color { + Piece::Black => &mut self.black_board, + Piece::White => &mut self.white_board, + } + } + + pub fn get_piece(&self, i: usize, j: usize, color: Piece) -> bool { + self.board(color).get(i, j) + } + /// Returns the color of a place on the [`Board`] at a position pub fn get(&self, i: usize, j: usize) -> Option { - if self.white_board.get(i, j) { + if self.get_piece(i, j, Piece::White) { Some(Piece::White) - } else if self.black_board.get(i, j) { + } else if self.get_piece(i, j, Piece::Black) { Some(Piece::Black) } else { None @@ -172,16 +192,8 @@ impl Board { /// Place a piece without checking for propegation of validity fn place_unchecked(&mut self, i: usize, j: usize, piece: Piece) { - match piece { - Piece::Black => { - self.black_board.set(i, j, true); - self.white_board.set(i, j, false); - } - Piece::White => { - self.black_board.set(i, j, false); - self.white_board.set(i, j, true); - } - } + self.board_mut(piece).set(i, j, true); + self.board_mut(!piece).set(i, j, false); } /// Return a modified [`Board`] with the piece placed at a position @@ -242,14 +254,14 @@ impl Board { .iter() .filter_map(move |chain| { let mut end_idx = None; - for (idx, &(new_i, new_j)) in chain.iter().enumerate() { + for (idx, &(new_i, new_j)) in chain.into_iter().enumerate() { let piece = self.get(new_i, new_j)?; if piece == starting_color { end_idx = Some(idx); break; } } - end_idx.map(|idx| &chain[..idx]) + end_idx.and_then(|idx| chain.get(..idx)) }) .flatten() .cloned() @@ -257,22 +269,15 @@ impl Board { /// Count the number of a type of [`Piece`] on the board pub fn count(&self, piece: Piece) -> usize { - match piece { - Piece::Black => self.black_board, - Piece::White => self.white_board, - } - .count() + self.board(piece).count() } + /// Get the "net score" of a player + /// Formula: `net_score = Score_player - Score_opponent` pub fn net_score(&self, piece: Piece) -> isize { self.count(piece) as isize - self.count(!piece) as isize } - /// Returns (White score, Black score) - pub fn get_score(&self) -> (usize, usize) { - (self.count(Piece::White), self.count(Piece::Black)) - } - /// Returns the winner of the board (if any) pub fn game_winner(&self, turn: Piece) -> Winner { // Wikipedia: `Players take alternate turns. If one player cannot make a valid move, play passes back to the other player. The game ends when the grid has filled up or if neither player can make a valid move.` @@ -283,8 +288,7 @@ impl Board { return Winner::None; } - let (white_score, black_score) = self.get_score(); - match white_score.cmp(&black_score) { + match self.count(Piece::White).cmp(&self.count(Piece::Black)) { Ordering::Greater => Winner::Player(Piece::White), // White win Ordering::Less => Winner::Player(Piece::Black), // Black win Ordering::Equal => Winner::Tie, diff --git a/src/complexagent.rs b/src/complexagent.rs index a2d6f16..ee32446 100644 --- a/src/complexagent.rs +++ b/src/complexagent.rs @@ -363,7 +363,9 @@ impl Agent for ComplexAgent { println!("# of moves stored: {}", self.future_moves.arena.len()); self.future_moves.best_move().inspect(|&(i, j)| { - self.future_moves.update_root_coord(i, j); + if !self.future_moves.update_root_coord(i, j) { + panic!("update_root_coord failed"); + } }) } diff --git a/src/main.rs b/src/main.rs index 37db162..a0b4902 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,8 +12,8 @@ mod piece; fn main() { let player1 = complexagent::ComplexAgent::new(Piece::Black); // let player2 = complexagent::ComplexAgent::new(Piece::White); - // let player2 = agent::ManualAgent::new(Piece::White); - let player2 = agent::RandomAgent::new(Piece::White); + let player2 = agent::ManualAgent::new(Piece::White); + // let player2 = agent::RandomAgent::new(Piece::White); let mut game = Game::new(Box::new(player1), Box::new(player2)); game.game_loop(); }