CoordPair::from_axes

This commit is contained in:
2025-03-05 20:14:18 -05:00
parent e60e546f2f
commit 8a7496a492
4 changed files with 18 additions and 31 deletions

View File

@@ -168,14 +168,10 @@ impl PlayerArena {
self.process_outcome(i, j, &o); self.process_outcome(i, j, &o);
received_num += 1; received_num += 1;
// print the leaderboard every 5 steps term.clear_last_lines(self.players.len())
if received_num % 5 == 0 { .expect("unable to clear prev lines");
term.clear_last_lines(self.players.len()) term.write_str(format!("{}", self).as_str())
.expect("unable to clear prev lines"); .expect("unable to write leaderboard");
let formatted_self = format!("{}", self);
term.write_str(&formatted_self)
.expect("unable to write leaderboard");
}
// break if all pairs were recieved // break if all pairs were recieved
if received_num == num { if received_num == num {

View File

@@ -20,12 +20,6 @@ impl BoardValueMap {
/// Weights from: https://repub.eur.nl/pub/7142/ei2005-47.pdf /// Weights from: https://repub.eur.nl/pub/7142/ei2005-47.pdf
pub fn new() -> Self { pub fn new() -> Self {
assert_eq!(
Board::BOARD_SIZE,
8,
"BVM only supports Board::BOARD_SIZE of 8"
);
const POSITION_VALUES: [[i64; 8]; 8] = [ const POSITION_VALUES: [[i64; 8]; 8] = [
[100, -20, 10, 5, 5, 10, -20, 100], [100, -20, 10, 5, 5, 10, -20, 100],
[-20, -50, -2, -2, -2, -2, -50, -20], [-20, -50, -2, -2, -2, -2, -50, -20],

View File

@@ -143,12 +143,13 @@ impl Board {
} }
/// Starting position /// Starting position
pub fn starting_pos(mut self) -> Self { pub const fn starting_pos(mut self) -> Self {
let hf = Self::BOARD_SIZE / 2; let hf = Self::BOARD_SIZE / 2;
self.place_unchecked((hf - 1, hf - 1).into(), Piece::White); self.place_unchecked(CoordPair::from_axes(hf - 1, hf - 1), Piece::White);
self.place_unchecked((hf, hf - 1).into(), Piece::Black); self.place_unchecked(CoordPair::from_axes(hf, hf - 1), Piece::Black);
self.place_unchecked((hf - 1, hf).into(), Piece::Black);
self.place_unchecked((hf, hf).into(), Piece::White); self.place_unchecked(CoordPair::from_axes(hf - 1, hf), Piece::Black);
self.place_unchecked(CoordPair::from_axes(hf, hf), Piece::White);
self self
} }
@@ -178,13 +179,11 @@ impl Board {
} }
} }
#[const_fn(cfg(not(feature = "bitvec")))]
pub const fn get_piece(&self, coord: CoordPair, color: Piece) -> bool { pub const fn get_piece(&self, coord: CoordPair, color: Piece) -> bool {
self.board(color).get(coord) self.board(color).get(coord)
} }
/// Returns the color of a place on the [`Board`] at a position /// Returns the color of a place on the [`Board`] at a position
#[const_fn(cfg(not(feature = "bitvec")))]
pub const fn get(&self, coord: CoordPair) -> Option<Piece> { pub const fn get(&self, coord: CoordPair) -> Option<Piece> {
if self.get_piece(coord, Piece::White) { if self.get_piece(coord, Piece::White) {
Some(Piece::White) Some(Piece::White)
@@ -197,13 +196,11 @@ impl Board {
/// Place a piece without checking for propegation of validity /// Place a piece without checking for propegation of validity
/// only pub for setting up benchmark /// only pub for setting up benchmark
#[const_fn(cfg(not(feature = "bitvec")))]
pub const fn place_unchecked(&mut self, coord: CoordPair, piece: Piece) { pub const fn place_unchecked(&mut self, coord: CoordPair, piece: Piece) {
self.board_mut(piece).set(coord, true); self.board_mut(piece).set(coord, true);
self.board_mut(piece.flip()).set(coord, false); self.board_mut(piece.flip()).set(coord, false);
} }
#[const_fn(cfg(not(feature = "bitvec")))]
const fn delete(&mut self, coord: CoordPair) { const fn delete(&mut self, coord: CoordPair) {
self.board_mut(Piece::White).set(coord, false); self.board_mut(Piece::White).set(coord, false);
self.board_mut(Piece::Black).set(coord, false); self.board_mut(Piece::Black).set(coord, false);
@@ -289,14 +286,12 @@ impl Board {
} }
/// Count the number of a type of [`Piece`] on the board /// Count the number of a type of [`Piece`] on the board
#[const_fn(cfg(not(feature = "bitvec")))]
pub const fn count(&self, piece: Piece) -> usize { pub const fn count(&self, piece: Piece) -> usize {
self.board(piece).count() self.board(piece).count()
} }
/// Get the "net score" of a player /// Get the "net score" of a player
/// Formula: `net_score = Score_player - Score_opponent` /// Formula: `net_score = Score_player - Score_opponent`
#[const_fn(cfg(not(feature = "bitvec")))]
pub const fn net_score(&self, piece: Piece) -> i16 { pub const fn net_score(&self, piece: Piece) -> i16 {
self.count(piece) as i16 - self.count(piece.flip()) as i16 self.count(piece) as i16 - self.count(piece.flip()) as i16
} }

View File

@@ -14,6 +14,13 @@ const_assert!(CoordPairInner::MAX as usize >= Board::BOARD_AREA as usize);
#[derive(PartialEq, Eq, Copy, Clone, Hash)] #[derive(PartialEq, Eq, Copy, Clone, Hash)]
pub struct CoordPair(pub CoordPairInner); pub struct CoordPair(pub CoordPairInner);
impl CoordPair {
/// Convert a row and column to an index in the board
pub const fn from_axes(row: CoordAxis, col: CoordAxis) -> Self {
Self(row * Board::BOARD_SIZE + col)
}
}
impl std::fmt::Display for CoordPair { impl std::fmt::Display for CoordPair {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let (i, j) = (*self).into(); let (i, j) = (*self).into();
@@ -27,18 +34,13 @@ impl std::fmt::Debug for CoordPair {
} }
} }
/// Convert a row and column to an index in the board
const fn get_index(row: CoordAxis, col: CoordAxis) -> CoordPair {
CoordPair(row * Board::BOARD_SIZE + col)
}
fn from_index(index: CoordPair) -> (CoordAxis, CoordAxis) { fn from_index(index: CoordPair) -> (CoordAxis, CoordAxis) {
index.0.div_mod_floor(&Board::BOARD_SIZE) index.0.div_mod_floor(&Board::BOARD_SIZE)
} }
impl From<(CoordAxis, CoordAxis)> for CoordPair { impl From<(CoordAxis, CoordAxis)> for CoordPair {
fn from(value: (CoordAxis, CoordAxis)) -> Self { fn from(value: (CoordAxis, CoordAxis)) -> Self {
get_index(value.0, value.1) Self::from_axes(value.0, value.1)
} }
} }