Compare commits
2 commits
21fae88bdb
...
6773da28fb
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6773da28fb |
||
|
|
e5a124d193 |
5 changed files with 119 additions and 13 deletions
57
Board.cpp
57
Board.cpp
|
|
@ -97,6 +97,7 @@ int Board::setupFromFEN(std::string strFEN) {
|
||||||
std::vector<std::string> splitField1 = split(splitFEN[0], '/');
|
std::vector<std::string> splitField1 = split(splitFEN[0], '/');
|
||||||
int rank = 0, file = 0;
|
int rank = 0, file = 0;
|
||||||
int skip = 0;
|
int skip = 0;
|
||||||
|
Square whiteKing, blackKing;
|
||||||
bool wKingPlaced = false, bKingPlaced = false;
|
bool wKingPlaced = false, bKingPlaced = false;
|
||||||
for(auto &ranks : splitField1) {
|
for(auto &ranks : splitField1) {
|
||||||
for(auto &sym : ranks) {
|
for(auto &sym : ranks) {
|
||||||
|
|
@ -104,34 +105,42 @@ int Board::setupFromFEN(std::string strFEN) {
|
||||||
case 'R':
|
case 'R':
|
||||||
// white rook
|
// white rook
|
||||||
boardGrid[rank][file] = std::make_unique<Rook>(PIECE_WHITE);
|
boardGrid[rank][file] = std::make_unique<Rook>(PIECE_WHITE);
|
||||||
|
file++;
|
||||||
break;
|
break;
|
||||||
case 'r':
|
case 'r':
|
||||||
// black rook
|
// black rook
|
||||||
boardGrid[rank][file] = std::make_unique<Rook>(PIECE_BLACK);
|
boardGrid[rank][file] = std::make_unique<Rook>(PIECE_BLACK);
|
||||||
|
file++;
|
||||||
break;
|
break;
|
||||||
case 'N':
|
case 'N':
|
||||||
// white knight
|
// white knight
|
||||||
boardGrid[rank][file] = std::make_unique<Knight>(PIECE_WHITE);
|
boardGrid[rank][file] = std::make_unique<Knight>(PIECE_WHITE);
|
||||||
|
file++;
|
||||||
break;
|
break;
|
||||||
case 'n':
|
case 'n':
|
||||||
// black knight
|
// black knight
|
||||||
boardGrid[rank][file] = std::make_unique<Knight>(PIECE_BLACK);
|
boardGrid[rank][file] = std::make_unique<Knight>(PIECE_BLACK);
|
||||||
|
file++;
|
||||||
break;
|
break;
|
||||||
case 'B':
|
case 'B':
|
||||||
// white bishop
|
// white bishop
|
||||||
boardGrid[rank][file] = std::make_unique<Bishop>(PIECE_WHITE);
|
boardGrid[rank][file] = std::make_unique<Bishop>(PIECE_WHITE);
|
||||||
|
file++;
|
||||||
break;
|
break;
|
||||||
case 'b':
|
case 'b':
|
||||||
// black bishop
|
// black bishop
|
||||||
boardGrid[rank][file] = std::make_unique<Bishop>(PIECE_BLACK);
|
boardGrid[rank][file] = std::make_unique<Bishop>(PIECE_BLACK);
|
||||||
|
file++;
|
||||||
break;
|
break;
|
||||||
case 'Q':
|
case 'Q':
|
||||||
// white queen
|
// white queen
|
||||||
boardGrid[rank][file] = std::make_unique<Queen>(PIECE_WHITE);
|
boardGrid[rank][file] = std::make_unique<Queen>(PIECE_WHITE);
|
||||||
|
file++;
|
||||||
break;
|
break;
|
||||||
case 'q':
|
case 'q':
|
||||||
// black queen;
|
// black queen;
|
||||||
boardGrid[rank][file] = std::make_unique<Queen>(PIECE_BLACK);
|
boardGrid[rank][file] = std::make_unique<Queen>(PIECE_BLACK);
|
||||||
|
file++;
|
||||||
break;
|
break;
|
||||||
case 'K':
|
case 'K':
|
||||||
// white king
|
// white king
|
||||||
|
|
@ -141,6 +150,10 @@ int Board::setupFromFEN(std::string strFEN) {
|
||||||
} else {
|
} else {
|
||||||
boardGrid[rank][file] = std::make_unique<King>(PIECE_WHITE);
|
boardGrid[rank][file] = std::make_unique<King>(PIECE_WHITE);
|
||||||
wKingPlaced = true;
|
wKingPlaced = true;
|
||||||
|
// store king position for later
|
||||||
|
whiteKing.file = file;
|
||||||
|
whiteKing.rank = rank;
|
||||||
|
file++;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'k':
|
case 'k':
|
||||||
|
|
@ -151,15 +164,21 @@ int Board::setupFromFEN(std::string strFEN) {
|
||||||
} else {
|
} else {
|
||||||
boardGrid[rank][file] = std::make_unique<King>(PIECE_BLACK);
|
boardGrid[rank][file] = std::make_unique<King>(PIECE_BLACK);
|
||||||
bKingPlaced = true;
|
bKingPlaced = true;
|
||||||
|
// store king position for later
|
||||||
|
blackKing.file = file;
|
||||||
|
blackKing.rank = rank;
|
||||||
|
file++;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'P':
|
case 'P':
|
||||||
// white pawn
|
// white pawn
|
||||||
boardGrid[rank][file] = std::make_unique<Pawn>(PIECE_WHITE);
|
boardGrid[rank][file] = std::make_unique<Pawn>(PIECE_WHITE);
|
||||||
|
file++;
|
||||||
break;
|
break;
|
||||||
case 'p':
|
case 'p':
|
||||||
// black pawn
|
// black pawn
|
||||||
boardGrid[rank][file] = std::make_unique<Pawn>(PIECE_BLACK);
|
boardGrid[rank][file] = std::make_unique<Pawn>(PIECE_BLACK);
|
||||||
|
file++;
|
||||||
break;
|
break;
|
||||||
case '1':
|
case '1':
|
||||||
case '2':
|
case '2':
|
||||||
|
|
@ -171,11 +190,10 @@ int Board::setupFromFEN(std::string strFEN) {
|
||||||
case '8': {
|
case '8': {
|
||||||
std::string skipStr(1, sym);
|
std::string skipStr(1, sym);
|
||||||
skip = atoi(skipStr.c_str());
|
skip = atoi(skipStr.c_str());
|
||||||
skip--; // fix off by 1 error
|
//skip--; // fix off by 1 error
|
||||||
do {
|
while(skip--) {
|
||||||
boardGrid[rank][file] = nullptr; // remove reference
|
boardGrid[rank][file++] = nullptr; // remove reference
|
||||||
file = (file < 8) ? file + 1 : 0;
|
}
|
||||||
} while(--skip);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
|
@ -183,7 +201,6 @@ int Board::setupFromFEN(std::string strFEN) {
|
||||||
return -1;
|
return -1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
file++;
|
|
||||||
if(file > 7) {
|
if(file > 7) {
|
||||||
file = 0;
|
file = 0;
|
||||||
break;
|
break;
|
||||||
|
|
@ -208,23 +225,47 @@ int Board::setupFromFEN(std::string strFEN) {
|
||||||
// ====== END OF FIELD 2 ======
|
// ====== END OF FIELD 2 ======
|
||||||
// ====== START OF FIELD 3 ======
|
// ====== START OF FIELD 3 ======
|
||||||
std::string k = "k", K = "K", q = "q", Q = "Q";
|
std::string k = "k", K = "K", q = "q", Q = "Q";
|
||||||
|
auto &bKing = this->getPieceAt(blackKing);
|
||||||
|
auto &wKing = this->getPieceAt(whiteKing);
|
||||||
if(splitFEN[2] == "-") {
|
if(splitFEN[2] == "-") {
|
||||||
// nobody can castle, either side
|
// nobody can castle, either side
|
||||||
// locate Kings and set appropriate variables
|
bKing->setCastleKS(false);
|
||||||
// canCastleKS, canCastleQS
|
bKing->setCastleQS(false);
|
||||||
|
wKing->setCastleKS(false);
|
||||||
|
wKing->setCastleQS(false);
|
||||||
}
|
}
|
||||||
if(splitFEN[2].find(k) != std::string::npos) {
|
if(splitFEN[2].find(k) != std::string::npos) {
|
||||||
// black king can castle kingside
|
// black king can castle kingside
|
||||||
|
bKing->setCastleKS(true);
|
||||||
}
|
}
|
||||||
if(splitFEN[2].find(K) != std::string::npos) {
|
if(splitFEN[2].find(K) != std::string::npos) {
|
||||||
// white king can castle kingside
|
// white king can castle kingside
|
||||||
|
wKing->setCastleKS(true);
|
||||||
}
|
}
|
||||||
if(splitFEN[2].find(q) != std::string::npos) {
|
if(splitFEN[2].find(q) != std::string::npos) {
|
||||||
// black king can castle queenside
|
// black king can castle queenside
|
||||||
|
bKing->setCastleQS(true);
|
||||||
}
|
}
|
||||||
if(splitFEN[2].find(Q) != std::string::npos) {
|
if(splitFEN[2].find(Q) != std::string::npos) {
|
||||||
// white king can castle queenside
|
// white king can castle queenside
|
||||||
|
wKing->setCastleQS(true);
|
||||||
}
|
}
|
||||||
|
// ====== END OF FIELD 3 ======
|
||||||
|
// ====== START OF FIELD 4 ======
|
||||||
|
// if a pawn has just moved and can be attacked enPassant, it will be listed here
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef DEBUG
|
||||||
|
for(auto &ranks : boardGrid) {
|
||||||
|
for(auto &files : ranks) {
|
||||||
|
if(files)
|
||||||
|
std::cout << files->getPieceName() << " ";
|
||||||
|
else
|
||||||
|
std::cout << "E ";
|
||||||
|
}
|
||||||
|
std::cout << "\n";
|
||||||
|
}
|
||||||
|
#endif
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
53
Piece.cpp
53
Piece.cpp
|
|
@ -12,6 +12,9 @@ Piece::~Piece() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Add operator overload for << to allow easy printing
|
||||||
|
// TODO: Test getPieceAt() function to see if it works as intended
|
||||||
|
|
||||||
// because the Piece class is instantiated in another class using
|
// because the Piece class is instantiated in another class using
|
||||||
// unique_ptr, non-pure virtual functions apparently *must*
|
// unique_ptr, non-pure virtual functions apparently *must*
|
||||||
// have definitions, or the linker freaks out
|
// have definitions, or the linker freaks out
|
||||||
|
|
@ -164,7 +167,7 @@ std::vector<Move> Rook::getLegalMoves(const Square &from, Board &board) const {
|
||||||
{0, -1}, // Left
|
{0, -1}, // Left
|
||||||
{0, 1} // Right
|
{0, 1} // Right
|
||||||
};
|
};
|
||||||
for(auto& dir : directions) {
|
for(auto &dir : directions) {
|
||||||
int r = from.rank + dir[0];
|
int r = from.rank + dir[0];
|
||||||
int f = from.file + dir[1];
|
int f = from.file + dir[1];
|
||||||
while(r >= 0 && r < 8 && f >= 0 && f < 8) {
|
while(r >= 0 && r < 8 && f >= 0 && f < 8) {
|
||||||
|
|
@ -188,16 +191,64 @@ std::vector<Move> Rook::getLegalMoves(const Square &from, Board &board) const {
|
||||||
|
|
||||||
std::vector<Move> Queen::getLegalMoves(const Square &from, Board &board) const {
|
std::vector<Move> Queen::getLegalMoves(const Square &from, Board &board) const {
|
||||||
std::vector<Move> moveList;
|
std::vector<Move> moveList;
|
||||||
|
std::vector<std::vector<int>> directions = {
|
||||||
|
{-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
|
||||||
|
};
|
||||||
|
for(auto &dir : directions) {
|
||||||
|
// establish r/f for square to check
|
||||||
|
int r = from.rank + dir[0];
|
||||||
|
int f = from.file + dir[1];
|
||||||
|
auto ra = static_cast<Rank>(r);
|
||||||
|
auto fi = static_cast<File>(f);
|
||||||
|
Square targetSquare = {ra, fi};
|
||||||
|
while(r >= 0 && r < 8 && f >= 0 && f < 8) {
|
||||||
|
const auto &target = board.boardGrid[r][f]; // 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
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
r += dir[0];
|
||||||
|
f += dir[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
return moveList;
|
return moveList;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<Move> Knight::getLegalMoves(const Square &from, Board &board) const {
|
std::vector<Move> Knight::getLegalMoves(const Square &from, Board &board) const {
|
||||||
std::vector<Move> moveList;
|
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}
|
||||||
|
};
|
||||||
return moveList;
|
return moveList;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<Move> Bishop::getLegalMoves(const Square &from, Board &board) const {
|
std::vector<Move> Bishop::getLegalMoves(const Square &from, Board &board) const {
|
||||||
std::vector<Move> moveList;
|
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
|
||||||
|
};
|
||||||
return moveList;
|
return moveList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
#include "Piece.hpp"
|
#include "Piece.hpp"
|
||||||
|
|
||||||
|
|
@ -32,6 +33,7 @@ public:
|
||||||
std::vector<std::vector<std::unique_ptr<Piece>>> boardGrid;
|
std::vector<std::vector<std::unique_ptr<Piece>>> boardGrid;
|
||||||
Board();
|
Board();
|
||||||
virtual ~Board();
|
virtual ~Board();
|
||||||
|
|
||||||
void setupInitialPosition();
|
void setupInitialPosition();
|
||||||
std::unique_ptr<Piece> &getPieceAt(Square square);
|
std::unique_ptr<Piece> &getPieceAt(Square square);
|
||||||
void movePiece(Square from, Square to);
|
void movePiece(Square from, Square to);
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@
|
||||||
|
|
||||||
#ifndef PIECE_HPP
|
#ifndef PIECE_HPP
|
||||||
#define PIECE_HPP
|
#define PIECE_HPP
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
@ -115,6 +114,13 @@ class King : public Piece {
|
||||||
else
|
else
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setCastleQS(bool canCastle) {
|
||||||
|
canCastleQS = canCastle;
|
||||||
|
}
|
||||||
|
void setCastleKS(bool canCastle) {
|
||||||
|
canCastleKS = canCastle;
|
||||||
|
}
|
||||||
protected:
|
protected:
|
||||||
bool canCastleQS = true;
|
bool canCastleQS = true;
|
||||||
bool canCastleKS = true;
|
bool canCastleKS = true;
|
||||||
|
|
@ -179,6 +185,10 @@ class Pawn : public Piece {
|
||||||
pieceType = PAWN;
|
pieceType = PAWN;
|
||||||
}
|
}
|
||||||
virtual std::vector<Move> getLegalMoves(const Square &from, Board &board) const override;
|
virtual std::vector<Move> getLegalMoves(const Square &from, Board &board) const override;
|
||||||
|
void promote(const Square &promotionSquare, Board &board, PieceType promoteTo);
|
||||||
|
protected:
|
||||||
|
bool vulnEnPassant = false;
|
||||||
|
bool firstMove = true;
|
||||||
};
|
};
|
||||||
#endif // PIECE_HPP
|
#endif // PIECE_HPP
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,10 +4,10 @@
|
||||||
<logicalFolder name="HeaderFiles"
|
<logicalFolder name="HeaderFiles"
|
||||||
displayName="Header Files"
|
displayName="Header Files"
|
||||||
projectFiles="true">
|
projectFiles="true">
|
||||||
<itemPath>Board.hpp</itemPath>
|
|
||||||
<itemPath>Piece.hpp</itemPath>
|
|
||||||
<itemPath>NeoPixel.hpp</itemPath>
|
|
||||||
<itemPath>inc/strFuncs.hpp</itemPath>
|
<itemPath>inc/strFuncs.hpp</itemPath>
|
||||||
|
<itemPath>inc/Board.hpp</itemPath>
|
||||||
|
<itemPath>inc/NeoPixel.hpp</itemPath>
|
||||||
|
<itemPath>inc/Piece.hpp</itemPath>
|
||||||
</logicalFolder>
|
</logicalFolder>
|
||||||
<logicalFolder name="ExternalFiles"
|
<logicalFolder name="ExternalFiles"
|
||||||
displayName="Important Files"
|
displayName="Important Files"
|
||||||
|
|
@ -408,6 +408,8 @@
|
||||||
value="${memories.instruction.ram.ranges}"/>
|
value="${memories.instruction.ram.ranges}"/>
|
||||||
<property key="memories.programmemory" value="true"/>
|
<property key="memories.programmemory" value="true"/>
|
||||||
<property key="memories.programmemory.ranges" value="1d000000-1d03ffff"/>
|
<property key="memories.programmemory.ranges" value="1d000000-1d03ffff"/>
|
||||||
|
<property key="programmerToGoFilePath"
|
||||||
|
value="/home/amr/MPLABXProjects/chessmcu.X/debug/Debug/chessmcu_ptg"/>
|
||||||
<property key="programoptions.donoteraseauxmem" value="false"/>
|
<property key="programoptions.donoteraseauxmem" value="false"/>
|
||||||
<property key="programoptions.eraseb4program" value="true"/>
|
<property key="programoptions.eraseb4program" value="true"/>
|
||||||
<property key="programoptions.pgmentry.voltage" value="low"/>
|
<property key="programoptions.pgmentry.voltage" value="low"/>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue