Rev 140 | Rev 172 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | pmbaty | 1 | // board.cpp |
2 | |||
3 | #include "common.h" |
||
4 | |||
5 | |||
6 | // initial position of a chess game : |
||
7 | // |
||
8 | // +-----------------+ |
||
9 | // 8| R K B Q K B K R | - black |
||
10 | // 7| P P P P P P P P | |
||
11 | // 6| | |
||
12 | // 5| | |
||
13 | // 4| | |
||
14 | // 3| | |
||
15 | // 2| P P P P P P P P | |
||
16 | // 1| R K B Q K B K R | - white |
||
17 | // +-----------------+ |
||
18 | // a b c d e f g h |
||
19 | |||
20 | |||
21 | bool Board_Init (board_t *board, int white_playertype, int black_playertype, wchar_t *fen_string) |
||
22 | { |
||
23 | // this function initializes a new chess game |
||
24 | |||
25 | bool is_success; |
||
26 | |||
27 | // initialize the moves array and blank out the first move's structure |
||
28 | board->moves = (boardmove_t *) SAFE_malloc (1, sizeof (boardmove_t), false); |
||
29 | memset (&board->moves[0], 0, sizeof (boardmove_t)); |
||
30 | |||
31 | board->viewed_move = 0; |
||
32 | board->move_count = 1; |
||
33 | board->game_state = STATE_UNKNOWN; // game has not started yet |
||
34 | board->lastmove_time = 0.0f; |
||
35 | |||
36 | // initialize this board's players |
||
37 | Player_Init (&board->players[COLOR_WHITE], COLOR_WHITE, white_playertype); |
||
38 | Player_Init (&board->players[COLOR_BLACK], COLOR_BLACK, black_playertype); |
||
39 | |||
40 | // can we NOT successfully initialize from the given FEN string ? |
||
41 | is_success = Move_SetupFromFEN (&board->moves[0], fen_string); |
||
42 | if (!is_success) |
||
43 | Move_SetupFromFEN (&board->moves[0], FENSTARTUP_STANDARDCHESS); // if so, fallback to standard chess table |
||
44 | |||
45 | // reset all selection and hovering information |
||
46 | Board_SetSelectedAndHovered (board, -1, -1, -1, -1); |
||
47 | |||
48 | // notify that board was just set up |
||
49 | board->was_setup = true; |
||
171 | pmbaty | 50 | board->players[COLOR_WHITE].should_wakeup = true; |
51 | board->players[COLOR_BLACK].should_wakeup = true; |
||
124 | pmbaty | 52 | board->reevaluate = true; |
1 | pmbaty | 53 | |
54 | return (is_success); // finished |
||
55 | } |
||
56 | |||
57 | |||
58 | void Board_Shutdown (board_t *board) |
||
59 | { |
||
60 | // this function ends a chess game |
||
61 | |||
62 | boardmove_t *move; |
||
63 | int move_index; |
||
64 | |||
65 | // reset the moves array |
||
66 | for (move_index = 0; move_index < board->move_count; move_index++) |
||
67 | { |
||
68 | move = &board->moves[move_index]; // quick access to move |
||
69 | SAFE_free ((void **) &move->sides[COLOR_WHITE].takenparts); // free white's taken parts |
||
70 | SAFE_free ((void **) &move->sides[COLOR_BLACK].takenparts); // free black's taken parts |
||
71 | SAFE_free ((void **) &move->comment); // free each move comment |
||
72 | } |
||
73 | SAFE_free ((void **) &board->moves); // and the moves array itself |
||
74 | board->viewed_move = -1; |
||
75 | board->move_count = 0; |
||
76 | |||
77 | // release players |
||
78 | Player_Shutdown (&board->players[COLOR_WHITE]); |
||
79 | Player_Shutdown (&board->players[COLOR_BLACK]); |
||
80 | |||
81 | // game has not started yet |
||
82 | board->game_state = STATE_UNKNOWN; |
||
83 | |||
84 | return; // that's all there is |
||
85 | } |
||
86 | |||
87 | |||
88 | bool Board_Reset (board_t *board, wchar_t *fen_string) |
||
89 | { |
||
90 | // this function initializes a chess game according to a FEN string, WITHOUT changing the players |
||
91 | |||
92 | boardmove_t *move; |
||
93 | int move_index; |
||
94 | bool is_success; |
||
95 | |||
96 | // reset the moves array |
||
97 | for (move_index = 0; move_index < board->move_count; move_index++) |
||
98 | { |
||
99 | move = &board->moves[move_index]; // quick access to move |
||
100 | SAFE_free ((void **) &move->sides[COLOR_WHITE].takenparts); // free white's taken parts |
||
101 | SAFE_free ((void **) &move->sides[COLOR_BLACK].takenparts); // free black's taken parts |
||
102 | SAFE_free ((void **) &move->comment); // free each move comment |
||
103 | } |
||
104 | |||
105 | // resize the moves array and blank out the first move's structure |
||
106 | board->moves = (boardmove_t *) SAFE_realloc (board->moves, board->move_count, 1, sizeof (boardmove_t), false); |
||
107 | memset (&board->moves[0], 0, sizeof (boardmove_t)); |
||
108 | |||
109 | board->viewed_move = 0; |
||
110 | board->move_count = 1; |
||
111 | board->game_state = STATE_UNKNOWN; // game has not started yet |
||
112 | board->lastmove_time = 0.0f; |
||
113 | |||
114 | // reset the players' view angles |
||
115 | Player_ResetView (&board->players[COLOR_BLACK]); |
||
116 | Player_ResetView (&board->players[COLOR_WHITE]); |
||
117 | |||
118 | // can we NOT successfully initialize from the given FEN string ? |
||
119 | is_success = Move_SetupFromFEN (&board->moves[0], fen_string); |
||
120 | if (!is_success) |
||
121 | Move_SetupFromFEN (&board->moves[0], FENSTARTUP_STANDARDCHESS); // if so, fallback to standard chess table |
||
122 | |||
123 | // reset all selection and hovering information |
||
124 | Board_SetSelectedAndHovered (board, -1, -1, -1, -1); |
||
125 | |||
126 | // notify that board was just set up |
||
127 | board->was_setup = true; |
||
171 | pmbaty | 128 | board->players[COLOR_WHITE].should_wakeup = true; |
129 | board->players[COLOR_BLACK].should_wakeup = true; |
||
1 | pmbaty | 130 | |
131 | return (is_success); // finished |
||
132 | } |
||
133 | |||
134 | |||
135 | bool Board_Think (board_t *board) |
||
136 | { |
||
137 | // helper function to make both a board's players think |
||
138 | |||
139 | bool do_update; |
||
140 | |||
141 | do_update = false; // don't update scene until told otherwise |
||
142 | |||
143 | do_update |= Player_Think (&board->players[COLOR_WHITE]); // make white player think |
||
144 | do_update |= Player_Think (&board->players[COLOR_BLACK]); // make black player think |
||
145 | |||
146 | // do the board sides need to be swapped ? |
||
147 | if (board->want_playerswap) |
||
148 | { |
||
149 | Board_SwapSides (board); // if so, swap board sides |
||
150 | board->want_playerswap = false; // don't do this all day long |
||
151 | do_update |= true; // and update the scene |
||
152 | } |
||
153 | |||
171 | pmbaty | 154 | // clear board setup notification |
1 | pmbaty | 155 | board->was_setup = false; |
156 | |||
157 | return (do_update); // finished, return whether we update the scene or not |
||
158 | } |
||
159 | |||
160 | |||
161 | void Board_SwapSides (board_t *board) |
||
162 | { |
||
163 | // helper function to swap a board's sides |
||
164 | |||
165 | player_t player_to_swap; |
||
166 | |||
167 | // THREAD-SAFE: wait until all buffers are unlocked |
||
168 | while (board->players[COLOR_BLACK].sendbuffer_locked |
||
169 | || board->players[COLOR_WHITE].sendbuffer_locked) |
||
170 | Sleep (10); // test again in 10 milliseconds |
||
171 | |||
172 | // lock all the buffers at once |
||
173 | board->players[COLOR_BLACK].sendbuffer_locked = true; |
||
174 | board->players[COLOR_WHITE].sendbuffer_locked = true; |
||
175 | |||
176 | // swap players structures |
||
177 | memcpy (&player_to_swap, &board->players[COLOR_BLACK], sizeof (player_t)); |
||
178 | memcpy (&board->players[COLOR_BLACK], &board->players[COLOR_WHITE], sizeof (player_t)); |
||
179 | memcpy (&board->players[COLOR_WHITE], &player_to_swap, sizeof (player_t)); |
||
180 | |||
181 | // restore correct colors |
||
182 | board->players[COLOR_BLACK].color = COLOR_BLACK; |
||
183 | board->players[COLOR_WHITE].color = COLOR_WHITE; |
||
184 | |||
185 | // turn their point of view 180 degrees |
||
186 | board->players[COLOR_BLACK].view_yaw = WrapAngle (board->players[COLOR_BLACK].view_yaw + 180.0f); |
||
187 | board->players[COLOR_WHITE].view_yaw = WrapAngle (board->players[COLOR_WHITE].view_yaw + 180.0f); |
||
188 | |||
171 | pmbaty | 189 | // now unlock all the players' buffers and have them sleep for 2 seconds |
1 | pmbaty | 190 | board->players[COLOR_BLACK].sendbuffer_locked = false; |
191 | board->players[COLOR_WHITE].sendbuffer_locked = false; |
||
171 | pmbaty | 192 | board->players[COLOR_BLACK].sleep_time = current_time + 2.0f; |
193 | board->players[COLOR_WHITE].sleep_time = current_time + 2.0f; |
||
1 | pmbaty | 194 | |
195 | board->reevaluate = true; // remember to reevaluate this board |
||
196 | |||
197 | return; // finished |
||
198 | } |
||
199 | |||
200 | |||
136 | pmbaty | 201 | void Board_EnterSetupPosition (board_t *board) |
202 | { |
||
203 | // helper function to switch to board setup position mode |
||
204 | |||
205 | memset (&board->moves[0].slots, 0, sizeof (board->moves[0].slots)); // erase all of start move's slots |
||
206 | |||
207 | the_board.game_state = STATE_SETUPPOSITION; // game not started yet, enter board setup mode |
||
208 | the_board.reevaluate = true; // evaluate board again |
||
209 | |||
210 | // display the "please choose start position" phrase in the middle of the screen |
||
140 | pmbaty | 211 | Scene_UpdateText (&the_scene.gui.central_text, RGBA_TO_RGBACOLOR (255, 255, 255, 191), DURATION_INFINITE, true, L"\n\n\n\n\n\n%s", LOCALIZE (L"SetupMode")); |
136 | pmbaty | 212 | the_scene.gui.partspick_selectedpart = ' '; // no selected part yet |
213 | the_scene.update = true; // and update the 3D scene |
||
214 | |||
215 | return; // finished |
||
216 | } |
||
217 | |||
218 | |||
1 | pmbaty | 219 | void Board_SetSelectedAndHovered (board_t *board, int selected_line, int selected_column, int hovered_line, int hovered_column) |
220 | { |
||
221 | // helper function to forcibly set a board's selected and hovered slots (respectively move source and destination) |
||
222 | |||
223 | board->selected_position[0] = selected_line; |
||
224 | board->selected_position[1] = selected_column; |
||
225 | board->hovered_position[0] = hovered_line; |
||
226 | board->hovered_position[1] = hovered_column; |
||
227 | |||
228 | return; // finished |
||
229 | } |
||
230 | |||
231 | |||
232 | char Board_ColorToMove (board_t *board) |
||
233 | { |
||
234 | // helper function that returns the current color to move for the given board |
||
235 | // NOTE: since the return value may be used to address an array, DO NOT return an invalid value ! |
||
236 | |||
237 | if ((board->moves == NULL) || (board->move_count < 1)) |
||
238 | return (COLOR_WHITE); // consistency check |
||
239 | |||
240 | return (1 - board->moves[the_board.move_count - 1].color); // return the opposite color of the last move's color |
||
241 | } |
||
242 | |||
243 | |||
244 | void Board_AppendMove (board_t *board, int from_line, int from_column, int to_line, int to_column, char promotion_type, wchar_t *comment) |
||
245 | { |
||
246 | // this function processes a part movement on the specified board. |
||
247 | |||
248 | boardmove_t *last_move; |
||
249 | boardmove_t new_move; |
||
250 | int side_index; |
||
251 | boardside_t *playing_side; |
||
252 | boardside_t *opposing_side; |
||
253 | |||
254 | // get a quick access to board's last move |
||
255 | last_move = &board->moves[board->move_count - 1]; |
||
256 | |||
257 | // prepare the new move |
||
258 | new_move.color = 1 - last_move->color; // switch colors |
||
259 | new_move.part = last_move->slots[from_line][from_column].part; // save the move part type |
||
260 | new_move.promotion_type = promotion_type; // save the promotion type |
||
261 | new_move.has_captured = false; // assume there's no capture until told otherwise |
||
262 | new_move.is_enpassant = false; // assume no en passant coup is played until told otherwise |
||
263 | new_move.is_check = false; // assume no check until told otherwise |
||
264 | new_move.is_stalemate = false; // assume no stalemate until told otherwise |
||
265 | new_move.source[0] = from_line; // save the move source line and column |
||
266 | new_move.source[1] = from_column; |
||
267 | new_move.target[0] = to_line; // save the move target line and column |
||
268 | new_move.target[1] = to_column; |
||
269 | new_move.pgntext[0] = 0; // will be evaluated at the end of this function |
||
270 | new_move.comment = NULL; // assume no comment until told otherwise |
||
271 | new_move.comment_size = 0; |
||
272 | for (side_index = 0; side_index < 2; side_index++) |
||
273 | { |
||
274 | new_move.sides[side_index].takenparts = (unsigned char *) SAFE_malloc (last_move->sides[side_index].takenpart_count, sizeof (unsigned char), false); |
||
275 | memcpy (new_move.sides[side_index].takenparts, last_move->sides[side_index].takenparts, last_move->sides[side_index].takenpart_count * sizeof (unsigned char)); |
||
276 | new_move.sides[side_index].takenpart_count = last_move->sides[side_index].takenpart_count; // copy side's taken parts |
||
277 | new_move.sides[side_index].shortcastle_allowed = last_move->sides[side_index].shortcastle_allowed; // copy whether side can castle short |
||
278 | new_move.sides[side_index].longcastle_allowed = last_move->sides[side_index].longcastle_allowed; // copy whether side can castle long |
||
279 | } |
||
280 | memcpy (&new_move.slots, last_move->slots, sizeof (last_move->slots)); // copy table disposition |
||
281 | new_move.fen_string[0] = 0; // will be evaluated at the end of this function |
||
282 | |||
283 | // get a quick access to current and opposing sides |
||
284 | playing_side = &new_move.sides[new_move.color]; |
||
285 | opposing_side = &new_move.sides[1 - new_move.color]; |
||
286 | |||
287 | // is a piece being taken ? |
||
288 | if (new_move.slots[to_line][to_column].part != PART_NONE) |
||
289 | { |
||
290 | // was the piece being taken a rook ? |
||
291 | if (new_move.slots[to_line][to_column].part == PART_ROOK) |
||
292 | { |
||
293 | // was it a white tower in A1, a white tower in A7, a black tower in H1 or a black tower in H7 ? |
||
294 | if ((new_move.slots[to_line][to_column].color == COLOR_WHITE) && (to_line == 0) && (to_column == 0)) |
||
295 | new_move.sides[COLOR_WHITE].longcastle_allowed = false; // our opponent can no longer castle queenside |
||
296 | else if ((new_move.slots[to_line][to_column].color == COLOR_WHITE) && (to_line == 0) && (to_column == 7)) |
||
297 | new_move.sides[COLOR_WHITE].shortcastle_allowed = false; // our opponent can no longer castle bishopside |
||
298 | else if ((new_move.slots[to_line][to_column].color == COLOR_BLACK) && (to_line == 7) && (to_column == 0)) |
||
299 | new_move.sides[COLOR_BLACK].longcastle_allowed = false; // our opponent can no longer castle queenside |
||
300 | else if ((new_move.slots[to_line][to_column].color == COLOR_BLACK) && (to_line == 7) && (to_column == 7)) |
||
301 | new_move.sides[COLOR_BLACK].shortcastle_allowed = false; // our opponent can no longer castle bishopside |
||
302 | } |
||
303 | |||
304 | // resize this players' taken parts array and move the taken piece in it |
||
305 | playing_side->takenparts = (unsigned char *) SAFE_realloc (playing_side->takenparts, playing_side->takenpart_count, playing_side->takenpart_count + 1, sizeof (unsigned char), true); |
||
306 | playing_side->takenparts[playing_side->takenpart_count] = new_move.slots[to_line][to_column].part; |
||
307 | playing_side->takenpart_count++; // player has now taken one piece more |
||
308 | new_move.has_captured = true; // remember a part has just been captured |
||
309 | } |
||
310 | |||
311 | // else is it an "en passant" coup ? |
||
312 | else if ((new_move.slots[from_line][from_column].part == PART_PAWN) // we're a pawn |
||
313 | && (abs (to_column - from_column) == 1) // we moved diagonally |
||
314 | && (new_move.slots[to_line][to_column].part == PART_NONE) // no part on destination |
||
315 | && (new_move.slots[from_line][to_column].part == PART_PAWN)) // but a pawn next to us |
||
316 | { |
||
317 | // resize this players' taken parts array and move the taken piece in it |
||
318 | playing_side->takenparts = (unsigned char *) SAFE_realloc (playing_side->takenparts, playing_side->takenpart_count, playing_side->takenpart_count + 1, sizeof (unsigned char), true); |
||
319 | playing_side->takenparts[playing_side->takenpart_count] = new_move.slots[from_line][to_column].part; |
||
320 | memset (&new_move.slots[from_line][to_column], 0, sizeof (boardslot_t)); // "en passant" coup |
||
321 | playing_side->takenpart_count++; // player has now taken one piece more |
||
322 | new_move.has_captured = true; // remember a part has just been captured |
||
323 | new_move.is_enpassant = true; // and that it's an en passant coup |
||
324 | } |
||
325 | |||
326 | // has a promotion been specified ? |
||
327 | if (promotion_type != PART_NONE) |
||
328 | Move_SetSlot (&new_move, from_line, from_column, new_move.color, promotion_type); // promote the pawn to the desired type |
||
329 | |||
330 | // was the moved part the king ? if so, player can't castle anymore |
||
331 | if (new_move.slots[from_line][from_column].part == PART_KING) |
||
332 | { |
||
333 | // did the king castle queenside ? or did it castle bishopside ? |
||
334 | if ((from_column == 4) && (to_column == 2)) |
||
335 | { |
||
336 | // move the rook from A to D (0 -> 3) and wipe its previous location |
||
337 | memcpy (&new_move.slots[to_line][3], &new_move.slots[from_line][0], sizeof (boardslot_t)); |
||
338 | memset (&new_move.slots[from_line][0], 0, sizeof (boardslot_t)); |
||
339 | } |
||
340 | else if ((from_column == 4) && (to_column == 6)) |
||
341 | { |
||
342 | // move the rook from H to F (7 -> 5) and wipe its previous location |
||
343 | memcpy (&new_move.slots[to_line][5], &new_move.slots[from_line][7], sizeof (boardslot_t)); |
||
344 | memset (&new_move.slots[from_line][7], 0, sizeof (boardslot_t)); |
||
345 | } |
||
346 | |||
347 | // no matter whether it castled or not, remember this king cannot castle anymore |
||
348 | playing_side->shortcastle_allowed = false; |
||
349 | playing_side->longcastle_allowed = false; |
||
350 | } |
||
351 | |||
352 | // else was it the rook ? |
||
353 | else if (new_move.slots[from_line][from_column].part == PART_ROOK) |
||
354 | { |
||
355 | // was the tower in A ? or was it in H ? |
||
356 | if (from_column == 0) |
||
357 | playing_side->longcastle_allowed = false; // player can no longer castle queenside |
||
358 | else if (from_column == 7) |
||
359 | playing_side->shortcastle_allowed = false; // player can no longer castle bishopside |
||
360 | } |
||
361 | |||
362 | // move the part and erase the starting slot |
||
363 | memcpy (&new_move.slots[to_line][to_column], &new_move.slots[from_line][from_column], sizeof (boardslot_t)); |
||
364 | memset (&new_move.slots[from_line][from_column], 0, sizeof (boardslot_t)); |
||
365 | |||
366 | // does the new move have a comment ? if so, save it |
||
367 | if ((comment != NULL) && (comment[0] != 0)) |
||
368 | { |
||
369 | new_move.comment_size = wcslen (comment) + 1; // save this move's comment and the size of the mallocated space |
||
370 | new_move.comment = (wchar_t *) SAFE_malloc (new_move.comment_size, sizeof (wchar_t), false); |
||
371 | wcscpy_s (new_move.comment, new_move.comment_size, comment); // copy comment |
||
372 | } |
||
373 | |||
374 | // evaluate check and stalemate status |
||
375 | new_move.is_check = Move_IsCheck (&new_move, 1 - new_move.color); // save whether opponent is in check or not |
||
376 | new_move.is_stalemate = Move_IsStaleMate (&new_move, 1 - new_move.color); // save whether opponent is stalemate |
||
377 | |||
378 | // describe our move in Standard Abbreviated Notation and describe the resulting table in Forsyth-Edwards Notation |
||
379 | Move_DescribeInSAN (&new_move); |
||
380 | Move_DescribeInFEN (&new_move); |
||
381 | |||
382 | // resize the previous moves array and insert our new move at the end of it |
||
383 | board->moves = (boardmove_t *) SAFE_realloc (board->moves, board->move_count, board->move_count + 1, sizeof (boardmove_t), false); |
||
384 | memcpy (&board->moves[board->move_count], &new_move, sizeof (boardmove_t)); |
||
385 | board->move_count++; // we know now one previous move more |
||
386 | |||
387 | board->viewed_move = board->move_count - 1; // set the move to be viewed to be the last move |
||
388 | |||
389 | // if the game hadn't started yet, remember it has now |
||
390 | if (board->game_state == STATE_UNKNOWN) |
||
391 | board->game_state = STATE_PLAYING; |
||
392 | |||
393 | // remember the last move time |
||
394 | board->lastmove_time = current_time; |
||
395 | |||
75 | pmbaty | 396 | // reset stoppage time |
397 | stoppage_time = 0; |
||
398 | |||
1 | pmbaty | 399 | // remember the game state should be reevaluated |
400 | board->reevaluate = true; |
||
401 | |||
402 | return; // finished |
||
403 | } |