Skip to content

Commit

Permalink
highlight last move made
Browse files Browse the repository at this point in the history
  • Loading branch information
spiroskou committed Aug 16, 2024
1 parent 2ca0811 commit b5b581d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 18 deletions.
4 changes: 0 additions & 4 deletions Board/Board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,10 +508,6 @@ int minimax(int depth, int alpha, int beta, bool isMaximizingPlayer)
for (const Move& move : board->getPossibleMoves(currentTurn)) {
std::shared_ptr<Piece> capturedPiece = board->getPiece(move.dest_row, move.dest_col);
board->makeMove(move);
if (board->isKingInCheck(currentTurn)) {
board->undoMove(move, capturedPiece);
continue;
}
int eval = minimax(depth - 1, alpha, beta, true);
board->undoMove(move, capturedPiece);
minEval = std::min(minEval, eval);
Expand Down
39 changes: 36 additions & 3 deletions ChessSDL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,37 @@ int ChessSDL_MakePreparations()
return 0;
}

void ChessSDL_RenderChessBoard()
void ChessSDL_HighlightLastMove()
{
std::shared_ptr<Board> board = getBoard();
Move move = board->getLastMove();

if (move.src_col == -1) return;

SDL_Renderer* renderer = getRenderer();

SDL_SetRenderDrawColor(renderer, 255, 255, 130, 255);
SDL_Rect tile = { move.src_col * TILE_SIZE, move.src_row * TILE_SIZE, TILE_SIZE, TILE_SIZE };
SDL_RenderFillRect(renderer, &tile);
tile = { move.dest_col * TILE_SIZE, move.dest_row * TILE_SIZE, TILE_SIZE, TILE_SIZE };
SDL_RenderFillRect(renderer, &tile);

std::shared_ptr<Piece> piece = board->getPiece(move.dest_row, move.dest_col);
if (piece) {
std::string imagePath;

imagePath = piece->getImagePath();
SDL_Texture* texture = getTexture(imagePath);
if (texture) {
SDL_Rect pieceRect = { move.dest_col * TILE_SIZE, move.dest_row * TILE_SIZE, TILE_SIZE, TILE_SIZE };
SDL_RenderCopy(renderer, texture, nullptr, &pieceRect);
}
}

SDL_RenderPresent(renderer);
}

void ChessSDL_RenderChessBoard(bool lastMove)
{
SDL_Renderer* renderer = getRenderer();

Expand All @@ -157,12 +187,13 @@ void ChessSDL_RenderChessBoard()
bool white = true;
for (int row = 0; row < ROWS; ++row) {
for (int col = 0; col < COLS; ++col) {

if (white) {
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); // White
}
else {
} else {
SDL_SetRenderDrawColor(renderer, 128, 128, 128, 255); // Gray
}

SDL_Rect tile = { col * TILE_SIZE, row * TILE_SIZE, TILE_SIZE, TILE_SIZE };
SDL_RenderFillRect(renderer, &tile);
white = !white;
Expand All @@ -185,6 +216,8 @@ void ChessSDL_RenderChessBoard()
}

SDL_RenderPresent(renderer);

if (lastMove) ChessSDL_HighlightLastMove();
}

static void showCheckmateMessage()
Expand Down
3 changes: 2 additions & 1 deletion ChessSDL.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ const int TILE_SIZE = SCREEN_WIDTH / COLS;

int ChessSDL_MakePreparations();
void ChessSDL_Close();
void ChessSDL_RenderChessBoard();
void ChessSDL_RenderChessBoard(bool lastMove);
void ChessSDL_ShowMoveMessage(MoveResult res);
void ChessSDL_HighlightLastMove();
16 changes: 6 additions & 10 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,11 @@ int main(int argc, char* args[])

bool quit = false;
SDL_Event e;
int print = 0;
ChessSDL_RenderChessBoard(false);

while (!quit) {
while (SDL_PollEvent(&e) != 0) {

if (!print) {
std::cout << (getTurnCounter() % 2 != 0 ? "Player1 (White) " : "Player2 (Black) ") << "turn" << std::endl;
print = 1;
}

if (e.type == SDL_QUIT) {
quit = true;
} else if (e.type == SDL_MOUSEBUTTONDOWN) {
Expand All @@ -47,18 +42,18 @@ int main(int argc, char* args[])
int dest_row = row;
int dest_col = col;
click_counter = 0;
print = 0;

MoveResult res = makeTheMove(src_row, src_col, dest_row, dest_col);

if (res == MoveResult::Checkmate || res == MoveResult::Stalemate) {
ChessSDL_RenderChessBoard();
ChessSDL_RenderChessBoard(true);
ChessSDL_ShowMoveMessage(res);
quit = true;
break;
}

if (res == MoveResult::ValidMove) {
ChessSDL_RenderChessBoard(true);
IncrementTurnCounter();
}
ChessSDL_ShowMoveMessage(res);
Expand All @@ -73,18 +68,19 @@ int main(int argc, char* args[])
MoveResult aiRes = makeTheMove(aiMove.src_row, aiMove.src_col, aiMove.dest_row, aiMove.dest_col);

if (aiRes == MoveResult::Checkmate || aiRes == MoveResult::Stalemate) {
ChessSDL_RenderChessBoard();
ChessSDL_RenderChessBoard(true);
ChessSDL_ShowMoveMessage(aiRes);
quit = true;
}

if (aiRes == MoveResult::ValidMove) {
ChessSDL_RenderChessBoard(true);
IncrementTurnCounter();
}
ChessSDL_ShowMoveMessage(aiRes);
}
}
ChessSDL_RenderChessBoard();

}

ChessSDL_Close();
Expand Down

0 comments on commit b5b581d

Please sign in to comment.