adding queen swapping

This commit is contained in:
Daniel Weber 2023-01-08 21:04:28 -05:00
parent c3c14e7882
commit 28f516018e
3 changed files with 99 additions and 22 deletions

View File

@ -20,6 +20,12 @@ static uint8_t King_Locations[2u][2u] = {{7,4}, {0,4}};
static bool Castling_Allowed[2u][2u] = {{true, true}, {true, true}}; static bool Castling_Allowed[2u][2u] = {{true, true}, {true, true}};
static bool High_Alert = false; static bool High_Alert = false;
static bool Converting_Pawn = false;
static uint8_t Converting_Pawn_Row_Col[2];
static uint8_t Pawn_Converted_To = QUEEN_WHITE;
/** /**
* @brief Function for clearing all of the lights on the board. Except for error moves. * @brief Function for clearing all of the lights on the board. Except for error moves.
* @retval None * @retval None
@ -273,15 +279,6 @@ void Check_If_Could_Cause_Check(uint8_t row, uint8_t column)
//If its the white's turn we want to see if the white king is still safe. //If its the white's turn we want to see if the white king is still safe.
uint8_t white_black_idx = White_Turn ? 0u : 1u; uint8_t white_black_idx = White_Turn ? 0u : 1u;
High_Alert = !square_is_safe(King_Locations[white_black_idx][0u], King_Locations[white_black_idx][1u]); High_Alert = !square_is_safe(King_Locations[white_black_idx][0u], King_Locations[white_black_idx][1u]);
if(High_Alert)
{
SDL_Log("High ALERT ENABLED!\n");
}
else
{
SDL_Log("High ALERT DISABLED!\n");
}
Board_State[row][column] = temp_storage; Board_State[row][column] = temp_storage;
} }
@ -654,6 +651,8 @@ static void Switch_Turns(void)
// Check if the current kings locations is safe. If it is safe then check is false, if it isnt safe then check is true. // Check if the current kings locations is safe. If it is safe then check is false, if it isnt safe then check is true.
uint8_t white_black_idx = White_Turn ? 0u : 1u; uint8_t white_black_idx = White_Turn ? 0u : 1u;
Check[white_black_idx] = !square_is_safe(King_Locations[white_black_idx][0u], King_Locations[white_black_idx][1u]); Check[white_black_idx] = !square_is_safe(King_Locations[white_black_idx][0u], King_Locations[white_black_idx][1u]);
//Last thing we need to check before sitching turns is to check if the game is over.
} }
/** /**
@ -688,6 +687,59 @@ static void Check_If_Moving_King(uint8_t row, uint8_t column)
} }
} }
static void Check_If_Converting_Pawn(uint8_t row, uint8_t column)
{
uint8_t white_black_idx = White_Turn ? 0u : 1u;
Converting_Pawn = false;
if((Selected_Piece == PAWN_WHITE) || (Selected_Piece == PAWN_BLACK))
{
if((row == 0u) || (row == 7u))
{
Selected_Piece = White_Turn ? QUEEN_WHITE : QUEEN_BLACK;
Pawn_Converted_To = Selected_Piece;
Converting_Pawn = true;
Converting_Pawn_Row_Col[0] = row;
Converting_Pawn_Row_Col[1] = column;
}
}
}
static bool Converting_Pawn_If_Applicable(uint8_t row, uint8_t column)
{
bool ret_val = false;
if(Converting_Pawn)
{
if((row == Converting_Pawn_Row_Col[0]) &&
(Converting_Pawn_Row_Col[1] == column))
{
//Putting the peice down on the board
if(Board_State[row][column] == SQUARE_EMPTY)
{
Board_State[row][column] = Pawn_Converted_To;
Board_Lights[row][column] = LIGHT_OFF;
}
//Picking the peice back up to toggle through the options
else
{
Board_State[row][column] = SQUARE_EMPTY;
Board_Lights[row][column] = CONVERTING_PAWN;
Pawn_Converted_To = Pawn_Converted_To - 2;
if (Pawn_Converted_To < ROOK_WHITE)
{
Pawn_Converted_To += 8u;
}
}
ret_val = true;
}
}
return ret_val;
}
/** /**
* @brief Function for toggeling a square's state. * @brief Function for toggeling a square's state.
* @param j: row location that was toggled. * @param j: row location that was toggled.
@ -698,6 +750,7 @@ static void Board_Square_Was_Toggled(uint8_t j, uint8_t i)
{ {
switch (Game_State) switch (Game_State)
{ {
/* Something unexpected happened, game cannot progress until the board is returned to the known state */
case GAME_STATE_ERROR_DETECTED: case GAME_STATE_ERROR_DETECTED:
{ {
if (Board_Lights[j][i] == PIECE_ORIGIN) if (Board_Lights[j][i] == PIECE_ORIGIN)
@ -740,6 +793,7 @@ static void Board_Square_Was_Toggled(uint8_t j, uint8_t i)
{ {
Error_Count--; Error_Count--;
Board_Lights[j][i] = LIGHT_OFF; Board_Lights[j][i] = LIGHT_OFF;
/* All Errors have been rectified so we can go back to where we were.*/
if(Error_Count == 0u) if(Error_Count == 0u)
{ {
Game_State = Last_Game_State; Game_State = Last_Game_State;
@ -756,10 +810,10 @@ static void Board_Square_Was_Toggled(uint8_t j, uint8_t i)
{ {
break; break;
} }
/* We are waiting till the player who's turn it is picks up a piece that is on their team */
case GAME_STATE_P2_TURN_BEGINING: case GAME_STATE_P2_TURN_BEGINING:
case GAME_STATE_P1_TURN_BEGINING: case GAME_STATE_P1_TURN_BEGINING:
{ {
/* We are waiting till the player who's turn it is picks up a piece that is on their team */
if ((j < 8u) && (Board_State[j][i] != SQUARE_EMPTY) && (white_team(Board_State[j][i]) == White_Turn)) if ((j < 8u) && (Board_State[j][i] != SQUARE_EMPTY) && (white_team(Board_State[j][i]) == White_Turn))
{ {
if((Board_State[j][i] != KING_BLACK) && (Board_State[j][i] != KING_WHITE)) if((Board_State[j][i] != KING_BLACK) && (Board_State[j][i] != KING_WHITE))
@ -771,6 +825,12 @@ static void Board_Square_Was_Toggled(uint8_t j, uint8_t i)
Mark_Potential_Moves(Selected_Piece, i, j); Mark_Potential_Moves(Selected_Piece, i, j);
Board_Lights[j][i] = PIECE_ORIGIN; Board_Lights[j][i] = PIECE_ORIGIN;
Game_State++; Game_State++;
}
else
{
if(Converting_Pawn_If_Applicable(j, i))
{
} }
else else
{ {
@ -779,16 +839,18 @@ static void Board_Square_Was_Toggled(uint8_t j, uint8_t i)
Board_Lights[j][i] = ERROR_MOVE; Board_Lights[j][i] = ERROR_MOVE;
Error_Count++; Error_Count++;
} }
}
break; break;
} }
/* Person is in the middle of taking a turn for example they might already have a peice in the hand*/
case GAME_STATE_P2_TURN_IN_PROGRESS: case GAME_STATE_P2_TURN_IN_PROGRESS:
case GAME_STATE_P1_TURN_IN_PROGRESS: case GAME_STATE_P1_TURN_IN_PROGRESS:
{ {
/* We are waiting till the player who's turn it is picks up a piece that is on their team */
if (Board_Lights[j][i] == POTENTIAL_MOVE) if (Board_Lights[j][i] == POTENTIAL_MOVE)
{ {
Check_If_Moving_King(j, i); Check_If_Moving_King(j, i);
Check_If_Converting_Pawn(j, i);
Board_State[j][i] = Selected_Piece; Board_State[j][i] = Selected_Piece;
Selected_Piece = SQUARE_EMPTY; Selected_Piece = SQUARE_EMPTY;
clear_lights(); clear_lights();
@ -848,6 +910,7 @@ static void Board_Square_Was_Toggled(uint8_t j, uint8_t i)
} }
break; break;
} }
/* Player still needs to do something to complete their turn, like complete castle, en pessant, or converting a pawn*/
case GAME_STATE_P2_TURN_TAKING: case GAME_STATE_P2_TURN_TAKING:
case GAME_STATE_P1_TURN_TAKING: case GAME_STATE_P1_TURN_TAKING:
{ {
@ -855,6 +918,7 @@ static void Board_Square_Was_Toggled(uint8_t j, uint8_t i)
{ {
if(j < 8u) if(j < 8u)
{ {
Check_If_Converting_Pawn(j, i);
Board_State[j][i] = Selected_Piece; Board_State[j][i] = Selected_Piece;
Selected_Piece = SQUARE_EMPTY; Selected_Piece = SQUARE_EMPTY;
Board_Lights[j][i] = LIGHT_OFF; Board_Lights[j][i] = LIGHT_OFF;
@ -867,6 +931,12 @@ static void Board_Square_Was_Toggled(uint8_t j, uint8_t i)
Taken_Piece = SQUARE_EMPTY; Taken_Piece = SQUARE_EMPTY;
} }
}
else
{
if(Converting_Pawn_If_Applicable(j, i))
{
} }
else else
{ {
@ -875,6 +945,7 @@ static void Board_Square_Was_Toggled(uint8_t j, uint8_t i)
Board_Lights[j][i] = ERROR_MOVE; Board_Lights[j][i] = ERROR_MOVE;
Error_Count++; Error_Count++;
} }
}
if ((Selected_Piece == SQUARE_EMPTY) && (Taken_Piece == SQUARE_EMPTY)) if ((Selected_Piece == SQUARE_EMPTY) && (Taken_Piece == SQUARE_EMPTY))
{ {

View File

@ -1,5 +1,4 @@
#include <SDL2/SDL.h> #include <cstdint>
#include <SDL2/SDL_video.h>
#define LIGHT_OFF 0u #define LIGHT_OFF 0u
#define POTENTIAL_MOVE 1u #define POTENTIAL_MOVE 1u
@ -10,6 +9,7 @@
#define PIECE_NEEDS_TO_BE_HERE 6u #define PIECE_NEEDS_TO_BE_HERE 6u
#define POTENTIAL_CASTLE 7u #define POTENTIAL_CASTLE 7u
#define PIECE_NEEDS_TO_BE_REMOVED 8u #define PIECE_NEEDS_TO_BE_REMOVED 8u
#define CONVERTING_PAWN 9u
#define PAWN_WHITE 0u #define PAWN_WHITE 0u
#define PAWN_BLACK 1u #define PAWN_BLACK 1u

View File

@ -75,6 +75,12 @@ static void ui_draw_board(SDL_Renderer *p_renderer, uint8_t board_lights[12][8],
SDL_RenderFillRect(p_renderer, &Rectangle); SDL_RenderFillRect(p_renderer, &Rectangle);
SDL_SetRenderDrawColor(p_renderer, 0x85, 0x5E, 0x42, 0x00); SDL_SetRenderDrawColor(p_renderer, 0x85, 0x5E, 0x42, 0x00);
} }
else if (board_lights[j][i] == CONVERTING_PAWN)
{
SDL_SetRenderDrawColor(p_renderer, 0xFF, 0x3B, 0x7A, 0x57);
SDL_RenderFillRect(p_renderer, &Rectangle);
SDL_SetRenderDrawColor(p_renderer, 0x85, 0x5E, 0x42, 0x00);
}
else if (((i % 2) + (j % 2)) == 1) else if (((i % 2) + (j % 2)) == 1)
{ {
SDL_RenderFillRect(p_renderer, &Rectangle); SDL_RenderFillRect(p_renderer, &Rectangle);