Rev 7 | Rev 32 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 7 | Rev 12 | ||
|---|---|---|---|
| Line 15... | Line 15... | ||
| 15 | static int current_obstinacy; |
15 | static int current_obstinacy; |
| 16 | 16 | ||
| 17 | // prototypes of local functions |
17 | // prototypes of local functions |
| 18 | static void PlayerEngine_Recv (player_t *player); |
18 | static void PlayerEngine_Recv (player_t *player); |
| 19 | static void PlayerEngine_Send (player_t *player); |
19 | static void PlayerEngine_Send (player_t *player); |
| 20 | static |
20 | static wchar_t *Move_BuildString (boardmove_t *move); |
| 21 | 21 | ||
| 22 | 22 | ||
| 23 | bool PlayerEngine_Init (player_t *player) |
23 | bool PlayerEngine_Init (player_t *player) |
| 24 | { |
24 | { |
| 25 | // this function starts a chess engine as a child process. This process's stdin and |
25 | // this function starts a chess engine as a child process. This process's stdin and |
| Line 329... | Line 329... | ||
| 329 | 329 | ||
| 330 | // is it NOT a hint, are blunders allowed, should we do one now AND can we do one now ? |
330 | // is it NOT a hint, are blunders allowed, should we do one now AND can we do one now ? |
| 331 | if (!is_hint && (options.engine.blunder_chances > 0) && (rand () % 100 < options.engine.blunder_chances) |
331 | if (!is_hint && (options.engine.blunder_chances > 0) && (rand () % 100 < options.engine.blunder_chances) |
| 332 | && Move_FindRandomMove (&the_board.moves[the_board.move_count - 1], player->color, &move)) |
332 | && Move_FindRandomMove (&the_board.moves[the_board.move_count - 1], player->color, &move)) |
| 333 | { |
333 | { |
| 334 | Move_DescribeInSAN (&move); // build the forced move string |
- | |
| 335 | Player_SendBuffer_Add (player, 1000, L"%s %s\n", options.engine.command_force, move |
334 | Player_SendBuffer_Add (player, 1000, L"%s %s\n", options.engine.command_force, Move_BuildString (&move)); // send the blunderous move to the engine |
| 336 | Player_SendBuffer_Add (player, 1000, L"disp\n"); |
335 | Player_SendBuffer_Add (player, 1000, L"disp\n"); |
| 337 | Debug_Log (L"===Discarding engine move, forcing a blunderous move (%s) instead===\n", move |
336 | Debug_Log (L"===Discarding engine move, forcing a blunderous move (%s) instead===\n", Move_BuildString (&move)); // blunder |
| 338 | } |
337 | } |
| 339 | 338 | ||
| 340 | // mark the engine's selected and hovered squares |
339 | // mark the engine's selected and hovered squares |
| 341 | Board_SetSelectedAndHovered (&the_board, move.source[0], move.source[1], move.target[0], move.target[1]); |
340 | Board_SetSelectedAndHovered (&the_board, move.source[0], move.source[1], move.target[0], move.target[1]); |
| 342 | 341 | ||
| Line 422... | Line 421... | ||
| 422 | Player_SendBuffer_Add (player, 1000, L"disp\n"); |
421 | Player_SendBuffer_Add (player, 1000, L"disp\n"); |
| 423 | 422 | ||
| 424 | // instruct it about its allowed search depth BEFORE each move (this ensures engine will be "ready" to handle the command) |
423 | // instruct it about its allowed search depth BEFORE each move (this ensures engine will be "ready" to handle the command) |
| 425 | // then build the move string, and send the move string to the engine |
424 | // then build the move string, and send the move string to the engine |
| 426 | Player_SendBuffer_Add (player, 1000, L"%s %d\n", options.engine.command_sd, options.engine.depth); |
425 | Player_SendBuffer_Add (player, 1000, L"%s %d\n", options.engine.command_sd, options.engine.depth); |
| 427 | Move_BuildString (&the_board.moves[the_board.move_count - 1], line_buffer, WCHAR_SIZEOF (line_buffer)); |
- | |
| 428 | Player_SendBuffer_Add (player, 1000, L"%s\n", |
426 | Player_SendBuffer_Add (player, 1000, L"%s\n", Move_BuildString (&the_board.moves[the_board.move_count - 1])); |
| 429 | } |
427 | } |
| 430 | 428 | ||
| 431 | // else game has not started yet, but it's our turn |
429 | // else game has not started yet, but it's our turn |
| 432 | else |
430 | else |
| 433 | Player_SendBuffer_Add (player, 1000, L"%s\n", options.engine.command_go); // so let's start the game |
431 | Player_SendBuffer_Add (player, 1000, L"%s\n", options.engine.command_go); // so let's start the game |
| Line 553... | Line 551... | ||
| 553 | 551 | ||
| 554 | return; // finished |
552 | return; // finished |
| 555 | } |
553 | } |
| 556 | 554 | ||
| 557 | 555 | ||
| 558 | static |
556 | static wchar_t *Move_BuildString (boardmove_t *move) |
| 559 | { |
557 | { |
| 560 | // helper function to build a move string to send to the engine from a particular board move |
558 | // helper function to build a move string to send to the engine from a particular board move. NOT THREAD SAFE. |
| - | 559 | ||
| - | 560 | static wchar_t output_string[8]; |
|
| 561 | 561 | ||
| 562 | // construct the four first characters |
562 | // construct the four first characters |
| 563 | swprintf_s (output_string, |
563 | swprintf_s (output_string, WCHAR_SIZEOF (output_string), L"%c%c%c%c", |
| 564 | L'a' + move->source[1], L'1' + move->source[0], |
564 | L'a' + move->source[1], L'1' + move->source[0], |
| 565 | L'a' + move->target[1], L'1' + move->target[0]); |
565 | L'a' + move->target[1], L'1' + move->target[0]); |
| 566 | 566 | ||
| 567 | // append any eventual promotion |
567 | // append any eventual promotion |
| 568 | if (move->promotion_type == PART_ROOK) |
568 | if (move->promotion_type == PART_ROOK) |
| 569 | wcscat_s (output_string, |
569 | wcscat_s (output_string, WCHAR_SIZEOF (output_string), L"r"); |
| 570 | else if (move->promotion_type == PART_KNIGHT) |
570 | else if (move->promotion_type == PART_KNIGHT) |
| 571 | wcscat_s (output_string, |
571 | wcscat_s (output_string, WCHAR_SIZEOF (output_string), L"n"); |
| 572 | else if (move->promotion_type == PART_BISHOP) |
572 | else if (move->promotion_type == PART_BISHOP) |
| 573 | wcscat_s (output_string, |
573 | wcscat_s (output_string, WCHAR_SIZEOF (output_string), L"b"); |
| 574 | else if (move->promotion_type == PART_QUEEN) |
574 | else if (move->promotion_type == PART_QUEEN) |
| 575 | wcscat_s (output_string, |
575 | wcscat_s (output_string, WCHAR_SIZEOF (output_string), L"q"); |
| 576 | 576 | ||
| 577 | return; // |
577 | return (output_string); // finished, return the move string |
| 578 | } |
578 | } |