Subversion Repositories Games.Chess Giants

Rev

Rev 75 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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