-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknight.cpp
More file actions
32 lines (26 loc) · 996 Bytes
/
Copy pathknight.cpp
File metadata and controls
32 lines (26 loc) · 996 Bytes
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
#include "Knight.h"
Knight::Knight(PieceColor color, int row, int col) : ChessPiece(ChessPiece::Knight, color, row, col) {
QString filename = (color == White)? ":/images/wn.png" : ":/images/bn.png";
QPixmap piecePixmap(filename);
setPixmap(piecePixmap.scaled(96, 96, Qt::KeepAspectRatio, Qt::SmoothTransformation));
}
QVector<QPair<int, int>> Knight::getValidMoves(const QVector<QVector<ChessPiece*>> &board) {
QVector<QPair<int, int>> moves;
const int row = getRow();
const int col = getCol();
const int jumps[8][2] = {
{-2, -1}, {-2, 1}, {-1, -2}, {-1, 2},
{1, -2}, {1, 2}, {2, -1}, {2, 1}
};
for(auto& move : jumps){
int r = row + move[0];
int c = col + move[1];
if(r >= 0 && r < 8 && c >= 0 && c < 8){
ChessPiece* target = board[r][c];
if(target == nullptr || target->getColor() != getColor()){
moves.append({r, c});
}
}
}
return moves;
}