pub struct PlayerIndex<const P: usize>(/* private fields */);
Expand description
An index into a per-player collection that is guaranteed to be in-range for a game
with P
players.
Note that players are indexed from zero for consistency with the rest of Rust. That is, the
first player in a game has index 0
, the second player has index 1
, and so on. This isn’t
ideal since most of the literature on game theory uses one-based terminology. However, I judged
internal consistency to be more important than external consistency, in this case. Juggling
multiple different indexing styles within the code itself would be really confusing!
Implementations§
Source§impl<const P: usize> PlayerIndex<P>
impl<const P: usize> PlayerIndex<P>
Sourcepub fn new(index: usize) -> Option<Self>
pub fn new(index: usize) -> Option<Self>
Construct a new index into a PerPlayer
collection. Returns None
if the provided index
value is out-of-range for the number of players in the game.
Predefined indexes for games of up to 16 players are defined in the forN
modules.
§Examples
use t4t::{for2, for8, PlayerIndex};
let p0_opt = PlayerIndex::<2>::new(0);
let p1_opt = PlayerIndex::<2>::new(1);
let p2_opt = PlayerIndex::<2>::new(2);
assert!(p0_opt.is_some());
assert!(p1_opt.is_some());
assert!(p2_opt.is_none());
assert_eq!(p0_opt.unwrap(), for2::P0);
assert_eq!(p1_opt.unwrap(), for2::P1);
assert_eq!(PlayerIndex::<8>::new(3).unwrap(), for8::P3);
assert_eq!(PlayerIndex::<8>::new(5).unwrap(), for8::P5);
Sourcepub fn as_usize(&self) -> usize
pub fn as_usize(&self) -> usize
Get the player index as a plain usize
value.
§Examples
use t4t::{for3, for6};
assert_eq!(for3::P0.as_usize(), 0);
assert_eq!(for3::P2.as_usize(), 2);
assert_eq!(for6::P2.as_usize(), 2);
assert_eq!(for6::P5.as_usize(), 5);
Sourcepub fn num_players(&self) -> usize
pub fn num_players(&self) -> usize
Get the number of players in the game, which corresponds to the numbers of unique indexes in this type.
§Examples
use t4t::{for5, for12};
assert_eq!(for5::P3.num_players(), 5);
assert_eq!(for12::P7.num_players(), 12);
Sourcepub fn all() -> PlayerIndexes<P> ⓘ
pub fn all() -> PlayerIndexes<P> ⓘ
Get an iterator that iterates over all player indexes of a given type.
§Examples
use t4t::{for3, for5, PlayerIndex};
assert_eq!(
PlayerIndex::all().collect::<Vec<PlayerIndex<3>>>(),
vec![for3::P0, for3::P1, for3::P2]
);
assert_eq!(
PlayerIndex::all().collect::<Vec<PlayerIndex<5>>>(),
vec![for5::P0, for5::P1, for5::P2, for5::P3, for5::P4]
);
Sourcepub fn next(&self) -> Self
pub fn next(&self) -> Self
Get the index after this one, wrapping around to zero if this index is the last.
§Examples
use t4t::{for2, for4, PlayerIndex};
assert_eq!(for2::P0.next(), for2::P1);
assert_eq!(for2::P1.next(), for2::P0);
assert_eq!(for4::P0.next(), for4::P1);
assert_eq!(for4::P1.next(), for4::P2);
assert_eq!(for4::P2.next(), for4::P3);
assert_eq!(for4::P3.next(), for4::P0);
Sourcepub fn previous(&self) -> Self
pub fn previous(&self) -> Self
Get the index before this one, wrapping around to P-1
if this index is zero.
§Examples
use t4t::{for2, for4, PlayerIndex};
assert_eq!(for2::P1.previous(), for2::P0);
assert_eq!(for2::P0.previous(), for2::P1);
assert_eq!(for4::P3.previous(), for4::P2);
assert_eq!(for4::P2.previous(), for4::P1);
assert_eq!(for4::P1.previous(), for4::P0);
assert_eq!(for4::P0.previous(), for4::P3);
Trait Implementations§
Source§impl<const P: usize> Clone for PlayerIndex<P>
impl<const P: usize> Clone for PlayerIndex<P>
Source§fn clone(&self) -> PlayerIndex<P>
fn clone(&self) -> PlayerIndex<P>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<const P: usize> Debug for PlayerIndex<P>
impl<const P: usize> Debug for PlayerIndex<P>
Source§impl<const P: usize> Display for PlayerIndex<P>
impl<const P: usize> Display for PlayerIndex<P>
Source§impl<const P: usize> From<PlayerIndex<P>> for usize
impl<const P: usize> From<PlayerIndex<P>> for usize
Source§fn from(index: PlayerIndex<P>) -> usize
fn from(index: PlayerIndex<P>) -> usize
Source§impl<const P: usize> Hash for PlayerIndex<P>
impl<const P: usize> Hash for PlayerIndex<P>
Source§impl<const P: usize> PartialEq for PlayerIndex<P>
impl<const P: usize> PartialEq for PlayerIndex<P>
impl<const P: usize> Copy for PlayerIndex<P>
impl<const P: usize> Eq for PlayerIndex<P>
impl<const P: usize> StructuralPartialEq for PlayerIndex<P>
Auto Trait Implementations§
impl<const P: usize> Freeze for PlayerIndex<P>
impl<const P: usize> RefUnwindSafe for PlayerIndex<P>
impl<const P: usize> Send for PlayerIndex<P>
impl<const P: usize> Sync for PlayerIndex<P>
impl<const P: usize> Unpin for PlayerIndex<P>
impl<const P: usize> UnwindSafe for PlayerIndex<P>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more