Skip to main content

GameAnalyzer

Struct GameAnalyzer 

Source
pub struct GameAnalyzer<G = ()> {
    game: G,
}
Expand description

Owns a fixed game definition and reusable analysis caches. Move generators may collect statistics, but must keep the rules unchanged. Closure-based games must be finite and acyclic; detected cycles panic. Outcome queries stop exploring a position once a winning move is found. Use impartial_graph for Win/Draw/Lose analysis of cyclic graphs.

use competitive::algorithm::{GameAnalyzer, GameOutcome};
let mut nim = GameAnalyzer::new().impartial(|&n: &usize| 0..n);
assert_eq!(nim.grundy().eval(&3), 3);
assert_eq!(nim.sum().grundy().eval(&[3, 3]), 0);
assert_eq!(nim.misere().outcome().eval(&1), GameOutcome::Lose);
assert_eq!(nim.outcome().eval(&1), GameOutcome::Win);
assert_eq!(nim.grundy().verify(0..10, |&n| n as u64), None);

Fields§

§game: G

Implementations§

Source§

impl GameAnalyzer

Source

pub fn impartial_graph<G>( self, graph: &G, ) -> GameAnalyzer<ImpartialGraph<'_, G>>
where G: Graph + VertexMap<Option<usize>>,

Uses the graph’s shared moves for both players. Query vertices must be valid inputs to the graph’s VertexMap operations.

Source§

impl<'g, G> GameAnalyzer<ImpartialGraph<'g, G>>
where G: Graph + VertexMap<Option<usize>>,

Source

pub fn grundy( &mut self, ) -> GameAnalysis<'_, ImpartialGraph<'g, G>, G::Vertex, u64>

Source

pub fn value( &mut self, ) -> GameAnalysis<'_, ImpartialGraph<'g, G>, G::Vertex, GameValue>

Source

pub fn outcome( &mut self, ) -> GameAnalysis<'_, ImpartialGraph<'g, G>, G::Vertex, GameOutcome>

Source

pub fn misere(&mut self) -> Misere<'_, ImpartialGraph<'g, G>>

Source

pub fn sum(&mut self) -> GameSum<'_, ImpartialGraph<'g, G>>

Source

pub fn clear_cache(&mut self)

Source§

impl GameAnalyzer

Source

pub fn impartial<S, F, I>( self, moves: F, ) -> GameAnalyzer<Impartial<S, impl FnMut(&S) -> Map<I::IntoIter, fn(S) -> Neighbor<S, ()>>>>
where F: FnMut(&S) -> I, I: IntoIterator<Item = S>,

Source

pub fn impartial_moves<S, L, F, I>( self, moves: F, ) -> GameAnalyzer<Impartial<S, F>>
where F: FnMut(&S) -> I, I: IntoIterator<Item = Neighbor<S, L>>,

Examples found in repository?
crates/competitive/src/algorithm/game_analyzer/impartial.rs (line 27)
16    pub fn impartial<S, F, I>(
17        self,
18        mut moves: F,
19    ) -> GameAnalyzer<
20        Impartial<S, impl FnMut(&S) -> std::iter::Map<I::IntoIter, fn(S) -> Neighbor<S, ()>>>,
21    >
22    where
23        F: FnMut(&S) -> I,
24        I: IntoIterator<Item = S>,
25    {
26        let label: fn(S) -> Neighbor<S, ()> = |to| Neighbor::new(to, ());
27        self.impartial_moves(move |s: &S| moves(s).into_iter().map(label))
28    }
Source§

impl<S, F> GameAnalyzer<Impartial<S, F>>

Source

pub fn misere(&mut self) -> Misere<'_, Impartial<S, F>>

Source

pub fn sum(&mut self) -> GameSum<'_, Impartial<S, F>>

Source

pub fn clear_cache(&mut self)

Source§

impl<S, L, F, I> GameAnalyzer<Impartial<S, F>>
where S: Clone + Eq + Hash, F: FnMut(&S) -> I, I: IntoIterator<Item = Neighbor<S, L>>,

Source

pub fn grundy(&mut self) -> GameAnalysis<'_, Impartial<S, F>, S, u64>

Source

pub fn value(&mut self) -> GameAnalysis<'_, Impartial<S, F>, S, GameValue>

Source

pub fn outcome(&mut self) -> GameAnalysis<'_, Impartial<S, F>, S, GameOutcome>

Source

pub fn fold<V, C>( &mut self, combine: C, ) -> GameAnalysis<'_, Impartial<S, F>, S, V, false, impl FnMut(&mut Impartial<S, F>, &S) -> V>
where V: Clone, C: FnMut(&S, &[(L, V)]) -> V,

combine receives each move’s label and the value of its destination.

Source§

impl GameAnalyzer

Source

