diff --git a/src/chess_board.cpp b/src/chess_board.cpp index 53ec901..8cdf5b0 100644 --- a/src/chess_board.cpp +++ b/src/chess_board.cpp @@ -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 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. * @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. 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]); - if(High_Alert) - { - SDL_Log("High ALERT ENABLED!\n"); - } - else - { - SDL_Log("High ALERT DISABLED!\n"); - } - 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. 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]); + //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. * @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) { + /* Something unexpected happened, game cannot progress until the board is returned to the known state */ case GAME_STATE_ERROR_DETECTED: { 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--; Board_Lights[j][i] = LIGHT_OFF; + /* All Errors have been rectified so we can go back to where we were.*/ if(Error_Count == 0u) { Game_State = Last_Game_State; @@ -756,17 +810,17 @@ static void Board_Square_Was_Toggled(uint8_t j, uint8_t i) { 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_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((Board_State[j][i] != KING_BLACK) && (Board_State[j][i] != KING_WHITE)) { Check_If_Could_Cause_Check(j, i); } - Selected_Piece = Board_State[j][i]; + Selected_Piece = Board_State[j][i]; Board_State[j][i] = SQUARE_EMPTY; Mark_Potential_Moves(Selected_Piece, i, j); Board_Lights[j][i] = PIECE_ORIGIN; @@ -774,21 +828,29 @@ static void Board_Square_Was_Toggled(uint8_t j, uint8_t i) } else { - Last_Game_State = Game_State; - Game_State = GAME_STATE_ERROR_DETECTED; - Board_Lights[j][i] = ERROR_MOVE; - Error_Count++; + if(Converting_Pawn_If_Applicable(j, i)) + { + + } + else + { + Last_Game_State = Game_State; + Game_State = GAME_STATE_ERROR_DETECTED; + Board_Lights[j][i] = ERROR_MOVE; + Error_Count++; + } } 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_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) { Check_If_Moving_King(j, i); + Check_If_Converting_Pawn(j, i); Board_State[j][i] = Selected_Piece; Selected_Piece = SQUARE_EMPTY; clear_lights(); @@ -848,6 +910,7 @@ static void Board_Square_Was_Toggled(uint8_t j, uint8_t i) } 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_P1_TURN_TAKING: { @@ -855,6 +918,7 @@ static void Board_Square_Was_Toggled(uint8_t j, uint8_t i) { if(j < 8u) { + Check_If_Converting_Pawn(j, i); Board_State[j][i] = Selected_Piece; Selected_Piece = SQUARE_EMPTY; Board_Lights[j][i] = LIGHT_OFF; @@ -870,10 +934,17 @@ static void Board_Square_Was_Toggled(uint8_t j, uint8_t i) } else { - Last_Game_State = Game_State; - Game_State = GAME_STATE_ERROR_DETECTED; - Board_Lights[j][i] = ERROR_MOVE; - Error_Count++; + if(Converting_Pawn_If_Applicable(j, i)) + { + + } + else + { + Last_Game_State = Game_State; + Game_State = GAME_STATE_ERROR_DETECTED; + Board_Lights[j][i] = ERROR_MOVE; + Error_Count++; + } } if ((Selected_Piece == SQUARE_EMPTY) && (Taken_Piece == SQUARE_EMPTY)) diff --git a/src/chess_board.h b/src/chess_board.h index 59eb56b..e6f0650 100644 --- a/src/chess_board.h +++ b/src/chess_board.h @@ -1,5 +1,4 @@ -#include -#include +#include #define LIGHT_OFF 0u #define POTENTIAL_MOVE 1u @@ -10,6 +9,7 @@ #define PIECE_NEEDS_TO_BE_HERE 6u #define POTENTIAL_CASTLE 7u #define PIECE_NEEDS_TO_BE_REMOVED 8u +#define CONVERTING_PAWN 9u #define PAWN_WHITE 0u #define PAWN_BLACK 1u diff --git a/src/user_interface_abstraction.cpp b/src/user_interface_abstraction.cpp index 763ded5..6a72194 100644 --- a/src/user_interface_abstraction.cpp +++ b/src/user_interface_abstraction.cpp @@ -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_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) { SDL_RenderFillRect(p_renderer, &Rectangle);