chessmcu/Piece.cpp
2025-08-04 23:14:56 -04:00

99 lines
No EOL
2.8 KiB
C++

// © 2025 A.M. Rowsell <amr@frzn.dev>
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
// This Source Code Form is "Incompatible With Secondary Licenses", as
// defined by the Mozilla Public License, v. 2.0.
#include "Piece.hpp"
Piece::~Piece() {
return;
}
// because the Piece class is instantiated in another class using
// unique_ptr, non-pure virtual functions apparently *must*
// have definitions, or the linker freaks out
// so this is just a stub that does nothing. it doesn't matter
// because only the derived class versions should be called.
std::vector<Move> Piece::getLegalMoves(const Square &from, const Board &board) const {
std::vector<Move> moveList;
return moveList;
}
std::vector<Move> King::getLegalMoves(const Square &from, const Board &board) const {
std::vector<Move> moveList;
const int directions[8][2] = {
{-1, 0}, // Up
{-1, -1}, // up-left
{-1, 1}, // up-right
{1, 0}, // Down
{1, -1}, // down-left
{1, 1}, // down-right
{0, -1}, // Left
{0, 1} // Right
};
return moveList;
}
std::vector<Move> Rook::getLegalMoves(const Square &from, const Board &board) const {
std::vector<Move> moveList;
const int directions[4][2] = {
{-1, 0}, // Up
{1, 0}, // Down
{0, -1}, // Left
{0, 1} // Right
};
for (auto& dir : directions) {
int r = from.rank + dir[0];
int f = from.file + dir[1];
while (r >= 0 && r < 8 && f >= 0 && f < 8) {
const auto& target = board.boardGrid[r][f];
auto ra = static_cast<Rank> (r);
auto fi = static_cast<File> (f);
Square targetSquare = {ra, fi};
if (!target) {
moveList.push_back({from,
targetSquare});
} else if (target->getColour() != this->colour) {
moveList.push_back({from,
targetSquare});
break;
} else {
break;
}
r += dir[0];
f += dir[1];
}
}
return moveList;
}
std::vector<Move> Queen::getLegalMoves(const Square &from, const Board &board) const {
std::vector<Move> moveList;
return moveList;
}
std::vector<Move> Knight::getLegalMoves(const Square &from, const Board &board) const {
std::vector<Move> moveList;
return moveList;
}
std::vector<Move> Bishop::getLegalMoves(const Square &from, const Board &board) const {
std::vector<Move> moveList;
return moveList;
}
std::vector<Move> Pawn::getLegalMoves(const Square &from, const Board &board) const {
std::vector<Move> moveList;
return moveList;
}