From 30476757dac89ed3b9525766e6a9455801bd3058 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Tue, 1 Apr 2025 15:49:18 -0400 Subject: [PATCH] Board: make starting_pos a const variable instead --- benches/future_children.rs | 2 +- src/allocs.rs | 2 +- src/game.rs | 2 +- src/logic/future_moves.rs | 8 ++++---- src/repr/board.rs | 21 +++++++++++---------- 5 files changed, 18 insertions(+), 17 deletions(-) diff --git a/benches/future_children.rs b/benches/future_children.rs index 217ca3e..9c76387 100644 --- a/benches/future_children.rs +++ b/benches/future_children.rs @@ -16,7 +16,7 @@ fn extend_layers_no_pruning(depth: usize) -> usize { children_eval_method: ChildrenEvalMethod::AverageDivDepth, }; let mut fut = FutureMoves::new(Piece::Black, config); - fut.update_from_board(&Board::new().starting_pos()); + fut.update_from_board(&Board::STARTING_POSITION); fut.extend_layers(); fut.arena_len() } diff --git a/src/allocs.rs b/src/allocs.rs index 8d80c84..0d7105c 100644 --- a/src/allocs.rs +++ b/src/allocs.rs @@ -22,7 +22,7 @@ pub fn run() { }, ); - let mut board = Board::new().starting_pos(); + let mut board = Board::STARTING_POSITION; let mut rng = rand::rng(); let mut i = 0; while board.game_winner() == Winner::None && i < 2 { diff --git a/src/game.rs b/src/game.rs index 056c293..a86a214 100644 --- a/src/game.rs +++ b/src/game.rs @@ -8,7 +8,7 @@ pub struct Game { impl Game { pub fn new(player1: Box, player2: Box) -> Self { Self { - inner: GameInner::new(player1, player2, true, Board::new().starting_pos()), + inner: GameInner::new(player1, player2, true, Board::STARTING_POSITION), } } diff --git a/src/logic/future_moves.rs b/src/logic/future_moves.rs index d323fbf..0b784e3 100644 --- a/src/logic/future_moves.rs +++ b/src/logic/future_moves.rs @@ -629,7 +629,7 @@ mod tests { let mut futm = FutureMoves::new(Piece::Black, FUTURE_MOVES_CONFIG); futm.config.max_depth = 1; - futm.update_from_board(&Board::new().starting_pos()); + futm.update_from_board(&Board::STARTING_POSITION); futm.extend_layers(); assert_eq!(futm.arena_len(), 5); @@ -710,7 +710,7 @@ mod tests { #[test] fn skip_move_recovery() { let mut futm = FutureMoves::new(Piece::Black, FUTURE_MOVES_CONFIG); - let mut board = Board::new().starting_pos(); + let mut board = Board::STARTING_POSITION; // replay of a test I did // TODO! make this as small of a test as possible @@ -774,8 +774,8 @@ mod tests { fn derive_board() { let mut futm = FutureMoves::new(Piece::White, FUTURE_MOVES_CONFIG); - let mut b = Board::new().starting_pos(); - futm.update_from_board(&Board::new().starting_pos()); + let mut b = Board::STARTING_POSITION; + futm.update_from_board(&b); b.place((4, 2).into(), Piece::White).unwrap(); diff --git a/src/repr/board.rs b/src/repr/board.rs index bfebb15..d2cd99b 100644 --- a/src/repr/board.rs +++ b/src/repr/board.rs @@ -123,7 +123,7 @@ impl Board { } pub fn random(steps: usize) -> Self { - let mut new = Self::new().starting_pos(); + let mut new = Self::STARTING_POSITION; let mut p = Piece::Black; let mut rng = rand::rng(); for _ in 0..steps { @@ -137,16 +137,17 @@ impl Board { new } - /// Starting position - pub const fn starting_pos(mut self) -> Self { + pub const STARTING_POSITION: Self = { + let mut new = Self::new(); let hf = Self::SIZE / 2; - self.place_unchecked(CoordPair::from_axes(hf - 1, hf - 1), Piece::White); - self.place_unchecked(CoordPair::from_axes(hf, hf - 1), Piece::Black); - self.place_unchecked(CoordPair::from_axes(hf - 1, hf), Piece::Black); - self.place_unchecked(CoordPair::from_axes(hf, hf), Piece::White); - self - } + new.place_unchecked(CoordPair::from_axes(hf - 1, hf - 1), Piece::White); + new.place_unchecked(CoordPair::from_axes(hf, hf - 1), Piece::Black); + + new.place_unchecked(CoordPair::from_axes(hf - 1, hf), Piece::Black); + new.place_unchecked(CoordPair::from_axes(hf, hf), Piece::White); + new + }; /// Provides an iterator of all possible positions on the board pub fn all_positions() -> impl Iterator { @@ -499,7 +500,7 @@ mod test { #[test] fn format_test() { assert_eq!( - Board::new().starting_pos().to_string().as_str(), + Board::STARTING_POSITION.to_string().as_str(), " 7 6 5 4 3 2 1 0 ----------------- 7| | | | | | | | |