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
143028
    pub fn from_index(index: u8) -> Self {
17
143028
        match index {
18
50857
            0 => Player::Even,
19
92171
            1 => Player::Odd,
20
            _ => panic!("Invalid player index {}", index),
21
        }
22
143028
    }
23

            
24
    /// Constructs a player from a priority.
25
5737
    pub fn from_priority(priority: &Priority) -> Self {
26
5737
        if priority.value() % 2 == 0 {
27
3198
            Player::Even
28
        } else {
29
2539
            Player::Odd
30
        }
31
5737
    }
32

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

            
41
    /// Returns the opponent of the current player.
42
11420
    pub fn opponent(&self) -> Self {
43
11420
        match self {
44
6050
            Player::Even => Player::Odd,
45
5370
            Player::Odd => Player::Even,
46
        }
47
11420
    }
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
}