t4t/
ply.rs

1use crate::{Move, Past, PlayerIndex, Transcript};
2
3/// A [ply](https://en.wikipedia.org/wiki/Ply_(game_theory)) is a single move played during a
4/// sequential game.
5#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
6pub struct Ply<M, const P: usize> {
7    /// The player that played the move, or `None` if it was a move of chance.
8    pub player: Option<PlayerIndex<P>>,
9    /// The move that was played.
10    pub the_move: M,
11}
12
13impl<M: Move, const P: usize> Ply<M, P> {
14    /// Construct a new played move.
15    pub fn new(player: Option<PlayerIndex<P>>, the_move: M) -> Self {
16        Ply { player, the_move }
17    }
18
19    /// Construct a move played by the given player.
20    pub fn player(player: PlayerIndex<P>, the_move: M) -> Self {
21        Ply::new(Some(player), the_move)
22    }
23
24    /// Construct a move played by chance.
25    pub fn chance(the_move: M) -> Self {
26        Ply::new(None, the_move)
27    }
28
29    /// Was this move played by a player (and not chance)?
30    pub fn is_player(&self) -> bool {
31        self.player.is_some()
32    }
33
34    /// Was this move played by chance?
35    pub fn is_chance(&self) -> bool {
36        self.player.is_none()
37    }
38}
39
40/// An iterator over the plies in a game.
41pub type Plies<'a, M, const P: usize> = Past<'a, Ply<M, P>>;
42
43impl<'a, M: Move, const P: usize> Plies<'a, M, P> {
44    /// Collect the plies in this iterator into a transcript.
45    pub fn into_transcript(self) -> Transcript<M, P> {
46        Transcript::from_plies(self)
47    }
48}