-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknight.cpp
More file actions
41 lines (34 loc) · 1.4 KB
/
Copy pathknight.cpp
File metadata and controls
41 lines (34 loc) · 1.4 KB
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
#include "knight.h"
#include "game.h"
#include <iostream>
Knight::Knight(const char* filename, SDL_Renderer* ren, int bRow, int bCol, char c)
: Piece(filename, ren, bRow, bCol, c) {}
Knight::~Knight() {}
bool Knight::move(int toRow, int toCol, char blueRectangles[][8])
{
return blueRectangles[toRow][toCol] == BLUE_RECTANGLE;
}
void Knight::displayBlueRectangles(int fromRow, int fromCol, int board[][8], char colors[][8], char blueRectangles[][8], bool forChecks)
{
if (colors[fromRow][fromCol] == WHITE)
updateRectangles(BLACK, fromRow, fromCol, board, colors, blueRectangles, forChecks);
if (colors[fromRow][fromCol] == BLACK)
updateRectangles(WHITE, fromRow, fromCol, board, colors, blueRectangles, forChecks);
}
void Knight::updateRectangles(char color, int fromRow, int fromCol, int board[][8], char colors[][8], char blueRectangles[][8], bool forChecks)
{
const int moves[][2] =
{
{2, 1}, {2, -1}, {-2, 1}, {-2, -1},
{1, 2}, {-1, 2}, {1, -2}, {-1, -2}
};
for (int i = 0; i < 8; ++i) {
int newRow = fromRow + moves[i][0];
int newCol = fromCol + moves[i][1];
if (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8 &&
(colors[newRow][newCol] == EMPTY || colors[newRow][newCol] == color ||
(forChecks && colors[fromRow][fromCol] != color))) {
blueRectangles[newRow][newCol] = BLUE_RECTANGLE;
}
}
}