55 lines
2.1 KiB
C
55 lines
2.1 KiB
C
#include "king.h"
|
|
#include "game_state.h"
|
|
#include "piece_logic.h"
|
|
|
|
static uint8_t King_Locations[2u][2u] = {{7,4}, {0,4}};
|
|
|
|
bool Check_Current_Players_Check_Status(Game_State_t * game_state)
|
|
{
|
|
bool ret_val;
|
|
uint8_t white_black_idx = game_state->player_turn ? 0u : 1u;
|
|
ret_val = !square_is_safe(King_Locations[white_black_idx][0u], King_Locations[white_black_idx][1u], game_state);
|
|
return ret_val;
|
|
}
|
|
|
|
bool Check_If_Move_Caused_Check(uint8_t piece, uint8_t row, uint8_t column, Game_State_t * game_state)
|
|
{
|
|
bool ret_val;
|
|
uint8_t store_current_piece = game_state->board_pieces[row*8+column];
|
|
game_state->board_pieces[row*8+column] = piece;
|
|
//If its the white's turn we want to see if the white king is still safe.
|
|
ret_val = Check_Current_Players_Check_Status(game_state);
|
|
game_state->board_pieces[row*8+column] = store_current_piece;
|
|
return ret_val;
|
|
}
|
|
|
|
/**
|
|
* @brief Function for checking the selected piece to see if we are moving the king.
|
|
* If we are then we also want to update the new location of the corresponding king.
|
|
* @param row: Current row location of the piece.
|
|
* @param column: Current column location of the piece.
|
|
* @retval None
|
|
*/
|
|
void Check_If_Moving_King(uint8_t row, uint8_t column, Game_State_t * game_state)
|
|
{
|
|
uint8_t white_black_idx = game_state->player_turn ? 0u : 1u;
|
|
uint8_t inverse_idx = game_state->player_turn ? 1u : 0u;
|
|
if((game_state->selected_peice == KING_WHITE) || (game_state->selected_peice == KING_BLACK))
|
|
{
|
|
King_Locations[white_black_idx][0u] = row;
|
|
King_Locations[white_black_idx][1u] = column;
|
|
game_state->castling_allowed[white_black_idx][0u] = false;
|
|
game_state->castling_allowed[white_black_idx][1u] = false;
|
|
}
|
|
// Disable the castling of the corresponding side if the rook is being moved.
|
|
if (game_state->board_pieces[inverse_idx*7*8+7] == SQUARE_EMPTY)
|
|
{
|
|
game_state->castling_allowed[white_black_idx][0u] = false;
|
|
}
|
|
if (game_state->board_pieces[inverse_idx*8*7+0] == SQUARE_EMPTY)
|
|
{
|
|
game_state->castling_allowed[white_black_idx][1u] = false;
|
|
}
|
|
}
|
|
|