astyle: reformatted all files
This commit is contained in:
parent
04c7a931d3
commit
7722a84b2e
5 changed files with 120 additions and 99 deletions
152
Board.cpp
152
Board.cpp
|
|
@ -31,9 +31,8 @@
|
||||||
|
|
||||||
Board::Board() {
|
Board::Board() {
|
||||||
boardGrid.resize(8);
|
boardGrid.resize(8);
|
||||||
for(int i = 0; i < 8; i++) {
|
for(int i = 0; i < 8; i++)
|
||||||
boardGrid[i].resize(8);
|
boardGrid[i].resize(8);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Board::~Board() {
|
Board::~Board() {
|
||||||
|
|
@ -92,9 +91,8 @@ void Board::setupInitialPosition() {
|
||||||
|
|
||||||
int setupFromFEN(std::string strFEN) {
|
int setupFromFEN(std::string strFEN) {
|
||||||
std::vector<std::string> splitFEN = split(strFEN, ' ');
|
std::vector<std::string> splitFEN = split(strFEN, ' ');
|
||||||
if(splitFEN.size() != 6) { // a valid FEN *must* contain 6 fields
|
if(splitFEN.size() != 6) // a valid FEN *must* contain 6 fields
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
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;
|
||||||
|
|
@ -108,80 +106,80 @@ int setupFromFEN(std::string strFEN) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
switch(sym) {
|
switch(sym) {
|
||||||
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);
|
||||||
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);
|
||||||
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);
|
||||||
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);
|
||||||
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);
|
||||||
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);
|
||||||
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);
|
||||||
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);
|
||||||
break;
|
break;
|
||||||
case 'K':
|
case 'K':
|
||||||
// white king
|
// white king
|
||||||
if(wKingPlaced) {
|
if(wKingPlaced) {
|
||||||
// invalid FEN, more than 1 white king
|
// invalid FEN, more than 1 white king
|
||||||
|
return -1;
|
||||||
|
} else {
|
||||||
|
boardGrid[rank][file] = std::make_unique<King>(PIECE_WHITE);
|
||||||
|
wKingPlaced = true;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'k':
|
||||||
|
// black king
|
||||||
|
if(bKingPlaced) {
|
||||||
|
// invalid FEN, more than 1 black king
|
||||||
|
return -1;
|
||||||
|
} else {
|
||||||
|
boardGrid[rank][file] = std::make_unique<King>(PIECE_BLACK);
|
||||||
|
bKingPlaced = true;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 'P':
|
||||||
|
// white pawn
|
||||||
|
boardGrid[rank][file] = std::make_unique<Pawn>(PIECE_WHITE);
|
||||||
|
break;
|
||||||
|
case 'p':
|
||||||
|
// black pawn
|
||||||
|
boardGrid[rank][file] = std::make_unique<Pawn>(PIECE_BLACK);
|
||||||
|
break;
|
||||||
|
case '1':
|
||||||
|
case '2':
|
||||||
|
case '3':
|
||||||
|
case '4':
|
||||||
|
case '5':
|
||||||
|
case '6':
|
||||||
|
case '7':
|
||||||
|
case '8':
|
||||||
|
case '9':
|
||||||
|
skip = atoi(sym);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
// invalid character?
|
||||||
return -1;
|
return -1;
|
||||||
} else {
|
|
||||||
boardGrid[rank][file] = std::make_unique<King>(PIECE_WHITE);
|
|
||||||
wKingPlaced = true;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'k':
|
|
||||||
// black king
|
|
||||||
if(bKingPlaced) {
|
|
||||||
// invalid FEN, more than 1 black king
|
|
||||||
return -1;
|
|
||||||
} else {
|
|
||||||
boardGrid[rank][file] = std::make_unique<King>(PIECE_BLACK);
|
|
||||||
bKingPlaced = true;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'P':
|
|
||||||
// white pawn
|
|
||||||
boardGrid[rank][file] = std::make_unique<Pawn>(PIECE_WHITE);
|
|
||||||
break;
|
|
||||||
case 'p':
|
|
||||||
// black pawn
|
|
||||||
boardGrid[rank][file] = std::make_unique<Pawn>(PIECE_BLACK);
|
|
||||||
break;
|
|
||||||
case '1':
|
|
||||||
case '2':
|
|
||||||
case '3':
|
|
||||||
case '4':
|
|
||||||
case '5':
|
|
||||||
case '6':
|
|
||||||
case '7':
|
|
||||||
case '8':
|
|
||||||
case '9':
|
|
||||||
skip = atoi(sym);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
// invalid character?
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
file++;
|
file++;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
48
Piece.cpp
48
Piece.cpp
|
|
@ -22,6 +22,28 @@ std::vector<Move> Piece::getLegalMoves(const Square &from, const Board &board) c
|
||||||
std::vector<Move> moveList;
|
std::vector<Move> moveList;
|
||||||
return moveList;
|
return moveList;
|
||||||
}
|
}
|
||||||
|
bool King::checkForCheck() const {
|
||||||
|
std::vector<std::vector<int>> kingVulnerable = {
|
||||||
|
{-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
|
||||||
|
{-1, -2}, // 1
|
||||||
|
{-1, 2}, // 2
|
||||||
|
{-2, -1}, // 3
|
||||||
|
{-2, 1}, // 4
|
||||||
|
{1, -2}, // 5
|
||||||
|
{1, 2}, // 6
|
||||||
|
{2, -1}, // 7
|
||||||
|
{2, 1} // 8
|
||||||
|
};
|
||||||
|
// locate our King
|
||||||
|
return inCheck;
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<Move> King::getLegalMoves(const Square &from, const Board &board) const {
|
std::vector<Move> King::getLegalMoves(const Square &from, const Board &board) const {
|
||||||
std::vector<Move> moveList;
|
std::vector<Move> moveList;
|
||||||
|
|
@ -97,18 +119,20 @@ 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 = INVALID_RANK;
|
||||||
}
|
moves.to.file = INVALID_FILE;
|
||||||
else if(target->getPieceType() == KNIGHT && knight) {
|
} else 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 = INVALID_RANK;
|
||||||
}
|
moves.to.file = INVALID_FILE;
|
||||||
else if(target->getPieceType() == ROOK || target->getPieceType() == QUEEN && (!diagonal && !knight)) {
|
} else if(target->getPieceType() == ROOK || target->getPieceType() == QUEEN && (!diagonal && !knight)) {
|
||||||
// again, being attacked, remove it
|
// again, being attacked, remove it
|
||||||
moves.to.rank = INVALID_RANK; moves.to.file = INVALID_FILE;
|
moves.to.rank = INVALID_RANK;
|
||||||
|
moves.to.file = INVALID_FILE;
|
||||||
}
|
}
|
||||||
if(target->getPieceType() == PAWN && (abs(r - startSquare.rank) == 1 && abs(f - startSquare.file) == 1)) {
|
if(target->getPieceType() == PAWN && (abs(r - startSquare.rank) == 1 && abs(f - startSquare.file) == 1)) {
|
||||||
moves.to.rank = INVALID_RANK; moves.to.file = INVALID_FILE;
|
moves.to.rank = INVALID_RANK;
|
||||||
|
moves.to.file = INVALID_FILE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(target && (target->getColour() == this->getColour())) {
|
if(target && (target->getColour() == this->getColour())) {
|
||||||
|
|
@ -125,10 +149,10 @@ std::vector<Move> King::getLegalMoves(const Square &from, const Board &board) co
|
||||||
}
|
}
|
||||||
// will this actually work?
|
// will this actually work?
|
||||||
moveList.erase(
|
moveList.erase(
|
||||||
std::remove_if(moveList.begin(), moveList.end(),
|
std::remove_if(moveList.begin(), moveList.end(),
|
||||||
[&](const auto &x) {
|
[&](const auto & x) {
|
||||||
return !x.to.isValid();
|
return !x.to.isValid();
|
||||||
}));
|
}));
|
||||||
return moveList;
|
return moveList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
14
Piece.hpp
14
Piece.hpp
|
|
@ -92,6 +92,7 @@ class Piece {
|
||||||
};
|
};
|
||||||
|
|
||||||
class King : public Piece {
|
class King : public Piece {
|
||||||
|
private:
|
||||||
friend class Board;
|
friend class Board;
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
|
@ -102,18 +103,17 @@ class King : public Piece {
|
||||||
}
|
}
|
||||||
virtual std::vector<Move> getLegalMoves(const Square &from, const Board &board) const override;
|
virtual std::vector<Move> getLegalMoves(const Square &from, const Board &board) const override;
|
||||||
|
|
||||||
bool checkForCheck() const {
|
bool checkForCheck() const;
|
||||||
return inCheck;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool checkForCastle(enum CastleSide side) const {
|
bool checkForCastle(enum CastleSide side) const {
|
||||||
if(side == KINGSIDE) {
|
if(side == KINGSIDE)
|
||||||
return canCastleKS;
|
return canCastleKS;
|
||||||
} else if(side == QUEENSIDE) {
|
|
||||||
|
else if(side == QUEENSIDE)
|
||||||
return canCastleQS;
|
return canCastleQS;
|
||||||
} else {
|
|
||||||
|
else
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
protected:
|
protected:
|
||||||
bool canCastleQS = true;
|
bool canCastleQS = true;
|
||||||
|
|
|
||||||
2
main.cpp
2
main.cpp
|
|
@ -114,7 +114,7 @@ uint8_t initSystem(void) {
|
||||||
SDI1R = 0b0100; // RB8
|
SDI1R = 0b0100; // RB8
|
||||||
RPA0R = 0b0001; // U1TX
|
RPA0R = 0b0001; // U1TX
|
||||||
SYSKEY = 0x12345678; // lock SYSKEY
|
SYSKEY = 0x12345678; // lock SYSKEY
|
||||||
ANSELBCLR = 0xFFFF; // port A all digital
|
ANSELBCLR = 0xFFFF; // port B all digital
|
||||||
SL_TRIS = 0; // RA0 as output
|
SL_TRIS = 0; // RA0 as output
|
||||||
SL_LAT = 1; // set high
|
SL_LAT = 1; // set high
|
||||||
/* Set up SPI1 */
|
/* Set up SPI1 */
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,8 @@ template <typename Out>
|
||||||
void split(const std::string &s, char delim, Out result) {
|
void split(const std::string &s, char delim, Out result) {
|
||||||
std::istringstream iss(s);
|
std::istringstream iss(s);
|
||||||
std::string item;
|
std::string item;
|
||||||
while (std::getline(iss, item, delim)) {
|
while(std::getline(iss, item, delim))
|
||||||
*result++ = item;
|
*result++ = item;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> split(const std::string &s, char delim) {
|
std::vector<std::string> split(const std::string &s, char delim) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue