diff --git a/.gitignore b/.gitignore index 18a9398..f034f68 100644 --- a/.gitignore +++ b/.gitignore @@ -39,4 +39,3 @@ chessmcu_default/ # End of https://www.toptal.com/developers/gitignore/api/mplabx -localTest/ \ No newline at end of file diff --git a/Piece.cpp b/Piece.cpp index bab48ec..40555f7 100644 --- a/Piece.cpp +++ b/Piece.cpp @@ -25,7 +25,7 @@ std::vector Piece::getLegalMoves(const Square &from, const Board &board) c std::vector King::getLegalMoves(const Square &from, const Board &board) const { std::vector moveList; - std::vector> directions = { + const int directions[8][2] = { {-1, 0}, // Up {-1, -1}, // up-left {-1, 1}, // up-right @@ -39,12 +39,11 @@ std::vector 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]; - auto ra = static_cast(r); - auto fi = static_cast(f); - Square targetSquare = {ra, fi}; - if(targetSquare.isValid()) { + if(r >= 0 && r < 8 && f >= 0 && f < 8) { const auto &target = board.boardGrid[r][f]; // examine the target square - + auto ra = static_cast(r); + auto fi = static_cast(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 @@ -62,61 +61,19 @@ std::vector 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> 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 moveto square + Square startSquare = moves.to; // get the potential move to 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) { - // empty square, continue - continue; - } - if(target && (target->getColour() != this->getColour())) { // is it occupied & an enemy? + 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; } } } diff --git a/inc/strFuncs.hpp b/inc/strFuncs.hpp deleted file mode 100644 index 2ea4945..0000000 --- a/inc/strFuncs.hpp +++ /dev/null @@ -1,23 +0,0 @@ -/* - * File: strFuncs.hpp - * Author: amr - * - * Created on August 27, 2025, 12:58 PM - */ - -#ifndef STRFUNCS_HPP -#define STRFUNCS_HPP - -#include -#include -#include -#include - -template -void split(const std::string &s, char delim, Out result); - -std::vector split(const std::string &s, char delim); - - -#endif /* STRFUNCS_HPP */ - diff --git a/strFuncs.cpp b/strFuncs.cpp deleted file mode 100644 index 9e09da6..0000000 --- a/strFuncs.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include "inc/strFuncs.hpp" - -template -void split(const std::string &s, char delim, Out result) { - std::istringstream iss(s); - std::string item; - while (std::getline(iss, item, delim)) { - *result++ = item; - } -} - -std::vector split(const std::string &s, char delim) { - std::vector elems; - split(s, delim, std::back_inserter(elems)); - return elems; -}