style: implement clang-format styling parameters
This commit is contained in:
parent
10a4dcb60b
commit
f2ce83335c
10 changed files with 462 additions and 547 deletions
237
Piece.cpp
237
Piece.cpp
|
|
@ -37,26 +37,26 @@ bool Piece::finalMoveChecks(std::vector<Move> *moveList, Board &board) {
|
|||
|
||||
// *INDENT-OFF*
|
||||
//
|
||||
// ## #
|
||||
// #
|
||||
// # ## ## # ## ## #
|
||||
// # # # # # # #
|
||||
// ## # # # # #
|
||||
// # # # # # # #
|
||||
// ## ## ##### ### ## ###
|
||||
// #
|
||||
// ###
|
||||
// ## #
|
||||
// #
|
||||
// # ## ## # ## ## #
|
||||
// # # # # # # #
|
||||
// ## # # # # #
|
||||
// # # # # # # #
|
||||
// ## ## ##### ### ## ###
|
||||
// #
|
||||
// ###
|
||||
// *INDENT-ON*
|
||||
bool King::checkForCheck(Board &board) const {
|
||||
std::vector<std::vector<int>> kingVulnerable = {
|
||||
{-1, 0}, // Up
|
||||
{-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
|
||||
{-1, 1}, // up-right
|
||||
{1, 0}, // Down
|
||||
{1, -1}, // down-left
|
||||
{1, 1}, // down-right
|
||||
{0, -1}, // Left
|
||||
{0, 1}, // Right
|
||||
{-1, -2}, // 1
|
||||
{-1, 2}, // 2
|
||||
{-2, -1}, // 3
|
||||
|
|
@ -73,14 +73,14 @@ bool King::checkForCheck(Board &board) const {
|
|||
std::vector<Move> King::getLegalMoves(const Square &from, Board &board) const {
|
||||
std::vector<Move> moveList;
|
||||
std::vector<std::vector<int>> directions = {
|
||||
{-1, 0}, // Up
|
||||
{-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
|
||||
{-1, 1}, // up-right
|
||||
{1, 0}, // Down
|
||||
{1, -1}, // down-left
|
||||
{1, 1}, // down-right
|
||||
{0, -1}, // Left
|
||||
{0, 1} // Right
|
||||
};
|
||||
for(auto &dir : directions) {
|
||||
// establish r/f for square to check
|
||||
|
|
@ -89,10 +89,11 @@ std::vector<Move> King::getLegalMoves(const Square &from, Board &board) const {
|
|||
Square targetSquare{static_cast<Rank>(r), static_cast<File>(f)};
|
||||
if(targetSquare.isValid()) {
|
||||
const Piece *target = board.getPieceAt(targetSquare); // examine the target square
|
||||
if(!target) { // if square is empty (nullptr)
|
||||
moveList.push_back({from, targetSquare}); // then it's potentially a legal move
|
||||
} else if(target && target->getColour() != this->getColour()) { // if it's occupied with a piece of opposite colour
|
||||
moveList.push_back({from, targetSquare}); // then again it's potentially legal
|
||||
if(!target) { // if square is empty (nullptr)
|
||||
moveList.push_back({from, targetSquare}); // then it's potentially a legal move
|
||||
} else if(target &&
|
||||
target->getColour() != this->getColour()) { // if it's occupied with a piece of opposite colour
|
||||
moveList.push_back({from, targetSquare}); // then again it's potentially legal
|
||||
break;
|
||||
} else {
|
||||
// otherwise it's one of our pieces
|
||||
|
|
@ -128,11 +129,11 @@ std::vector<Move> King::getLegalMoves(const Square &from, Board &board) const {
|
|||
directions.insert(directions.end(), knightAttacks.begin(), knightAttacks.end()); // concatenate the knight attacks
|
||||
for(auto &moves : moveList) {
|
||||
Square startSquare = moves.to; // get the potential moveto square
|
||||
for(auto &dir : directions) { // now go through all directions
|
||||
for(auto &dir : directions) { // now go through all directions
|
||||
int r = startSquare.rank + dir[0];
|
||||
int f = startSquare.file + dir[1];
|
||||
bool diagonal = (abs(dir[0]) + abs(dir[1]) == 2) ? 1 : 0; // check if diagonal
|
||||
bool knight = (abs(dir[0]) + abs(dir[1]) == 3) ? 1 : 0; // check if knight attack
|
||||
bool knight = (abs(dir[0]) + abs(dir[1]) == 3) ? 1 : 0; // check if knight attack
|
||||
while(r >= 0 && r < 8 && f >= 0 && f < 8) {
|
||||
Square targetSquare{static_cast<Rank>(r), static_cast<File>(f)};
|
||||
const Piece *target = board.getPieceAt(targetSquare); // access the square we're examining
|
||||
|
|
@ -149,12 +150,14 @@ std::vector<Move> King::getLegalMoves(const Square &from, Board &board) const {
|
|||
// we are being attacked by a knight, so remove it
|
||||
moves.to.rank = INVALID_RANK;
|
||||
moves.to.file = INVALID_FILE;
|
||||
} else if((target->getPieceType() == ROOK || target->getPieceType() == QUEEN) && (!diagonal && !knight)) {
|
||||
} else if((target->getPieceType() == ROOK || target->getPieceType() == QUEEN) &&
|
||||
(!diagonal && !knight)) {
|
||||
// again, being attacked, remove it
|
||||
moves.to.rank = INVALID_RANK;
|
||||
moves.to.file = INVALID_FILE;
|
||||
}
|
||||
if(target->getPieceType() == PAWN && (abs(r - startSquare.rank) == 1 && abs(f - startSquare.file) == 1)) {
|
||||
if(target->getPieceType() == PAWN &&
|
||||
(abs(r - startSquare.rank) == 1 && abs(f - startSquare.file) == 1)) {
|
||||
moves.to.rank = INVALID_RANK;
|
||||
moves.to.file = INVALID_FILE;
|
||||
}
|
||||
|
|
@ -173,23 +176,19 @@ std::vector<Move> King::getLegalMoves(const Square &from, Board &board) const {
|
|||
}
|
||||
}
|
||||
// will this actually work?
|
||||
moveList.erase(
|
||||
std::remove_if(moveList.begin(), moveList.end(),
|
||||
[&](const auto & x) {
|
||||
return !x.to.isValid();
|
||||
}));
|
||||
moveList.erase(std::remove_if(moveList.begin(), moveList.end(), [&](const auto &x) { return !x.to.isValid(); }));
|
||||
return moveList;
|
||||
}
|
||||
|
||||
// *INDENT-OFF*
|
||||
//
|
||||
// ##
|
||||
// #
|
||||
// # ### ## ## # ##
|
||||
// # # # # # # #
|
||||
// # # # # # ##
|
||||
// # # # # # # #
|
||||
// #### ## ## ## ##
|
||||
// ##
|
||||
// #
|
||||
// # ### ## ## # ##
|
||||
// # # # # # # #
|
||||
// # # # # # ##
|
||||
// # # # # # # #
|
||||
// #### ## ## ## ##
|
||||
//
|
||||
// *INDENT-OM*
|
||||
|
||||
|
|
@ -197,9 +196,9 @@ std::vector<Move> Rook::getLegalMoves(const Square &from, Board &board) const {
|
|||
std::vector<Move> moveList;
|
||||
const int directions[4][2] = {
|
||||
{-1, 0}, // Up
|
||||
{1, 0}, // Down
|
||||
{1, 0}, // Down
|
||||
{0, -1}, // Left
|
||||
{0, 1} // Right
|
||||
{0, 1} // Right
|
||||
};
|
||||
for(auto &dir : directions) {
|
||||
int r = from.rank + dir[0];
|
||||
|
|
@ -222,26 +221,26 @@ std::vector<Move> Rook::getLegalMoves(const Square &from, Board &board) const {
|
|||
}
|
||||
|
||||
// *INDENT-OFF*
|
||||
// ## # ## ## ## ## # ##
|
||||
// # # # # # # # # # #
|
||||
// # # # # ### ### # #
|
||||
// # # # # # # # #
|
||||
// ### ## # ### ### ### ##
|
||||
// #
|
||||
// ###
|
||||
// ## # ## ## ## ## # ##
|
||||
// # # # # # # # # # #
|
||||
// # # # # ### ### # #
|
||||
// # # # # # # # #
|
||||
// ### ## # ### ### ### ##
|
||||
// #
|
||||
// ###
|
||||
// *INDENT-ON*
|
||||
|
||||
std::vector<Move> Queen::getLegalMoves(const Square &from, Board &board) const {
|
||||
std::vector<Move> moveList;
|
||||
std::vector<std::vector<int>> directions = {
|
||||
{-1, 0}, // Up
|
||||
{-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
|
||||
{-1, 1}, // up-right
|
||||
{1, 0}, // Down
|
||||
{1, -1}, // down-left
|
||||
{1, 1}, // down-right
|
||||
{0, -1}, // Left
|
||||
{0, 1} // Right
|
||||
};
|
||||
for(auto &dir : directions) {
|
||||
// establish r/f for square to check
|
||||
|
|
@ -249,11 +248,12 @@ std::vector<Move> Queen::getLegalMoves(const Square &from, Board &board) const {
|
|||
int f = from.file + dir[1];
|
||||
while(r >= 0 && r < 8 && f >= 0 && f < 8) {
|
||||
Square targetSquare{static_cast<Rank>(r), static_cast<File>(f)};
|
||||
const Piece *target = board.getPieceAt(targetSquare);// examine the target square
|
||||
if(!target) { // if square is empty (NULL)
|
||||
moveList.push_back({from, targetSquare}); // then it's potentially a legal move
|
||||
} else if(target && target->getColour() != this->getColour()) { // if it's occupied with a piece of opposite colour
|
||||
moveList.push_back({from, targetSquare}); // then again it's potentially legal
|
||||
const Piece *target = board.getPieceAt(targetSquare); // examine the target square
|
||||
if(!target) { // if square is empty (NULL)
|
||||
moveList.push_back({from, targetSquare}); // then it's potentially a legal move
|
||||
} else if(target &&
|
||||
target->getColour() != this->getColour()) { // if it's occupied with a piece of opposite colour
|
||||
moveList.push_back({from, targetSquare}); // then again it's potentially legal
|
||||
break;
|
||||
} else {
|
||||
// otherwise it's one of our pieces
|
||||
|
|
@ -267,29 +267,20 @@ std::vector<Move> Queen::getLegalMoves(const Square &from, Board &board) const {
|
|||
}
|
||||
|
||||
// *INDENT-OFF*
|
||||
// ## # ## #
|
||||
// # # #
|
||||
// # ## # ## ## ## # ### ####
|
||||
// # # # # # # # # # #
|
||||
// ## # # # # # # # #
|
||||
// # # # # # # # # # # #
|
||||
// ## ## ### ## ##### ### ### ## ##
|
||||
// #
|
||||
// ###
|
||||
// ## # ## #
|
||||
// # # #
|
||||
// # ## # ## ## ## # ### ####
|
||||
// # # # # # # # # # #
|
||||
// ## # # # # # # # #
|
||||
// # # # # # # # # # # #
|
||||
// ## ## ### ## ##### ### ### ## ##
|
||||
// #
|
||||
// ###
|
||||
// *INDENT-ON*
|
||||
|
||||
std::vector<Move> Knight::getLegalMoves(const Square &from, Board &board) const {
|
||||
std::vector<Move> moveList;
|
||||
std::vector<std::vector<int>> directions = {
|
||||
{-1, -2},
|
||||
{-1, 2},
|
||||
{-2, -1},
|
||||
{-2, 1},
|
||||
{1, -2},
|
||||
{1, 2},
|
||||
{2, -1},
|
||||
{2, 1}
|
||||
};
|
||||
std::vector<std::vector<int>> directions = {{-1, -2}, {-1, 2}, {-2, -1}, {-2, 1}, {1, -2}, {1, 2}, {2, -1}, {2, 1}};
|
||||
for(auto &dir : directions) {
|
||||
int r = from.rank + dir[0];
|
||||
int f = from.file + dir[1];
|
||||
|
|
@ -305,24 +296,24 @@ std::vector<Move> Knight::getLegalMoves(const Square &from, Board &board) const
|
|||
}
|
||||
|
||||
// *INDENT-OFF*
|
||||
// ## # ##
|
||||
// # #
|
||||
// ### ## ### ### ## # ##
|
||||
// # # # # # # # # # #
|
||||
// # # # ## # # # # # #
|
||||
// # # # # # # # # # #
|
||||
// #### ##### ### ### ## ## ###
|
||||
// #
|
||||
// ###
|
||||
// ## # ##
|
||||
// # #
|
||||
// ### ## ### ### ## # ##
|
||||
// # # # # # # # # # #
|
||||
// # # # ## # # # # # #
|
||||
// # # # # # # # # # #
|
||||
// #### ##### ### ### ## ## ###
|
||||
// #
|
||||
// ###
|
||||
// *INDENT-ON*
|
||||
|
||||
std::vector<Move> Bishop::getLegalMoves(const Square &from, Board &board) const {
|
||||
std::vector<Move> moveList;
|
||||
std::vector<std::vector<int>> directions = {
|
||||
{-1, -1}, // up-left
|
||||
{-1, 1}, // up-right
|
||||
{1, -1}, // down-left
|
||||
{1, 1}, // down-right
|
||||
{-1, 1}, // up-right
|
||||
{1, -1}, // down-left
|
||||
{1, 1}, // down-right
|
||||
};
|
||||
for(auto &dir : directions) {
|
||||
int r = from.rank + dir[0];
|
||||
|
|
@ -347,44 +338,45 @@ std::vector<Move> Bishop::getLegalMoves(const Square &from, Board &board) const
|
|||
|
||||
// *INDENT-OFF*
|
||||
//
|
||||
// # ## ## # # ## # ##
|
||||
// # # # # # # # #
|
||||
// # # ### # # # # #
|
||||
// # # # # # # # #
|
||||
// ### ## # # # ### ##
|
||||
// #
|
||||
// ###
|
||||
// # ## ## # # ## # ##
|
||||
// # # # # # # # #
|
||||
// # # ### # # # # #
|
||||
// # # # # # # # #
|
||||
// ### ## # # # ### ##
|
||||
// #
|
||||
// ###
|
||||
// *INDENT-ON*
|
||||
|
||||
std::vector<Move> Pawn::getLegalMoves(const Square &from, 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
|
||||
}
|
||||
};
|
||||
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
|
||||
if(r >= 0 && r < 8 && f >= 0 &&
|
||||
f < 8) { // no need for a while loop as we only have finite moves in limited directions
|
||||
Square targetSquare{static_cast<Rank>(r), static_cast<File>(f)};
|
||||
const Piece *target = board.getPieceAt(targetSquare);
|
||||
if(dir[0] == -2 && !this->checkIfMoved()) {
|
||||
// then 2 is potentially legal
|
||||
if(!target && board.isSquareEmpty(Square{static_cast<Rank>(r + 1), static_cast<File>(f)})) // check both squares for pieces of any colour
|
||||
if(!target &&
|
||||
board.isSquareEmpty(Square{static_cast<Rank>(r + 1),
|
||||
static_cast<File>(f)})) // check both squares for pieces of any colour
|
||||
moveList.push_back({from, targetSquare});
|
||||
else
|
||||
continue;
|
||||
|
|
@ -410,7 +402,8 @@ std::vector<Move> Pawn::getLegalMoves(const Square &from, Board &board) const {
|
|||
// 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
|
||||
if(r >= 0 && r < 8 && f >= 0 &&
|
||||
f < 8) { // no need for a while loop as we only have finite moves in limited directions
|
||||
Square targetSquare{static_cast<Rank>(r), static_cast<File>(f)};
|
||||
const Piece *target = board.getPieceAt(targetSquare);
|
||||
if(dir[0] == 2 && !this->checkIfMoved()) {
|
||||
|
|
@ -419,7 +412,7 @@ std::vector<Move> Pawn::getLegalMoves(const Square &from, Board &board) const {
|
|||
moveList.push_back({from, targetSquare});
|
||||
else
|
||||
continue;
|
||||
} else if(abs(dir[1]) == 1) { // attempted capture (diagonal)
|
||||
} 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});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue