Compare commits
No commits in common. "78749a6af2c3c5e559b042107f0143c6a5be77b3" and "7fad5e53aff5840f44387d727d5857774eac26ec" have entirely different histories.
78749a6af2
...
7fad5e53af
5 changed files with 16 additions and 54 deletions
16
Board.cpp
16
Board.cpp
|
|
@ -7,7 +7,6 @@
|
||||||
// defined by the Mozilla Public License, v. 2.0.
|
// defined by the Mozilla Public License, v. 2.0.
|
||||||
|
|
||||||
#include "Board.hpp"
|
#include "Board.hpp"
|
||||||
#include "inc/strFuncs.hpp"
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This is how the chessboard is organized in memory
|
* This is how the chessboard is organized in memory
|
||||||
|
|
@ -86,21 +85,6 @@ void Board::setupInitialPosition() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int setupFromFEN(std::string strFEN) {
|
|
||||||
std::string field1, field2, field3, field4, field5, field6;
|
|
||||||
std::vector<std::string> splitFEN = split(strFEN, ' ');
|
|
||||||
if(splitFEN.size() != 6) { // a valid FEN *must* contain 6 fields
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
field1 = splitFEN[0];
|
|
||||||
field2 = splitFEN[1];
|
|
||||||
field3 = splitFEN[2];
|
|
||||||
field4 = splitFEN[3];
|
|
||||||
field5 = splitFEN[4];
|
|
||||||
field6 = splitFEN[5];
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Board::movePiece(Square from, Square to) {
|
void Board::movePiece(Square from, Square to) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,6 @@ class Board {
|
||||||
void setupInitialPosition();
|
void setupInitialPosition();
|
||||||
Piece *getPieceAt(Square square);
|
Piece *getPieceAt(Square square);
|
||||||
void movePiece(Square from, Square to);
|
void movePiece(Square from, Square to);
|
||||||
int setupFromFEN(std::string strFEN);
|
|
||||||
bool isInBounds(Square square) const;
|
bool isInBounds(Square square) const;
|
||||||
bool isEmpty(Square square) const;
|
bool isEmpty(Square square) const;
|
||||||
uint64_t serialBoard = 0xFFFF00000000FFFF; // opening position
|
uint64_t serialBoard = 0xFFFF00000000FFFF; // opening position
|
||||||
|
|
|
||||||
29
Piece.cpp
29
Piece.cpp
|
|
@ -44,6 +44,7 @@ std::vector<Move> King::getLegalMoves(const Square &from, const Board &board) co
|
||||||
Square targetSquare = {ra, fi};
|
Square targetSquare = {ra, fi};
|
||||||
if(targetSquare.isValid()) {
|
if(targetSquare.isValid()) {
|
||||||
const auto &target = board.boardGrid[r][f]; // examine the target square
|
const auto &target = board.boardGrid[r][f]; // examine the target square
|
||||||
|
|
||||||
if(!target) { // if square is empty (NULL)
|
if(!target) { // if square is empty (NULL)
|
||||||
moveList.push_back({from, targetSquare}); // then it's potentially a legal move
|
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
|
} else if(target && target->getColour() != this->getColour()) { // if it's occupied with a piece of opposite colour
|
||||||
|
|
@ -67,8 +68,8 @@ std::vector<Move> King::getLegalMoves(const Square &from, const Board &board) co
|
||||||
* _1___2__
|
* _1___2__
|
||||||
* ___K____
|
* ___K____
|
||||||
* _5___6__
|
* _5___6__
|
||||||
* __7_8___
|
* __7_8___
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
std::vector<std::vector<int>> knightAttacks = {
|
std::vector<std::vector<int>> knightAttacks = {
|
||||||
{-1, -2}, // 1
|
{-1, -2}, // 1
|
||||||
|
|
@ -89,7 +90,7 @@ std::vector<Move> King::getLegalMoves(const Square &from, const Board &board) co
|
||||||
bool diagonal = (abs(dir[0]) + abs(dir[1]) == 2) ? 1 : 0; // check if diagonal
|
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) {
|
while(r >= 0 && r < 8 && f >= 0 && f < 8) {
|
||||||
auto &target = board.boardGrid[r][f]; // access the square we're examining
|
auto &target = board.boardGrid[r][f]; // check what's at the square we're examining
|
||||||
if(!target) {
|
if(!target) {
|
||||||
// empty square, continue
|
// empty square, continue
|
||||||
continue;
|
continue;
|
||||||
|
|
@ -97,18 +98,14 @@ std::vector<Move> King::getLegalMoves(const Square &from, const Board &board) co
|
||||||
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) {
|
if((target->getPieceType() == QUEEN || target->getPieceType() == BISHOP) && diagonal) {
|
||||||
// we are being attacked diagonally on this square, so remove it
|
// we are being attacked diagonally on this square, so remove it
|
||||||
moves.to.rank = INVALID_RANK; moves.to.file = INVALID_FILE;
|
//moves.to.rank = 255; moves.to.file = 255; // mark as invalid???
|
||||||
}
|
}
|
||||||
else if(target->getPieceType() == KNIGHT && knight) {
|
if(target->getPieceType() == KNIGHT && knight) {
|
||||||
// we are being attacked by a knight, so remove it
|
// we are being attacked by a knight, so remove it
|
||||||
moves.to.rank = INVALID_RANK; moves.to.file = INVALID_FILE;
|
//moves.to.rank = 255; moves.to.file = 255;
|
||||||
}
|
}
|
||||||
else if(target->getPieceType() == ROOK || target->getPieceType() == QUEEN && (!diagonal && !knight)) {
|
if(target->getPieceType() == ROOK && (!diagonal && !knight)) {
|
||||||
// again, being attacked, remove it
|
// 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)) {
|
|
||||||
moves.to.rank = INVALID_RANK; moves.to.file = INVALID_FILE;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(target && (target->getColour() == this->getColour())) {
|
if(target && (target->getColour() == this->getColour())) {
|
||||||
|
|
@ -118,17 +115,13 @@ std::vector<Move> King::getLegalMoves(const Square &from, const Board &board) co
|
||||||
if(!knight) {
|
if(!knight) {
|
||||||
r += dir[0];
|
r += dir[0];
|
||||||
f += dir[1];
|
f += dir[1];
|
||||||
} else
|
} else {
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
// will this actually work?
|
|
||||||
moveList.erase(
|
|
||||||
std::remove_if(moveList.begin(), moveList.end(),
|
|
||||||
[&](const auto &x) {
|
|
||||||
return !x.to.isValid();
|
|
||||||
}));
|
|
||||||
return moveList;
|
return moveList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
22
Piece.hpp
22
Piece.hpp
|
|
@ -11,7 +11,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <algorithm>
|
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
@ -28,15 +27,11 @@ enum PieceColour {
|
||||||
};
|
};
|
||||||
|
|
||||||
enum Rank {
|
enum Rank {
|
||||||
R1 = 0, R2 = 1, R3 = 2, R4 = 3, R5 = 4, R6 = 5, R7 = 6, R8 = 7, INVALID_RANK = 255
|
R1 = 0, R2 = 1, R3 = 2, R4 = 3, R5 = 4, R6 = 5, R7 = 6, R8 = 7
|
||||||
};
|
};
|
||||||
|
|
||||||
enum File {
|
enum File {
|
||||||
A = 0, B = 1, C = 2, D = 3, E = 4, F = 5, G = 6, H = 7, INVALID_FILE = 255
|
A = 0, B = 1, C = 2, D = 3, E = 4, F = 5, G = 6, H = 7
|
||||||
};
|
|
||||||
|
|
||||||
enum CastleSide {
|
|
||||||
KINGSIDE = 1, QUEENSIDE = 2
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Square {
|
struct Square {
|
||||||
|
|
@ -106,18 +101,11 @@ class King : public Piece {
|
||||||
return inCheck;
|
return inCheck;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool checkForCastle(enum CastleSide side) const {
|
bool checkForCastle() const {
|
||||||
if(side == KINGSIDE) {
|
return canCastle;
|
||||||
return canCastleKS;
|
|
||||||
} else if(side == QUEENSIDE) {
|
|
||||||
return canCastleQS;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
protected:
|
protected:
|
||||||
bool canCastleQS = true;
|
bool canCastle = true;
|
||||||
bool canCastleKS = true;
|
|
||||||
bool inCheck = false;
|
bool inCheck = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,6 @@
|
||||||
<itemPath>Board.hpp</itemPath>
|
<itemPath>Board.hpp</itemPath>
|
||||||
<itemPath>Piece.hpp</itemPath>
|
<itemPath>Piece.hpp</itemPath>
|
||||||
<itemPath>NeoPixel.hpp</itemPath>
|
<itemPath>NeoPixel.hpp</itemPath>
|
||||||
<itemPath>inc/strFuncs.hpp</itemPath>
|
|
||||||
</logicalFolder>
|
</logicalFolder>
|
||||||
<logicalFolder name="ExternalFiles"
|
<logicalFolder name="ExternalFiles"
|
||||||
displayName="Important Files"
|
displayName="Important Files"
|
||||||
|
|
@ -39,7 +38,6 @@
|
||||||
<itemPath>Piece.cpp</itemPath>
|
<itemPath>Piece.cpp</itemPath>
|
||||||
<itemPath>NeoPixel.cpp</itemPath>
|
<itemPath>NeoPixel.cpp</itemPath>
|
||||||
<itemPath>graphics.cpp</itemPath>
|
<itemPath>graphics.cpp</itemPath>
|
||||||
<itemPath>strFuncs.cpp</itemPath>
|
|
||||||
</logicalFolder>
|
</logicalFolder>
|
||||||
</logicalFolder>
|
</logicalFolder>
|
||||||
<sourceRootList>
|
<sourceRootList>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue