Subversion Repositories Games.Chess Giants

Rev

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