FEN: major portion of FEN import is working and tested

This commit is contained in:
A.M. Rowsell 2025-08-30 18:45:04 -04:00
commit e5a124d193
Signed by untrusted user who does not match committer: amr
GPG key ID: E0879EDBDB0CA7B1

View file

@ -97,6 +97,7 @@ int Board::setupFromFEN(std::string strFEN) {
std::vector<std::string> splitField1 = split(splitFEN[0], '/');
int rank = 0, file = 0;
int skip = 0;
Square whiteKing, blackKing;
bool wKingPlaced = false, bKingPlaced = false;
for(auto &ranks : splitField1) {
for(auto &sym : ranks) {
@ -104,34 +105,42 @@ int Board::setupFromFEN(std::string strFEN) {
case 'R':
// white rook
boardGrid[rank][file] = std::make_unique<Rook>(PIECE_WHITE);
file++;
break;
case 'r':
// black rook
boardGrid[rank][file] = std::make_unique<Rook>(PIECE_BLACK);
file++;
break;
case 'N':
// white knight
boardGrid[rank][file] = std::make_unique<Knight>(PIECE_WHITE);
file++;
break;
case 'n':
// black knight
boardGrid[rank][file] = std::make_unique<Knight>(PIECE_BLACK);
file++;
break;
case 'B':
// white bishop
boardGrid[rank][file] = std::make_unique<Bishop>(PIECE_WHITE);
file++;
break;
case 'b':
// black bishop
boardGrid[rank][file] = std::make_unique<Bishop>(PIECE_BLACK);
file++;
break;
case 'Q':
// white queen
boardGrid[rank][file] = std::make_unique<Queen>(PIECE_WHITE);
file++;
break;
case 'q':
// black queen;
boardGrid[rank][file] = std::make_unique<Queen>(PIECE_BLACK);
file++;
break;
case 'K':
// white king
@ -141,6 +150,10 @@ int Board::setupFromFEN(std::string strFEN) {
} else {
boardGrid[rank][file] = std::make_unique<King>(PIECE_WHITE);
wKingPlaced = true;
// store king position for later
whiteKing.file = file;
whiteKing.rank = rank;
file++;
}
break;
case 'k':
@ -151,15 +164,21 @@ int Board::setupFromFEN(std::string strFEN) {
} else {
boardGrid[rank][file] = std::make_unique<King>(PIECE_BLACK);
bKingPlaced = true;
// store king position for later
blackKing.file = file;
blackKing.rank = rank;
file++;
}
break;
case 'P':
// white pawn
boardGrid[rank][file] = std::make_unique<Pawn>(PIECE_WHITE);
file++;
break;
case 'p':
// black pawn
boardGrid[rank][file] = std::make_unique<Pawn>(PIECE_BLACK);
file++;
break;
case '1':
case '2':
@ -171,11 +190,10 @@ int Board::setupFromFEN(std::string strFEN) {
case '8': {
std::string skipStr(1, sym);
skip = atoi(skipStr.c_str());
skip--; // fix off by 1 error
do {
boardGrid[rank][file] = nullptr; // remove reference
file = (file < 8) ? file + 1 : 0;
} while(--skip);
//skip--; // fix off by 1 error
while(skip--) {
boardGrid[rank][file++] = nullptr; // remove reference
}
break;
}
default:
@ -183,7 +201,6 @@ int Board::setupFromFEN(std::string strFEN) {
return -1;
break;
}
file++;
if(file > 7) {
file = 0;
break;
@ -208,23 +225,47 @@ int Board::setupFromFEN(std::string strFEN) {
// ====== END OF FIELD 2 ======
// ====== START OF FIELD 3 ======
std::string k = "k", K = "K", q = "q", Q = "Q";
auto &bKing = this->getPieceAt(blackKing);
auto &wKing = this->getPieceAt(whiteKing);
if(splitFEN[2] == "-") {
// nobody can castle, either side
// locate Kings and set appropriate variables
// canCastleKS, canCastleQS
bKing->setCastleKS(false);
bKing->setCastleQS(false);
wKing->setCastleKS(false);
wKing->setCastleQS(false);
}
if(splitFEN[2].find(k) != std::string::npos) {
// black king can castle kingside
bKing->setCastleKS(true);
}
if(splitFEN[2].find(K) != std::string::npos) {
// white king can castle kingside
wKing->setCastleKS(true);
}
if(splitFEN[2].find(q) != std::string::npos) {
// black king can castle queenside
bKing->setCastleQS(true);
}
if(splitFEN[2].find(Q) != std::string::npos) {
// 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;
}