-
Notifications
You must be signed in to change notification settings - Fork 0
/
bishop.cpp
57 lines (48 loc) · 2.09 KB
/
bishop.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "bishop.h"
#include "game.h"
Bishop::Bishop(const char* filename, SDL_Renderer* ren, int bRow, int bCol, char c)
: Piece(filename, ren, bRow, bCol, c) {}
Bishop::~Bishop() {}
bool Bishop::move(int toRow, int toCol, char blueRectangles[][8])
{
return blueRectangles[toRow][toCol] == BLUE_RECTANGLE;
}
void Bishop::displayBlueRectangles(int fromRow, int fromCol, int board[][8], char colors[][8], char blueRectangles[][8], bool forChecks)
{
if (colors[fromRow][fromCol] == WHITE)
updateRectangles(WHITE, fromRow, fromCol, board, colors, blueRectangles, forChecks);
else if (colors[fromRow][fromCol] == BLACK)
updateRectangles(BLACK, fromRow, fromCol, board, colors, blueRectangles, forChecks);
}
void Bishop::updateRectangles(char color, int fromRow, int fromCol, int board[][8], char colors[][8], char blueRectangles[][8], bool forChecks)
{
// Define the possible directions for diagonal moves
int directions[4][2] = { {1, 1}, {1, -1}, {-1, 1}, {-1, -1} };
char enemyColor;
for (size_t d = 0; d < 4; d++)
{
int dirRow = directions[d][0];
int dirCol = directions[d][1];
for (size_t step = 1; step < 8; step++) {
int toRow = fromRow + step * dirRow;
int toCol = fromCol + step * dirCol;
enemyColor = colors[fromRow][fromCol] == WHITE ? BLACK : WHITE;
// Check if the position is within the bounds of the chessboard
if (toRow >= 0 && toRow < 8 && toCol >= 0 && toCol < 8)
{
if (colors[toRow][toCol] == EMPTY || colors[toRow][toCol] != color || (forChecks && colors[toRow][toCol] == color))
blueRectangles[toRow][toCol] = BLUE_RECTANGLE;
// Stop further moves if the position is not empty
if (colors[toRow][toCol] != EMPTY)
{
if (colors[toRow][toCol] == enemyColor && board[toRow][toCol] == KING)
continue;
break;
}
}
// Stop when going out of the board
else
break;
}
}
}