From b553823dde69405f56ecfbedbe67db8101adac32 Mon Sep 17 00:00:00 2001 From: "A.M. Rowsell" Date: Thu, 28 Aug 2025 10:28:11 -0400 Subject: [PATCH] FEN: implemented first field of FEN string handling --- Board.cpp | 123 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 106 insertions(+), 17 deletions(-) diff --git a/Board.cpp b/Board.cpp index 77cbd8e..32590ad 100644 --- a/Board.cpp +++ b/Board.cpp @@ -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 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 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(PIECE_WHITE); + break; + case 'r': + // black rook + boardGrid[rank][file] = std::make_unique(PIECE_BLACK); + break; + case 'N': + // white knight + boardGrid[rank][file] = std::make_unique(PIECE_WHITE); + break; + case 'n': + // black knight + boardGrid[rank][file] = std::make_unique(PIECE_BLACK); + break; + case 'B': + // white bishop + boardGrid[rank][file] = std::make_unique(PIECE_WHITE); + break; + case 'b': + // black bishop + boardGrid[rank][file] = std::make_unique(PIECE_BLACK); + break; + case 'Q': + // white queen + boardGrid[rank][file] = std::make_unique(PIECE_WHITE); + break; + case 'q': + // black queen; + boardGrid[rank][file] = std::make_unique(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(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(PIECE_BLACK); + bKingPlaced = true; + } + break; + case 'P': + // white pawn + boardGrid[rank][file] = std::make_unique(PIECE_WHITE); + break; + case 'p': + // black pawn + boardGrid[rank][file] = std::make_unique(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; }