Skip to content
Navigation Menu
{{ message }}
forked from RedDragonWebDesign/php-chess
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessMove.php
More file actions
260 lines (234 loc) · 8.53 KB
/
Copy pathChessMove.php
File metadata and controls
260 lines (234 loc) · 8.53 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
class ChessMove {
const PIECE_LETTERS = array(
ChessPiece::PAWN => 'p',
ChessPiece::KNIGHT => 'n',
ChessPiece::BISHOP => 'b',
ChessPiece::ROOK => 'r',
ChessPiece::QUEEN => 'q',
ChessPiece::KING => 'k'
);
public $starting_square;
public $ending_square;
public $color;
public $piece_type;
public $capture;
public $check = FALSE;
public $checkmate = FALSE;
public $promotion_piece_type = NULL; // Use the setter to change this. Keeping public so it can be read publicly.
public $en_passant = FALSE;
public $disambiguation = '';
public $castling = FALSE;
public $board;
function __construct(
ChessSquare $starting_square,
ChessSquare $ending_square,
$color,
$piece_type,
bool $capture,
ChessBoard $old_board,
bool $store_board = TRUE
) {
$this->starting_square = $starting_square;
$this->ending_square = $ending_square;
$this->color = $color;
$this->piece_type = $piece_type;
$this->capture = $capture;
// Adding $store_board sped up the code by 300ms
if ( $store_board ) {
$this->board = clone $old_board;
// Perft uses an empty move to store a board. If not empty move, modify the board.
if ( $this->starting_square ) {
$this->board->make_move($starting_square, $ending_square);
$this->possibly_remove_our_castling_privileges();
$this->possibly_remove_enemy_castling_privileges();
$this->if_castling_move_rook();
}
}
}
function possibly_remove_our_castling_privileges(): void {
// if our king or rook moves update the FEN to take away our castling privileges
if ( $this->color == ChessPiece::BLACK ) {
if ( $this->piece_type == ChessPiece::KING && $this->starting_square->get_alphanumeric() == 'e8' ) {
$this->board->castling['black_can_castle_kingside'] = FALSE;
$this->board->castling['black_can_castle_queenside'] = FALSE;
} elseif ( $this->piece_type == ChessPiece::ROOK && $this->starting_square->get_alphanumeric() == 'a8' ) {
$this->board->castling['black_can_castle_queenside'] = FALSE;
} elseif ( $this->piece_type == ChessPiece::ROOK && $this->starting_square->get_alphanumeric() == 'h8' ) {
$this->board->castling['black_can_castle_kingside'] = FALSE;
}
} elseif ( $this->color == ChessPiece::WHITE ) {
if ( $this->piece_type == ChessPiece::KING && $this->starting_square->get_alphanumeric() == 'e1' ) {
$this->board->castling['white_can_castle_kingside'] = FALSE;
$this->board->castling['white_can_castle_queenside'] = FALSE;
} elseif ( $this->piece_type == ChessPiece::ROOK && $this->starting_square->get_alphanumeric() == 'a1' ) {
$this->board->castling['white_can_castle_queenside'] = FALSE;
} elseif ( $this->piece_type == ChessPiece::ROOK && $this->starting_square->get_alphanumeric() == 'h1' ) {
$this->board->castling['white_can_castle_kingside'] = FALSE;
}
}
}
function possibly_remove_enemy_castling_privileges(): void {
// If an enemy rook is captured, update the FEN to take away enemy castling privileges.
// We'll keep it simple. Anytime a piece moves into a corner square (a1, a8, h1, h8),
// remove the other side's castling privileges.
if ( $this->color == ChessPiece::BLACK ) {
if ( $this->ending_square->get_alphanumeric() == 'a1' ) {
$this->board->castling['white_can_castle_queenside'] = FALSE;
} elseif ( $this->ending_square->get_alphanumeric() == 'h1' ) {
$this->board->castling['white_can_castle_kingside'] = FALSE;
}
} elseif ( $this->color == ChessPiece::WHITE ) {
if ( $this->ending_square->get_alphanumeric() == 'a8' ) {
$this->board->castling['black_can_castle_queenside'] = FALSE;
} elseif ( $this->ending_square->get_alphanumeric() == 'h8' ) {
$this->board->castling['black_can_castle_kingside'] = FALSE;
}
}
}
function if_castling_move_rook(): void {
// if castling, move the rook into the right place
if ( $this->color == ChessPiece::BLACK ) {
if (
$this->piece_type == ChessPiece::KING &&
$this->starting_square->get_alphanumeric() == 'e8' &&
$this->ending_square->get_alphanumeric() == 'g8'
) {
$starting_square = new ChessSquare('h8');
$ending_square = new ChessSquare('f8');
$this->board->make_additional_move_on_same_turn($starting_square, $ending_square);
$this->castling = TRUE;
} elseif (
$this->piece_type == ChessPiece::KING &&
$this->starting_square->get_alphanumeric() == 'e8' &&
$this->ending_square->get_alphanumeric() == 'c8'
) {
$starting_square = new ChessSquare('a8');
$ending_square = new ChessSquare('d8');
$this->board->make_additional_move_on_same_turn($starting_square, $ending_square);
$this->castling = TRUE;
}
} elseif ( $this->color == ChessPiece::WHITE ) {
if (
$this->piece_type == ChessPiece::KING &&
$this->starting_square->get_alphanumeric() == 'e1' &&
$this->ending_square->get_alphanumeric() == 'g1'
) {
$starting_square = new ChessSquare('h1');
$ending_square = new ChessSquare('f1');
$this->board->make_additional_move_on_same_turn($starting_square, $ending_square);
$this->castling = TRUE;
} elseif (
$this->piece_type == ChessPiece::KING &&
$this->starting_square->get_alphanumeric() == 'e1' &&
$this->ending_square->get_alphanumeric() == 'c1'
) {
$starting_square = new ChessSquare('a1');
$ending_square = new ChessSquare('d1');
$this->board->make_additional_move_on_same_turn($starting_square, $ending_square);
$this->castling = TRUE;
}
}
}
// Do a deep clone. Needed for pawn promotion.
function __clone() {
$this->starting_square = clone $this->starting_square;
$this->ending_square = clone $this->ending_square;
if ( $this->board ) {
$this->board = clone $this->board;
}
}
function set_promotion_piece($piece_type): void {
// update the piece
$rank = $this->ending_square->rank;
$file = $this->ending_square->file;
if ( $this->board ) {
$this->board->board[$rank][$file]->type = $piece_type;
}
// update the notation
$this->promotion_piece_type = $piece_type;
}
function get_notation(): string {
$string = '';
if (
$this->starting_square->get_alphanumeric() == 'e8' &&
$this->ending_square->get_alphanumeric() == 'g8' &&
$this->piece_type == ChessPiece::KING &&
$this->color == ChessPiece::BLACK
) {
$string .= 'O-O';
} elseif (
$this->starting_square->get_alphanumeric() == 'e1' &&
$this->ending_square->get_alphanumeric() == 'g1' &&
$this->piece_type == ChessPiece::KING &&
$this->color == ChessPiece::WHITE
) {
$string .= 'O-O';
} elseif (
$this->starting_square->get_alphanumeric() == 'e8' &&
$this->ending_square->get_alphanumeric() == 'c8' &&
$this->piece_type == ChessPiece::KING &&
$this->color == ChessPiece::BLACK
) {
$string .= 'O-O-O';
} elseif (
$this->starting_square->get_alphanumeric() == 'e1' &&
$this->ending_square->get_alphanumeric() == 'c1' &&
$this->piece_type == ChessPiece::KING &&
$this->color == ChessPiece::WHITE
) {
$string .= 'O-O-O';
} else {
// type of piece
if ( $this->piece_type == ChessPiece::PAWN && $this->capture ) {
$string .= substr($this->starting_square->get_alphanumeric(), 0, 1);
} elseif ( $this->piece_type != ChessPiece::PAWN ) {
$string .= strtoupper(self::PIECE_LETTERS[$this->piece_type]);
}
// disambiguation of rank/file/square
$string .= $this->disambiguation;
// capture?
if ( $this->capture ) {
$string .= 'x';
}
// destination square
$string .= $this->ending_square->get_alphanumeric();
// en passant
if ( $this->en_passant ) {
$string .= 'e.p.';
}
// pawn promotion
if ( $this->promotion_piece_type == ChessPiece::QUEEN ) {
$string .= '=Q';
} elseif ( $this->promotion_piece_type == ChessPiece::ROOK ) {
$string .= '=R';
} elseif ( $this->promotion_piece_type == ChessPiece::BISHOP ) {
$string .= '=B';
} elseif ( $this->promotion_piece_type == ChessPiece::KNIGHT ) {
$string .= '=N';
}
}
// check or checkmate
if ( $this->checkmate ) {
$string .= '#';
} elseif ( $this->check ) {
$string .= '+';
}
return $string;
}
function get_coordinate_notation(): string {
// Automatically pick queen when drag and dropping.
if (
$this->promotion_piece_type == ChessPiece::ROOK ||
$this->promotion_piece_type == ChessPiece::BISHOP ||
$this->promotion_piece_type == ChessPiece::KNIGHT
) {
return "";
} else {
return $this->starting_square->get_alphanumeric() . $this->ending_square->get_alphanumeric();
}
}
function get_piece_letter(): string {
return strtoupper(self::PIECE_LETTERS[$this->piece_type]);
}
}
You can’t perform that action at this time.
