1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//! Module for ruling in the game of go.

use crate::pieces::stones::{Color, Stone};
use crate::pieces::util::coord::Coord;
use crate::rules::game::Game;
use std::ops::Not;

pub mod game;
mod sgf_bridge;

#[derive(Debug, Clone, PartialEq, Eq, Copy)]
pub enum Player {
    White,
    Black,
}

impl Not for Player {
    type Output = Player;

    fn not(self) -> Self::Output {
        match self {
            Player::Black => Player::White,
            Player::White => Player::Black,
        }
    }
}

impl Player {
    pub fn get_stone_color(self) -> Color {
        match self {
            Player::Black => Color::Black,
            Player::White => Color::White,
        }
    }
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum GobanSizes {
    Nineteen,
    Nine,
    Thirteen,
}

impl Into<usize> for GobanSizes {
    fn into(self) -> usize {
        match self {
            GobanSizes::Nine => 9,
            GobanSizes::Thirteen => 13,
            GobanSizes::Nineteen => 19,
        }
    }
}

impl From<usize> for GobanSizes {
    fn from(x: usize) -> Self {
        match x {
            9 => GobanSizes::Nine,
            13 => GobanSizes::Thirteen,
            19 => GobanSizes::Nineteen,
            _ => panic!("Not implemented for others size than 9,13,19"),
        }
    }
}

/// Enum for playing in the Goban.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Move {
    Pass,
    Resign(Player),
    Play(usize, usize),
}

impl From<Coord> for Move {
    fn from(x: (usize, usize)) -> Self {
        Move::Play(x.0, x.1)
    }
}

#[derive(Debug, Clone, PartialEq, Copy)]
pub enum EndGame {
    WinnerByScore(Player, f32),
    WinnerByResign(Player),
    WinnerByTime(Player),
    WinnerByForfeit(Player),
    Draw,
}

impl EndGame {
    ///
    /// Return the winner of the game, if none the game is draw.
    ///
    pub fn get_winner(self) -> Option<Player> {
        match self {
            EndGame::WinnerByScore(p, _) => Some(p),
            EndGame::WinnerByResign(p) => Some(p),
            EndGame::WinnerByTime(p) => Some(p),
            EndGame::WinnerByForfeit(p) => Some(p),
            EndGame::Draw => None,
        }
    }
}

#[derive(Clone, Eq, PartialEq, Debug)]
pub enum PlayError {
    Ko,
    Suicide,
    GamePaused,
}

///
/// This enum describes the rules for the game.
/// for example in chinese rules we don't count prisoners.
///
#[derive(Clone, Eq, PartialEq, Debug, Copy)]
pub enum Rule {
    Japanese,
    Chinese,
}

impl Rule {
    ///
    /// Count the points of the game
    ///
    pub fn count_points(self, game: &Game) -> (f32, f32) {
        match self {
            Rule::Japanese => {
                let mut scores = game.goban().calculate_territories();
                scores.0 += game.prisoners().0 as f32;
                scores.1 += game.prisoners().1 as f32;
                scores.1 += game.komi();

                scores
            }
            Rule::Chinese => {
                // Territories in seki are not counted
                let mut scores = game.goban().calculate_territories();
                let ns = game.goban().number_of_stones();
                scores.0 += ns.0 as f32;
                scores.1 += ns.1 as f32;
                scores.1 += game.komi();
                scores
            }
        }
    }
    ///
    /// Specify the constraints in the move validation by rule.
    ///
    pub fn move_validation(self, game: &Game, stone: Stone) -> Option<PlayError> {
        match self {
            Rule::Japanese | Rule::Chinese => {
                if game.is_suicide(stone) {
                    Some(PlayError::Suicide)
                } else if game.ko(stone) {
                    Some(PlayError::Ko)
                } else {
                    None
                }
            }
        }
    }

    pub fn is_suicide_valid(self) -> bool {
        false
    }

    pub fn from_sgf_code(s: &str) -> Result<Rule, String> {
        match s {
            "JAP" => Ok(Rule::Japanese),
            "CHI" => Ok(Rule::Chinese),
            _ => Err("The rule is not implemented yet.".to_string()),
        }
    }
}