dev: working on check detection algorithm
This commit is contained in:
parent
68ed9ce922
commit
31499ed6e1
1 changed files with 50 additions and 7 deletions
57
Piece.cpp
57
Piece.cpp
|
|
@ -25,7 +25,7 @@ std::vector<Move> Piece::getLegalMoves(const Square &from, const Board &board) c
|
|||
|
||||
std::vector<Move> King::getLegalMoves(const Square &from, const Board &board) const {
|
||||
std::vector<Move> moveList;
|
||||
const int directions[8][2] = {
|
||||
std::vector<std::vector<int>> directions = {
|
||||
{-1, 0}, // Up
|
||||
{-1, -1}, // up-left
|
||||
{-1, 1}, // up-right
|
||||
|
|
@ -39,11 +39,12 @@ std::vector<Move> King::getLegalMoves(const Square &from, const Board &board) co
|
|||
// establish r/f for square to check
|
||||
int r = from.rank + dir[0];
|
||||
int f = from.file + dir[1];
|
||||
if(r >= 0 && r < 8 && f >= 0 && f < 8) {
|
||||
auto ra = static_cast<Rank>(r);
|
||||
auto fi = static_cast<File>(f);
|
||||
Square targetSquare = {ra, fi};
|
||||
if(targetSquare.isValid()) {
|
||||
const auto &target = board.boardGrid[r][f]; // examine the target square
|
||||
auto ra = static_cast<Rank>(r);
|
||||
auto fi = static_cast<File>(f);
|
||||
Square targetSquare = {ra, fi};
|
||||
|
||||
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
|
||||
|
|
@ -61,19 +62,61 @@ std::vector<Move> King::getLegalMoves(const Square &from, const Board &board) co
|
|||
// - Determine the piece type
|
||||
// - Determine the direction we are looking at (diagonal or not)
|
||||
// - If that piece can attack in that direction, it would be check, so remove this move from moveList
|
||||
// We need to add knight attack to the directions to check
|
||||
/*
|
||||
* __3_4___
|
||||
* _1___2__
|
||||
* ___K____
|
||||
* _5___6__
|
||||
* __7_8___
|
||||
*
|
||||
*/
|
||||
std::vector<std::vector<int>> knightAttacks = {
|
||||
{-1, -2}, // 1
|
||||
{-1, 2}, // 2
|
||||
{-2, -1}, // 3
|
||||
{-2, 1}, // 4
|
||||
{1, -2}, // 5
|
||||
{1, 2}, // 6
|
||||
{2, -1}, // 7
|
||||
{2, 1} // 8
|
||||
};
|
||||
directions.insert(directions.end(), knightAttacks.begin(), knightAttacks.end()); // concatenate the knight attacks
|
||||
for(auto &moves : moveList) {
|
||||
Square startSquare = moves.to; // get the potential move to square
|
||||
Square startSquare = moves.to; // get the potential moveto square
|
||||
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
|
||||
while(r >= 0 && r < 8 && f >= 0 && f < 8) {
|
||||
auto &target = board.boardGrid[r][f]; // check what's at the square we're examining
|
||||
if(target && target->getColour() != this->getColour()) { // is it occupied & an enemy?
|
||||
if(!target) {
|
||||
// empty square, continue
|
||||
continue;
|
||||
}
|
||||
if(target && (target->getColour() != this->getColour())) { // is it occupied & an enemy?
|
||||
if((target->getPieceType() == QUEEN || target->getPieceType() == BISHOP) && diagonal) {
|
||||
// we are being attacked diagonally on this square, so remove it
|
||||
//moves.to.rank = 255; moves.to.file = 255; // mark as invalid???
|
||||
}
|
||||
if(target->getPieceType() == KNIGHT && knight) {
|
||||
// we are being attacked by a knight, so remove it
|
||||
//moves.to.rank = 255; moves.to.file = 255;
|
||||
}
|
||||
if(target->getPieceType() == ROOK && (!diagonal && !knight)) {
|
||||
// again, being attacked, remove it
|
||||
}
|
||||
}
|
||||
if(target && (target->getColour() == this->getColour())) {
|
||||
// we've hit a friendly, stop
|
||||
break;
|
||||
}
|
||||
if(!knight) {
|
||||
r += dir[0];
|
||||
f += dir[1];
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue