Struct PlayerIndex

Source
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>

Source

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);
Source

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);
Source

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);
Source

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]
);
Source

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);
Source

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>

Source§

fn clone(&self) -> PlayerIndex<P>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<const P: usize> Debug for PlayerIndex<P>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const P: usize> Display for PlayerIndex<P>

Source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const P: usize> From<PlayerIndex<P>> for usize

Source§

fn from(index: PlayerIndex<P>) -> usize

Converts to this type from the input type.
Source§

impl<const P: usize> Hash for PlayerIndex<P>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T, const P: usize> Index<PlayerIndex<P>> for PerPlayer<T, P>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, idx: PlayerIndex<P>) -> &T

Performs the indexing (container[index]) operation. Read more
Source§

impl<M, const P: usize> Index<PlayerIndex<P>> for Profile<M, P>

Source§

type Output = M

The returned type after indexing.
Source§

fn index(&self, idx: PlayerIndex<P>) -> &M

Performs the indexing (container[index]) operation. Read more
Source§

impl<T, const P: usize> IndexMut<PlayerIndex<P>> for PerPlayer<T, P>

Source§

fn index_mut(&mut self, idx: PlayerIndex<P>) -> &mut T

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<M, const P: usize> IndexMut<PlayerIndex<P>> for Profile<M, P>

Source§

fn index_mut(&mut self, idx: PlayerIndex<P>) -> &mut M

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<const P: usize> PartialEq for PlayerIndex<P>

Source§

fn eq(&self, other: &PlayerIndex<P>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<const P: usize> Copy for PlayerIndex<P>

Source§

impl<const P: usize> Eq for PlayerIndex<P>

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> Move for T
where T: Copy + Debug + Eq + Hash + Send + Sync + 'static,

Source§

impl<T> State for T
where T: Clone + Debug + PartialEq + Send + Sync + 'static,