fen: Merge branch 'fen' into dev

This commit is contained in:
A.M. Rowsell 2025-08-28 10:39:50 -04:00
commit 04c7a931d3
Signed by untrusted user who does not match committer: amr
GPG key ID: E0879EDBDB0CA7B1

123
Board.cpp
View file

@ -30,12 +30,20 @@
*/
Board::Board() {
// set up the board grid with smart pointers
// initialize each square with a Piece
playerTurn = PL_WHITE;
boardGrid.resize(8);
for(int i = 0; i < 8; i++) {
boardGrid[i].resize(8);
}
}
Board::~Board() {
}
void Board::setupInitialPosition() {
// set up the board grid with smart pointers
// initialize each square with a Piece
playerTurn = PL_WHITE;
for(int i = 0; i < 8; i++) {
switch(i) {
case 0:
// white back rank
@ -57,7 +65,9 @@ Board::Board() {
case 3:
case 4:
case 5:
continue;
for(int j = 0; j <= 8; j++)
boardGrid[i][j] = nullptr;
break;
case 6:
// black pawn rank
for(int j = 0; j <= 8; j++)
@ -77,27 +87,106 @@ Board::Board() {
break;
}
}
}
Board::~Board() {
}
void Board::setupInitialPosition() {
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];
std::vector<std::string> splitField1 = split(splitFEN[0], '/');
int rank = 0, file = 0;
int skip = 0;
bool wKingPlaced = false, bKingPlaced = false;
for(auto &ranks : splitField1) {
for(auto &sym : ranks) {
if(skip) {
boardGrid[rank][file] = nullptr; // remove reference
skip--;
file++;
continue;
}
switch(sym) {
case 'R':
// white rook
boardGrid[rank][file] = std::make_unique<Rook>(PIECE_WHITE);
break;
case 'r':
// black rook
boardGrid[rank][file] = std::make_unique<Rook>(PIECE_BLACK);
break;
case 'N':
// white knight
boardGrid[rank][file] = std::make_unique<Knight>(PIECE_WHITE);
break;
case 'n':
// black knight
boardGrid[rank][file] = std::make_unique<Knight>(PIECE_BLACK);
break;
case 'B':
// white bishop
boardGrid[rank][file] = std::make_unique<Bishop>(PIECE_WHITE);
break;
case 'b':
// black bishop
boardGrid[rank][file] = std::make_unique<Bishop>(PIECE_BLACK);
break;
case 'Q':
// white queen
boardGrid[rank][file] = std::make_unique<Queen>(PIECE_WHITE);
break;
case 'q':
// black queen;
boardGrid[rank][file] = std::make_unique<Queen>(PIECE_BLACK);
break;
case 'K':
// white king
if(wKingPlaced) {
// 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;
}
file++;
}
rank++;
}
return 0;
}