Win game logic

This commit is contained in:
Daniel Weber 2023-01-10 22:20:55 -05:00
parent a488636424
commit 63fa9d8eab

View File

@ -3,6 +3,7 @@
#include <stack> #include <stack>
#include <string.h> #include <string.h>
#include "pieces.hpp" #include "pieces.hpp"
#include <SDL2/SDL.h>
static uint8_t Board_State[12][8] = {{0}}; static uint8_t Board_State[12][8] = {{0}};
@ -259,11 +260,11 @@ bool square_is_safe(uint8_t row, uint8_t column)
return true; return true;
} }
bool Check_If_Move_Caused_Check(uint8_t row, uint8_t column) bool Check_If_Move_Caused_Check(uint8_t piece, uint8_t row, uint8_t column)
{ {
bool ret_val; bool ret_val;
uint8_t store_current_piece = Board_State[row][column]; uint8_t store_current_piece = Board_State[row][column];
Board_State[row][column] = Selected_Piece; Board_State[row][column] = piece;
//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;
ret_val = !square_is_safe(King_Locations[white_black_idx][0u], King_Locations[white_black_idx][1u]); ret_val = !square_is_safe(King_Locations[white_black_idx][0u], King_Locations[white_black_idx][1u]);
@ -281,12 +282,15 @@ void Check_If_Could_Cause_Check(uint8_t row, uint8_t column)
Board_State[row][column] = temp_storage; Board_State[row][column] = temp_storage;
} }
static void Set_Light(uint8_t row, uint8_t column, uint8_t state) static bool Set_Light(uint8_t piece, uint8_t row, uint8_t column, uint8_t state)
{ {
if ((!High_Alert) || (!Check_If_Move_Caused_Check(row, column))) bool ret_val = false;
if (!Check_If_Move_Caused_Check(piece, row, column))
{ {
Board_Lights[row][column] = state; Board_Lights[row][column] = state;
ret_val = true;
} }
return ret_val;
} }
/** /**
@ -295,12 +299,14 @@ static void Set_Light(uint8_t row, uint8_t column, uint8_t state)
* @param column: column to move to * @param column: column to move to
* @retval None * @retval None
*/ */
static void pawn_move(uint8_t row, uint8_t column) static bool pawn_move(uint8_t piece, uint8_t row, uint8_t column)
{ {
bool ret_val = false;
if (Board_State[row][column] == SQUARE_EMPTY) if (Board_State[row][column] == SQUARE_EMPTY)
{ {
Set_Light(row, column, POTENTIAL_MOVE); ret_val = Set_Light(piece, row, column, POTENTIAL_MOVE);
} }
return ret_val;
} }
/** /**
@ -310,11 +316,12 @@ static void pawn_move(uint8_t row, uint8_t column)
* @param direction_c: Column direction * @param direction_c: Column direction
* @param row: current row location * @param row: current row location
* @param column: current column location * @param column: current column location
* @param piece_one: the piece that is casting the ray. * @param piece: the piece that is casting the ray.
* @retval None * @retval None
*/ */
static void cast_a_ray(int8_t direction_r, int8_t direction_c, uint8_t column, uint8_t row, uint8_t piece_one) static bool cast_a_ray(uint8_t piece, int8_t direction_r, int8_t direction_c, uint8_t column, uint8_t row)
{ {
bool ret_val = false;
bool loop = true; bool loop = true;
int8_t x = row; int8_t x = row;
int8_t y = column; int8_t y = column;
@ -328,11 +335,11 @@ static void cast_a_ray(int8_t direction_r, int8_t direction_c, uint8_t column, u
} }
else if (Board_State[x][y] == SQUARE_EMPTY) else if (Board_State[x][y] == SQUARE_EMPTY)
{ {
Set_Light(x, y, POTENTIAL_MOVE); ret_val = Set_Light(piece, x, y, POTENTIAL_MOVE) || ret_val;
} }
else if (opposite_teams(piece_one, Board_State[x][y])) else if (opposite_teams(piece, Board_State[x][y]))
{ {
Set_Light(x, y, POTENTIAL_TAKE); ret_val = Set_Light(piece, x, y, POTENTIAL_TAKE) || ret_val;
/* once we take a piece we can no longer take anymore */ /* once we take a piece we can no longer take anymore */
loop = false; loop = false;
} }
@ -341,6 +348,7 @@ static void cast_a_ray(int8_t direction_r, int8_t direction_c, uint8_t column, u
loop = false; loop = false;
} }
} }
return ret_val;
} }
/** /**
@ -349,12 +357,14 @@ static void cast_a_ray(int8_t direction_r, int8_t direction_c, uint8_t column, u
* @param column: Column obviously * @param column: Column obviously
* @retval None * @retval None
*/ */
static void pawn_take(uint8_t row, uint8_t column, uint8_t piece) static bool pawn_take(uint8_t piece, uint8_t row, uint8_t column)
{ {
bool ret_val = false;
if ((Board_State[row][column] < SQUARE_EMPTY) && opposite_teams(piece, Board_State[row][column])) if ((Board_State[row][column] < SQUARE_EMPTY) && opposite_teams(piece, Board_State[row][column]))
{ {
Set_Light(row, column, POTENTIAL_TAKE); ret_val = Set_Light(piece, row, column, POTENTIAL_TAKE);
} }
return ret_val;
} }
/** /**
@ -364,31 +374,32 @@ static void pawn_take(uint8_t row, uint8_t column, uint8_t piece)
* @param column: Current column location of the piece. * @param column: Current column location of the piece.
* @retval None * @retval None
*/ */
static void Mark_Potential_Moves(uint8_t piece, uint8_t column, uint8_t row) static bool Mark_Potential_Moves(uint8_t piece, uint8_t column, uint8_t row)
{ {
bool ret_val = false;
switch (piece) switch (piece)
{ {
case PAWN_WHITE : case PAWN_WHITE :
case PAWN_BLACK: case PAWN_BLACK:
{ {
int8_t direction = White_Turn ? -1 : 1; int8_t direction = white_team(piece) ? -1 : 1;
uint8_t temp_row = row + direction; uint8_t temp_row = row + direction;
if (row == (White_Turn ? 6u : 1u)) if (row == (white_team(piece) ? 6u : 1u))
{ {
if(Board_State[temp_row][column] == SQUARE_EMPTY) if(Board_State[temp_row][column] == SQUARE_EMPTY)
{ {
pawn_move(row + (direction * 2u), column); ret_val = pawn_move(piece, row + (direction * 2u), column) || ret_val;
} }
} }
pawn_move(temp_row, column); ret_val = pawn_move(piece, temp_row, column) || ret_val;
if(column != 0) if(column != 0)
{ {
pawn_take(temp_row, column - 1u, piece); ret_val = pawn_take(piece, temp_row, column - 1u) || ret_val;
} }
if(column != 7u) if(column != 7u)
{ {
pawn_take(temp_row, column + 1u, piece); ret_val = pawn_take(piece, temp_row, column + 1u) || ret_val;
} }
break; break;
@ -420,7 +431,7 @@ static void Mark_Potential_Moves(uint8_t piece, uint8_t column, uint8_t row)
up_down_step = 0; up_down_step = 0;
left_right_step = -1; left_right_step = -1;
} }
cast_a_ray(up_down_step, left_right_step, column, row, piece); ret_val = cast_a_ray(piece, up_down_step, left_right_step, column, row) || ret_val;
} }
break; break;
@ -470,11 +481,11 @@ static void Mark_Potential_Moves(uint8_t piece, uint8_t column, uint8_t row)
{ {
if (Board_State[x][y] == SQUARE_EMPTY) if (Board_State[x][y] == SQUARE_EMPTY)
{ {
Set_Light(x, y, POTENTIAL_MOVE); ret_val = Set_Light(piece, x, y, POTENTIAL_MOVE) || ret_val;
} }
else if (opposite_teams(piece, Board_State[x][y])) else if (opposite_teams(piece, Board_State[x][y]))
{ {
Set_Light(x, y, POTENTIAL_TAKE); ret_val = Set_Light(piece, x, y, POTENTIAL_TAKE) || ret_val;
} }
} }
} }
@ -508,7 +519,7 @@ static void Mark_Potential_Moves(uint8_t piece, uint8_t column, uint8_t row)
up_down_step = 1; up_down_step = 1;
left_right_step = -1; left_right_step = -1;
} }
cast_a_ray(up_down_step, left_right_step, column, row, piece); ret_val = cast_a_ray(piece, up_down_step, left_right_step, column, row) || ret_val;
} }
break; break;
} }
@ -516,9 +527,9 @@ static void Mark_Potential_Moves(uint8_t piece, uint8_t column, uint8_t row)
case QUEEN_BLACK: case QUEEN_BLACK:
{ {
//Mark bishop moves //Mark bishop moves
Mark_Potential_Moves((piece - 2u), column, row); ret_val = Mark_Potential_Moves((piece - 2u), column, row) || ret_val;
//Mark rook moves //Mark rook moves
Mark_Potential_Moves((piece - 6u), column, row); ret_val = Mark_Potential_Moves((piece - 6u), column, row) || ret_val;
break; break;
} }
case KING_WHITE: case KING_WHITE:
@ -539,16 +550,18 @@ static void Mark_Potential_Moves(uint8_t piece, uint8_t column, uint8_t row)
if (Board_State[x][y] == SQUARE_EMPTY) if (Board_State[x][y] == SQUARE_EMPTY)
{ {
Board_Lights[x][y] = POTENTIAL_MOVE; Board_Lights[x][y] = POTENTIAL_MOVE;
ret_val = true;
} }
else if (opposite_teams(piece, Board_State[x][y])) else if (opposite_teams(piece, Board_State[x][y]))
{ {
Board_Lights[x][y] = POTENTIAL_TAKE; Board_Lights[x][y] = POTENTIAL_TAKE;
ret_val = true;
} }
} }
} }
} }
uint8_t white_black_idx = White_Turn ? 0u : 1u; uint8_t white_black_idx = white_team(piece) ? 0u : 1u;
uint8_t kings_row = White_Turn ? 7u : 0u; uint8_t kings_row = white_team(piece) ? 7u : 0u;
//Can only castle if not currently in check //Can only castle if not currently in check
if (square_is_safe(row, column)) if (square_is_safe(row, column))
@ -562,6 +575,7 @@ static void Mark_Potential_Moves(uint8_t piece, uint8_t column, uint8_t row)
{ {
// Yay we can castle queen side! // Yay we can castle queen side!
Board_Lights[kings_row][2u] = POTENTIAL_CASTLE; Board_Lights[kings_row][2u] = POTENTIAL_CASTLE;
ret_val = true;
} }
} }
@ -573,6 +587,7 @@ static void Mark_Potential_Moves(uint8_t piece, uint8_t column, uint8_t row)
{ {
// Yay we can castle king side! // Yay we can castle king side!
Board_Lights[kings_row][6u] = POTENTIAL_CASTLE; Board_Lights[kings_row][6u] = POTENTIAL_CASTLE;
ret_val = true;
} }
} }
} }
@ -583,6 +598,7 @@ static void Mark_Potential_Moves(uint8_t piece, uint8_t column, uint8_t row)
default: default:
break; break;
} }
return ret_val;
} }
/** /**
@ -638,6 +654,31 @@ static void Mark_Taken_Piece_Spots_In_Jail(void)
} }
} }
bool Check_If_Player_Can_Move(bool white)
{
for (uint8_t row = 0; row < 8u; row++)
{
for (uint8_t column = 0; column < 8u; column++)
{
if((white_team(Board_State[row][column]) == white))
{
SDL_Log("move: Row:%d, Col:%d", row, column);
if(Mark_Potential_Moves(Board_State[row][column], column, row))
{
SDL_Log("Player Can still move: Row:%d, Col:%d", row, column);
clear_lights();
return true;
}
}
}
}
clear_lights();
SDL_Log("Player cant move");
return false;
}
/** /**
* @brief Function for switching the players turn. Incharge of handling the state machine reset. * @brief Function for switching the players turn. Incharge of handling the state machine reset.
*/ */
@ -650,8 +691,21 @@ 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.
//Last thing we need to check before sitching turns is to check if the game is over.
bool player_can_play = Check_If_Player_Can_Move(White_Turn);
if(!player_can_play)
{
if(Check[white_black_idx])
{
Game_State = White_Turn ? GAME_STATE_OVER_BLACK_WIN : GAME_STATE_OVER_WHITE_WIN;
}
else
{
Game_State = GAME_STATE_OVER_STALE_MATE;
}
}
} }
/** /**
@ -712,13 +766,13 @@ static bool Converting_Pawn_If_Applicable(uint8_t row, uint8_t column)
if((row == Converting_Pawn_Row_Col[0]) && if((row == Converting_Pawn_Row_Col[0]) &&
(Converting_Pawn_Row_Col[1] == column)) (Converting_Pawn_Row_Col[1] == column))
{ {
//Putting the peice down on the board //Putting the piece down on the board
if(Board_State[row][column] == SQUARE_EMPTY) if(Board_State[row][column] == SQUARE_EMPTY)
{ {
Board_State[row][column] = Pawn_Converted_To; Board_State[row][column] = Pawn_Converted_To;
Board_Lights[row][column] = LIGHT_OFF; Board_Lights[row][column] = LIGHT_OFF;
} }
//Picking the peice back up to toggle through the options //Picking the piece back up to toggle through the options
else else
{ {
Board_State[row][column] = SQUARE_EMPTY; Board_State[row][column] = SQUARE_EMPTY;
@ -821,17 +875,13 @@ static void Board_Square_Was_Toggled(uint8_t j, uint8_t i)
} }
Selected_Piece = Board_State[j][i]; Selected_Piece = Board_State[j][i];
Board_State[j][i] = SQUARE_EMPTY; Board_State[j][i] = SQUARE_EMPTY;
Mark_Potential_Moves(Selected_Piece, i, j); (void)Mark_Potential_Moves(Selected_Piece, i, j);
Board_Lights[j][i] = PIECE_ORIGIN; Board_Lights[j][i] = PIECE_ORIGIN;
Game_State++; Game_State++;
} }
else else
{ {
if(Converting_Pawn_If_Applicable(j, i)) if(!Converting_Pawn_If_Applicable(j, i))
{
}
else
{ {
Last_Game_State = Game_State; Last_Game_State = Game_State;
Game_State = GAME_STATE_ERROR_DETECTED; Game_State = GAME_STATE_ERROR_DETECTED;
@ -842,7 +892,7 @@ static void Board_Square_Was_Toggled(uint8_t j, uint8_t i)
break; break;
} }
/* Person is in the middle of taking a turn for example they might already have a peice in the hand*/ /* Person is in the middle of taking a turn for example they might already have a piece 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:
{ {
@ -933,11 +983,7 @@ static void Board_Square_Was_Toggled(uint8_t j, uint8_t i)
} }
else else
{ {
if(Converting_Pawn_If_Applicable(j, i)) if(!Converting_Pawn_If_Applicable(j, i))
{
}
else
{ {
Last_Game_State = Game_State; Last_Game_State = Game_State;
Game_State = GAME_STATE_ERROR_DETECTED; Game_State = GAME_STATE_ERROR_DETECTED;