pub fn partisan<S, FL, FR, IL, IR>( self, left: FL, right: FR, ) -> GameAnalyzer<Partisan<S, impl FnMut(&S) -> Map<IL::IntoIter, fn(S) -> Neighbor<S, ()>>, impl FnMut(&S) -> Map<IR::IntoIter, fn(S) -> Neighbor<S, ()>>>>
where FL: FnMut(&S) -> IL, FR: FnMut(&S) -> IR, IL: IntoIterator<Item = S>, IR: IntoIterator<Item = S>,

Source

pub fn partisan_moves<S, L, FL, FR, IL, IR>( self, left: FL, right: FR, ) -> GameAnalyzer<Partisan<S, FL, FR>>
where FL: FnMut(&S) -> IL, FR: FnMut(&S) -> IR, IL: IntoIterator<Item = Neighbor<S, L>>, IR: IntoIterator<Item = Neighbor<S, L>>,

Examples found in repository?
crates/competitive/src/algorithm/game_analyzer/partisan.rs (lines 37-40)
19    pub fn partisan<S, FL, FR, IL, IR>(
20        self,
21        mut left: FL,
22        mut right: FR,
23    ) -> GameAnalyzer<
24        Partisan<
25            S,
26            impl FnMut(&S) -> std::iter::Map<IL::IntoIter, fn(S) -> Neighbor<S, ()>>,
27            impl FnMut(&S) -> std::iter::Map<IR::IntoIter, fn(S) -> Neighbor<S, ()>>,
28        >,
29    >
30    where
31        FL: FnMut(&S) -> IL,
32        FR: FnMut(&S) -> IR,
33        IL: IntoIterator<Item = S>,
34        IR: IntoIterator<Item = S>,
35    {
36        let label: fn(S) -> Neighbor<S, ()> = |to| Neighbor::new(to, ());
37        self.partisan_moves(
38            move |s: &S| left(s).into_iter().map(label),
39            move |s: &S| right(s).into_iter().map(label),
40        )
41    }
Source§

impl<S, FL, FR> GameAnalyzer<Partisan<S, FL, FR>>

Source

pub fn misere(&mut self) -> Misere<'_, Partisan<S, FL, FR>>

Source

pub fn sum(&mut self) -> GameSum<'_, Partisan<S, FL, FR>>

Source

pub fn clear_cache(&mut self)

Source§

impl<S, L, FL, FR, IL, IR> GameAnalyzer<Partisan<S, FL, FR>>
where S: Clone + Eq + Hash, FL: FnMut(&S) -> IL, FR: FnMut(&S) -> IR, IL: IntoIterator<Item = Neighbor<S, L>>, IR: IntoIterator<Item = Neighbor<S, L>>,

Source

pub fn value(&mut self) -> GameAnalysis<'_, Partisan<S, FL, FR>, S, GameValue>

Source

pub fn outcome( &mut self, ) -> GameAnalysis<'_, Partisan<S, FL, FR>, S, PartisanOutcome>

Source

pub fn fold<V, C>( &mut self, combine: C, ) -> GameAnalysis<'_, Partisan<S, FL, FR>, S, V, false, impl FnMut(&mut Partisan<S, FL, FR>, &S) -> V>
where V: Clone, C: FnMut(&S, &[(Player, L, V)]) -> V,

combine receives all Left options followed by all Right options.

Source§

impl GameAnalyzer

Source

pub fn scoring<S, L, F, T, I>( self, moves: F, terminal: T, ) -> GameAnalyzer<Scoring<S, F, T>>
where F: FnMut(&S) -> I, I: IntoIterator<Item = ScoreMove<S, L>>, T: FnMut(&S) -> Option<i64>,

Scores and rewards are measured for the player whose turn it is. terminal returns Some(score) at terminal states and None elsewhere.

Source§

impl<S, F, T> GameAnalyzer<Scoring<S, F, T>>

Source

pub fn clear_cache(&mut self)

Source§

impl<S, L, F, T, I> GameAnalyzer<Scoring<S, F, T>>
where S: Clone + Eq + Hash, F: FnMut(&S) -> I, I: IntoIterator<Item = ScoreMove<S, L>>, T: FnMut(&S) -> Option<i64>,

Source

pub fn score(&mut self) -> GameAnalysis<'_, Scoring<S, F, T>, S, i64>

Source§

impl GameAnalyzer

Source

pub fn new() -> Self

Trait Implementations§

Source§

impl<G: Default> Default for GameAnalyzer<G>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<G> Forget for GameAnalyzer<G>

§

impl<G> Freeze for GameAnalyzer<G>
where G: Freeze,

§

impl<G> Move for GameAnalyzer<G>

§

impl<G> RefUnwindSafe for GameAnalyzer<G>
where G: RefUnwindSafe,

§

impl<G> Send for GameAnalyzer<G>
where G: Send,

§

impl<G> Sync for GameAnalyzer<G>
where G: Sync,

§

impl<G> Unpin for GameAnalyzer<G>
where G: Unpin,

§

impl<G> UnsafeUnpin for GameAnalyzer<G>
where G: UnsafeUnpin,

§

impl<G> UnwindSafe for GameAnalyzer<G>
where G: UnwindSafe,

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> 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> ToArrayVecScalar for T

Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.