dev: added Pawn movement checks, working through legal move logic
This commit is contained in:
parent
c577c23f1f
commit
016654da2f
2 changed files with 102 additions and 2 deletions
80
Piece.cpp
80
Piece.cpp
|
|
@ -58,7 +58,7 @@ std::vector<Move> Rook::getLegalMoves(const Square &from, const Board &board) co
|
|||
if (!target) {
|
||||
moveList.push_back({from,
|
||||
targetSquare});
|
||||
} else if (target->getColour() != this->colour) {
|
||||
} else if (target->getColour() != this->getColour()) {
|
||||
moveList.push_back({from,
|
||||
targetSquare});
|
||||
break;
|
||||
|
|
@ -94,6 +94,84 @@ std::vector<Move> Bishop::getLegalMoves(const Square &from, const Board &board)
|
|||
|
||||
std::vector<Move> Pawn::getLegalMoves(const Square &from, const Board &board) const {
|
||||
std::vector<Move> moveList;
|
||||
const int directions[2][4][2] = {
|
||||
{ // black
|
||||
{-1, 0}, // Up
|
||||
{-2, 0}, // 2up first move
|
||||
{-1, 1}, // attack to right
|
||||
{-1, -1} // attack to left
|
||||
},
|
||||
{ // white
|
||||
{1, 0}, // down
|
||||
{2, 0}, // 2down first move
|
||||
{1, 1}, // attack to right
|
||||
{1, -1} // attack to left
|
||||
}
|
||||
};
|
||||
|
||||
if (this->getColour() == PIECE_BLACK) {
|
||||
// go through black options
|
||||
for (auto &dir : directions[0]) {
|
||||
int r = from.rank + dir[0];
|
||||
int f = from.file + dir[1];
|
||||
if (r >= 0 && r < 8 && f >= 0 && f < 8) { // no need for a while loop as we only have finite moves in limited directions
|
||||
const auto& target = board.boardGrid[r][f];
|
||||
auto ra = static_cast<Rank> (r);
|
||||
auto fi = static_cast<File> (f);
|
||||
Square targetSquare = {ra, fi};
|
||||
|
||||
if (dir[0] == -2 && !this->checkIfMoved()) {
|
||||
// then 2 is potentially legal
|
||||
if (!target && !(board.boardGrid[r + 1][f])) // check both squares for pieces of any colour
|
||||
moveList.push_back({from, targetSquare});
|
||||
else
|
||||
break;
|
||||
} else if (abs(dir[1]) == 1) { // attempted capture (diagonal)
|
||||
if (target && target->getColour() != this->getColour()) {
|
||||
// legal capture
|
||||
moveList.push_back({from, targetSquare});
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} else { // normal one square forward
|
||||
if (!target)
|
||||
moveList.push_back({from, targetSquare});
|
||||
}
|
||||
// now check if we are on 8th rank, for promotion
|
||||
}
|
||||
}
|
||||
} else if (this->getColour() == PIECE_WHITE) {
|
||||
for (auto &dir : directions[1]) {
|
||||
// go through white options
|
||||
int r = from.rank + dir[0];
|
||||
int f = from.file + dir[1];
|
||||
if (r >= 0 && r < 8 && f >= 0 && f < 8) { // no need for a while loop as we only have finite moves in limited directions
|
||||
const auto& target = board.boardGrid[r][f];
|
||||
auto ra = static_cast<Rank> (r);
|
||||
auto fi = static_cast<File> (f);
|
||||
Square targetSquare = {ra, fi};
|
||||
|
||||
if (dir[0] == 2 && !this->checkIfMoved()) {
|
||||
// then 2 is potentially legal
|
||||
if (!target && !(boardGrid[r - 1][f]))
|
||||
moveList.push_back({from, targetSquare});
|
||||
else
|
||||
break;
|
||||
} else if (abs(dir[1]) == 1) { // attempted capture (diagonal)
|
||||
if (target && target->getColour() != this->getColour()) { // can only move there if it's a capture
|
||||
// legal capture
|
||||
moveList.push_back({from, targetSquare});
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} else { // normal one square forward
|
||||
if (!target)
|
||||
moveList.push_back({from, targetSquare});
|
||||
}
|
||||
// now check if we are on 8th rank, for promotion
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return moveList;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue