game_solver/player.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
/// Represents a player.
pub trait Player: Sized + Eq {
/// The max player count.
#[must_use]
fn count() -> usize;
/// The current index of this player starting at 0.
#[must_use]
fn idx(&self) -> usize;
/// The next player to play
#[must_use]
fn next(self) -> Self;
/// The previous player to play
#[must_use]
fn previous(self) -> Self;
/// How the player instance 'changes' on the next move.
///
/// For partizan games, the player doesn't change:
/// Left stays left; right stays right.
///
/// For impartial games, the player does change:
/// Next turns into previous, and previous turns into next
fn turn(self) -> Self;
}
/// Represents a two player player.
///
/// This player should always be representable by a byte.
pub trait TwoPlayer: Player + Copy {
/// Gets the other player
#[must_use]
fn other(self) -> Self {
self.next()
}
}
/// Represents a player in a zero-sum (2-player) game,
/// where the game is partizan. That is,
/// a player can affect the `Game::possible_moves` function,
/// or players have different winning outcomes.
#[derive(PartialEq, Eq, Debug, Clone, Copy, Hash)]
pub enum PartizanPlayer {
/// The first player.
Left,
/// The second player.
Right,
}
impl Player for PartizanPlayer {
fn count() -> usize {
2
}
fn idx(&self) -> usize {
match self {
Self::Left => 0,
Self::Right => 1,
}
}
fn next(self) -> Self {
match self {
Self::Left => Self::Right,
Self::Right => Self::Left,
}
}
fn previous(self) -> Self {
self.next()
}
fn turn(self) -> Self {
self
}
}
impl TwoPlayer for PartizanPlayer {}
/// Represents a player in a zero-sum (2-player) game,
/// where the game is impartial. That is,
/// the only difference between players is who goes first,
/// and the only thing that defines a game is its 'state':
/// both players need to have the same winning criteria for this player to be used.
#[derive(PartialEq, Eq, Debug, Clone, Copy, Hash)]
pub enum ImpartialPlayer {
/// The player that will play on the current game state,
Next,
/// The player that has played previous to this game state
/// (or will play after Next).
Previous,
}
impl ImpartialPlayer {
pub fn from_move_count(initial_move_count: usize, final_move_count: usize) -> ImpartialPlayer {
if (final_move_count - initial_move_count) % 2 == 0 {
ImpartialPlayer::Next
} else {
ImpartialPlayer::Previous
}
}
}
impl Player for ImpartialPlayer {
fn count() -> usize {
2
}
fn idx(&self) -> usize {
match self {
Self::Next => 0,
Self::Previous => 1,
}
}
fn next(self) -> Self {
match self {
Self::Next => Self::Previous,
Self::Previous => Self::Next,
}
}
fn previous(self) -> Self {
self.next()
}
fn turn(self) -> Self {
self.next()
}
}
impl TwoPlayer for ImpartialPlayer {}
/// Represents a player in an N-player game.
#[derive(PartialEq, Eq, PartialOrd, Ord)]
pub struct NPlayerPartizanConst<const N: usize>(usize);
impl<const N: usize> NPlayerPartizanConst<N> {
pub fn new(index: usize) -> NPlayerPartizanConst<N> {
assert!(index < N, "Player index {index} >= max player count {N}");
Self(index)
}
pub fn new_unchecked(index: usize) -> NPlayerPartizanConst<N> {
debug_assert!(index < N, "Player index {index} >= max player count {N}");
Self(index)
}
}
impl<const N: usize> Player for NPlayerPartizanConst<N> {
fn count() -> usize {
N
}
fn idx(&self) -> usize {
self.0
}
fn next(self) -> Self {
// This will always make index < N.
Self::new_unchecked((self.0 + 1) % N)
}
fn previous(self) -> Self {
if self.0 == 0 {
Self::new_unchecked(N - 1)
} else {
Self::new_unchecked(self.0 - 1)
}
}
fn turn(self) -> Self {
self
}
}