1
//! Authors: Maurice Laveaux and Sjef van Loo
2
use core::fmt;
3

            
4
use crate::Priority;
5

            
6
/// The two players in a parity game.
7
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
8
pub enum Player {
9
    Even,
10
    Odd,
11
}
12

            
13
impl Player {
14
    /// Constructs a player from its index. This can be used in algorithms where
15
    /// we have a 2-array, and 0 is Even and 1 is Odd.
16
79004
    pub fn from_index(index: u8) -> Self {
17
79004
        match index {
18
37605
            0 => Player::Even,
19
41399
            1 => Player::Odd,
20
            _ => panic!("Invalid player index {}", index),
21
        }
22
79004
    }
23

            
24
    /// Constructs a player from a priority.
25
36500
    pub fn from_priority(priority: &Priority) -> Self {
26
36500
        if priority.value().is_multiple_of(2) {
27
17375
            Player::Even
28
        } else {
29
19125
            Player::Odd
30
        }
31
36500
    }
32

            
33
    /// Returns the index of the player, the inverse of [Self::from_index].
34
34824
    pub fn to_index(&self) -> usize {
35
34824
        match self {
36
17001
            Player::Even => 0,
37
17823
            Player::Odd => 1,
38
        }
39
34824
    }
40

            
41
    /// Returns the opponent of the current player.
42
77378
    pub fn opponent(&self) -> Self {
43
77378
        match self {
44
38861
            Player::Even => Player::Odd,
45
38517
            Player::Odd => Player::Even,
46
        }
47
77378
    }
48

            
49
    /// Returns the string representation of the solution for this player.
50
    pub fn solution(&self) -> &'static str {
51
        match self {
52
            Player::Even => "true",
53
            Player::Odd => "false",
54
        }
55
    }
56
}
57

            
58
impl fmt::Display for Player {
59
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60
        match self {
61
            Player::Even => write!(f, "even"),
62
            Player::Odd => write!(f, "odd"),
63
        }
64
    }
65
}