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
use std::str::FromStr;
use crate::{Board, ChessMove, Color, Square};
#[derive(Copy, Clone, Eq, PartialEq, Default, Debug)]
pub enum GameState {
#[default]
Ongoing,
Checkmates(Color),
Stalemate,
DrawAccepted,
DrawDeclared,
Resigns(Color),
}
impl GameState {
pub fn is_ongoing(&self) -> bool {
matches!(self, GameState::Ongoing)
}
pub fn is_finish(&self) -> bool {
!matches!(self, GameState::Ongoing)
}
}
#[derive(Clone, Eq, PartialEq, Default, Debug)]
pub struct Chess {
pub(crate) board: Board,
pub(crate) square_focused: Option<Square>,
pub(crate) offer_draw: bool,
pub(crate) history: Vec<String>,
pub(crate) state: GameState,
}
impl Chess {
pub fn new(board: Board) -> Self {
Chess {
board,
square_focused: None,
offer_draw: false,
history: vec![],
state: GameState::Ongoing,
}
}
pub fn history(&self) -> Vec<String> {
self.history.clone()
}
pub fn undo(&mut self) {
if let Some(fen) = self.history.pop() {
self.board = Board::from_str(fen.as_str()).expect("valid fen from history");
}
}
pub fn reset(&mut self) {
self.board = Board::default();
self.offer_draw = false;
self.square_focused = None;
self.history = vec![];
self.state = GameState::Ongoing;
}
pub fn state(&mut self) -> GameState {
self.state
}
pub fn play(&mut self, from: Square, to: Square) {
let m = ChessMove::new(from, to);
if self.board.is_legal(m) {
self.history.push(self.board.to_string());
self.board.update(m);
if self.offer_draw {
self.offer_draw = false;
}
self.state = self.board.state();
}
self.square_focused = None;
}
pub fn offer_draw(&mut self) {
self.offer_draw = true;
}
pub fn accept_draw(&mut self) {
self.state = GameState::DrawAccepted;
}
pub fn can_declare_draw(&self) -> bool {
let t = self.history.len();
if t >= 8 {
let fen_boards = [
self.board
.to_string()
.split_once(' ')
.unwrap()
.0
.to_string(),
self.history[t - 4].split_once(' ').unwrap().0.to_string(),
self.history[t - 8].split_once(' ').unwrap().0.to_string(),
];
if fen_boards[0] == fen_boards[1] && fen_boards[1] == fen_boards[2] {
return true;
}
if self.board.halfmoves() >= 100 {
return true;
}
}
false
}
pub fn declare_draw(&mut self) {
self.state = GameState::DrawDeclared;
}
pub fn resign(&mut self, color: Color) {
self.state = GameState::Resigns(color);
}
}