Revert "Move: bit madness"

This reverts commit afa80ac597.

Only made a 3% memory decrease, Most memory is used in the children Vec
This commit is contained in:
2025-03-13 09:33:56 -04:00
parent afa80ac597
commit ed5ad738e3
5 changed files with 30 additions and 125 deletions

View File

@@ -7,67 +7,29 @@ pub struct Move {
/// Coordinates (i, j) of the move (if it exists)
pub coord: Option<CoordPair>,
/// Current winner of the match
pub winner: Winner,
/// Index of this move's parent
pub parent: Option<usize>,
/// Indices of this Move's Children
pub children: Vec<usize>,
/// Has this [`Move`] already attempted to create children?
pub tried_children: bool,
/// Value of this move (including children)
pub value: Option<i32>,
/// What is the inherit value of this move (not including children)
pub self_value: i16,
pub data: MoveData,
}
/// Which color made a move on this move?
pub color: Piece,
#[derive(Clone, Debug, Copy)]
pub struct MoveData(u8);
impl MoveData {
const WINNER_LOC: usize = 0; // uses 2 bits
pub const fn set_winner(&mut self, winner: Winner) {
self.0 &= !(0b11 << Self::WINNER_LOC);
self.0 |= winner.raw() << Self::WINNER_LOC;
}
pub const fn get_winner(&self) -> Winner {
Winner::from_raw(((self.0 >> Self::WINNER_LOC) & 0b11) as u8)
}
const PIECE_LOC: usize = 2;
pub const fn set_piece(&mut self, piece: Piece) {
self.0 &= !(0b1 << Self::PIECE_LOC);
self.0 |= (unsafe { std::mem::transmute::<Piece, u8>(piece) }) << Self::PIECE_LOC;
}
pub const fn get_piece(&self) -> Piece {
unsafe { std::mem::transmute(((self.0 >> Self::PIECE_LOC) & 0b1) as u8) }
}
const TRIMMED_LOC: usize = 3;
pub const fn set_trimmed(&mut self, trimmed: bool) {
self.0 &= !(0b1 << Self::TRIMMED_LOC);
self.0 |= (trimmed as u8) << Self::TRIMMED_LOC;
}
pub const fn get_trimmed(&self) -> bool {
((self.0 >> Self::TRIMMED_LOC) & 0b1) != 0
}
const TRIED_CHILDREN_LOC: usize = 4;
pub const fn set_tried_children(&mut self, tried_children: bool) {
self.0 &= !(0b1 << Self::TRIED_CHILDREN_LOC);
self.0 |= (tried_children as u8) << Self::TRIED_CHILDREN_LOC;
}
pub const fn get_tried_children(&self) -> bool {
((self.0 >> Self::TRIED_CHILDREN_LOC) & 0b1) != 0
}
/// Was this move's children previously trimmed?
pub is_trimmed: bool,
}
static BVM: LazyLock<BoardValueMap> = LazyLock::new(BoardValueMap::new);
@@ -76,28 +38,26 @@ impl Move {
pub fn new(coord: Option<CoordPair>, board: Board, color: Piece, agent_color: Piece) -> Self {
let mut m = Move {
coord,
winner: board.game_winner(),
parent: None,
children: Vec::new(),
value: None,
color,
is_trimmed: false,
self_value: 0,
data: MoveData(0),
tried_children: false,
};
let winner = board.game_winner();
m.data.set_winner(winner);
m.data.set_piece(color);
m.data.set_trimmed(false);
m.data.set_tried_children(false);
m.self_value = m.compute_self_value(agent_color, &board);
m
}
fn compute_self_value(&self, agent_color: Piece, board: &Board) -> i16 {
if self.data.get_winner() == Winner::Player(!agent_color) {
if self.winner == Winner::Player(!agent_color) {
// if this board results in the opponent winning, MAJORLY negatively weigh this move
// NOTE! this branch isn't completely deleted because if so, the bot wouldn't make a move.
// We shouldn't prune branches because we still need to always react to the opponent's moves
return i16::MIN + 1;
} else if self.data.get_winner() == Winner::Player(agent_color) {
} else if self.winner == Winner::Player(agent_color) {
// results in a win for the agent
return i16::MAX - 1;
}
@@ -118,32 +78,3 @@ impl Move {
});
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn move_data_winner() {
let mut m = MoveData(0);
macro_rules! test_winner {
($winner:expr) => {
m.set_winner($winner);
assert_eq!(m.get_winner(), $winner);
};
}
test_winner!(Winner::Player(Piece::Black));
test_winner!(Winner::Player(Piece::White));
test_winner!(Winner::Tie);
test_winner!(Winner::None);
}
#[test]
fn move_data_trimmed() {
let mut m = MoveData(0);
m.set_trimmed(false);
assert!(!m.get_trimmed());
m.set_trimmed(true);
assert!(m.get_trimmed());
}
}