-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
227 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/// Represents a player. | ||
pub trait Player { | ||
/// 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; | ||
} | ||
|
||
/// Represents a player in a zero-sum (2-player) game. | ||
/// | ||
/// Allows for usage of `negamax` instead of minimax. | ||
#[derive(PartialEq, Eq, Debug, Clone, Copy, Hash)] | ||
pub enum ZeroSumPlayer { | ||
/// The first player. | ||
Left, | ||
/// The second player. | ||
Right, | ||
} | ||
|
||
impl Player for ZeroSumPlayer { | ||
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, | ||
} | ||
} | ||
} | ||
|
||
/// Represents a player in a zero-sum (2-player) game, | ||
/// where the game is impartial. That is, | ||
/// a player does not affect the `Game::possible_moves` function. | ||
#[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 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, | ||
} | ||
} | ||
} | ||
|
||
/// Represents a player in an N-player game. | ||
pub struct NPlayerConst<const N: usize>(usize); | ||
|
||
impl<const N: usize> NPlayerConst<N> { | ||
pub fn new(index: usize) -> NPlayerConst<N> { | ||
assert!(index < N, "Player index {index} >= max player count {N}"); | ||
Self(index) | ||
} | ||
|
||
pub fn new_unchecked(index: usize) -> NPlayerConst<N> { | ||
debug_assert!(index < N, "Player index {index} >= max player count {N}"); | ||
Self(index) | ||
} | ||
} | ||
|
||
impl<const N: usize> Player for NPlayerConst<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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.