Subversion Repositories Games.Chess Giants

Rev

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