This commit is contained in:
Jana Dönszelmann 2025-09-11 11:46:42 -07:00
parent d4e626d13c
commit 20a6d2faeb
No known key found for this signature in database
4 changed files with 401 additions and 8 deletions

View file

@ -1,7 +1,9 @@
use std::fmt::Display;
use simple_mcts::Game;
#[derive(Clone, Copy, PartialEq)]
struct PlayerState {
pub struct PlayerState {
xy: u8,
walls_left: u8,
}
@ -45,7 +47,7 @@ impl PlayerState {
}
#[derive(Copy, Clone)]
struct WallState {
pub struct WallState {
verticals: [u8; 9],
horizontals: [u8; 9],
}
@ -101,10 +103,46 @@ impl Default for WallState {
}
}
struct GameState {
p1: PlayerState,
p2: PlayerState,
walls: WallState,
#[derive(Clone, Copy)]
pub struct GameState {
pub p1: PlayerState,
pub p2: PlayerState,
pub walls: WallState,
pub whose_turn: bool,
}
impl GameState {
pub fn current_player(&self) -> &PlayerState {
if self.whose_turn { &self.p1 } else { &self.p2 }
}
pub fn current_player_mut(&mut self) -> &mut PlayerState {
if self.whose_turn {
&mut self.p1
} else {
&mut self.p2
}
}
pub fn mcts_result(&self) -> Option<f64> {
let p1_won = self.p1.y() == 8;
let p2_won = self.p2.y() == 0;
let outcome_for_p1 = match (p1_won, p2_won) {
(false, false) => return None,
(true, false) => 1.0,
(false, true) => -1.0,
(true, true) => 0.0,
};
Some(if self.whose_turn {
//p1 wants to win
outcome_for_p1
} else {
//p2 wants to win
-1.0 * outcome_for_p1
})
}
}
impl Default for GameState {
@ -113,6 +151,7 @@ impl Default for GameState {
p1: PlayerState::P1_START,
p2: PlayerState::P2_START,
walls: Default::default(),
whose_turn: true,
}
}
}