Compare commits

...

2 commits

Author SHA1 Message Date
A.M. Rowsell
7fad5e53af
dev: added some string helper functions, updated gitignore 2025-08-27 14:20:22 -04:00
A.M. Rowsell
31499ed6e1
dev: working on check detection algorithm 2025-08-26 20:34:04 -04:00
4 changed files with 90 additions and 7 deletions

1
.gitignore vendored
View file

@ -39,3 +39,4 @@ chessmcu_default/
# End of https://www.toptal.com/developers/gitignore/api/mplabx
localTest/

View file

@ -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;
}
}
}

23
inc/strFuncs.hpp Normal file
View file

@ -0,0 +1,23 @@
/*
* File: strFuncs.hpp
* Author: amr
*
* Created on August 27, 2025, 12:58 PM
*/
#ifndef STRFUNCS_HPP
#define STRFUNCS_HPP
#include <string>
#include <sstream>
#include <vector>
#include <iterator>
template <typename Out>
void split(const std::string &s, char delim, Out result);
std::vector<std::string> split(const std::string &s, char delim);
#endif /* STRFUNCS_HPP */

16
strFuncs.cpp Normal file
View file

@ -0,0 +1,16 @@
#include "inc/strFuncs.hpp"
template <typename Out>
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<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
split(s, delim, std::back_inserter(elems));
return elems;
}