diff options
| author | Peter B. <peter.bezdek@gmail.com> | 2026-07-26 16:37:26 +0200 |
|---|---|---|
| committer | Peter B. <peter.bezdek@gmail.com> | 2026-07-26 16:37:26 +0200 |
| commit | 7f60a36b744ba2baeee98f490556082f00de3dc9 (patch) | |
| tree | 02854089104a0d4aed01de197d1dcbd5932b0d43 /src/board.cpp | |
| parent | 63039394e4f8ef056289805bcee6ad1e7519de0a (diff) | |
| parent | a46558e0aab30eb88ac3cafac8d490174bd4785f (diff) | |
Merge branch 'main' of https://github.com/AdamMegaRules/chess
Diffstat (limited to 'src/board.cpp')
| -rw-r--r-- | src/board.cpp | 189 |
1 files changed, 43 insertions, 146 deletions
diff --git a/src/board.cpp b/src/board.cpp index 5c30d75..172a9be 100644 --- a/src/board.cpp +++ b/src/board.cpp @@ -1,14 +1,12 @@ #include "board.hpp" +#include <cassert> #include <cctype> #include <cstdio> #include <iostream> #include <print> #include <string> -bool WHITE = true; -bool BLACK = false; - -const char toChar(pieceType type) { +char toChar(pieceType type) { switch (type) { case NONE: return '.'; @@ -25,6 +23,8 @@ const char toChar(pieceType type) { case KING: return 'K'; default: + std::cout << "\n" << type << "\n"; + assert(false && "Unknown piece" && type); return '?'; } } @@ -38,158 +38,55 @@ Piece createPiece(pieceType type, bool color, bool moved) { }; void printBoard(board *b) { std::string line = " +-----------------+\n"; - std::string letter = " a b c d e f g h\n"; + std::string whiteLetters = " a b c d e f g h\n"; + std::string blackLetters = " h g f e d c b a\n"; - std::cout << letter; + std::cout << (b->turn ? whiteLetters : blackLetters); std::cout << line; - for (int rank = 0; rank < 8; rank++) { - std::printf("%d |", 8 - rank); - for (int file = 0; file < 8; file++) { - Piece piece = b->pieces[rank * 8 + file]; - char symbol = toChar(piece.type); - if (piece.color == BLACK) { - symbol = std::tolower(static_cast<unsigned char>(symbol)); + if (b->turn) { + for (int rank = 0; rank < 8; rank++) { + std::printf("%d |", 8 - rank); + + for (int file = 0; file < 8; file++) { + Piece piece = b->pieces[rank * 8 + file]; + char symbol = toChar(piece.type); + + if (!piece.color) + symbol = std::tolower(static_cast<unsigned char>(symbol)); + + std::printf(" %c", symbol); } - std::printf(" %c", symbol); + + std::printf(" | %d\n", 8 - rank); + } + } else { + for (int rank = 7; rank >= 0; rank--) { + std::printf("%d |", 8 - rank); + + for (int file = 7; file >= 0; file--) { + Piece piece = b->pieces[rank * 8 + file]; + char symbol = toChar(piece.type); + + if (!piece.color) + symbol = std::tolower(static_cast<unsigned char>(symbol)); + + std::printf(" %c", symbol); + } + + std::printf(" | %d\n", 8 - rank); } - std::printf(" | %d\n", 8 - rank); } std::cout << line; - std::cout << letter; + std::cout << (b->turn ? whiteLetters : blackLetters); std::println(); - std::println("Turn: {}", b->turn == WHITE ? "White" : "Black"); + std::println("Turn: {}", b->turn ? "White" : "Black"); + + assert(b->castle != ""); + std::println("Castling: {}", b->castle); } -void setBoardFen(std::string fen, board *b) { - // example fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1 - int file = 0; - int rank = 0; - - std::string castle = ""; - - enum parserState { - POSITION, - TURN, - CASTLE, - ENPASSANT, - CLOCK1, - CLOCK2, - }; - parserState state = POSITION; - - for (int i = 0; i < fen.length(); i++) { - std::printf("state: %d\n", state); - std::printf("doing: %c\n", fen[i]); - if (fen[i] == ' ') { - if (state == POSITION) { - state = TURN; - continue; - } - if (state == TURN) { - state = CASTLE; - continue; - } - if (state == CASTLE) { - state = ENPASSANT; - continue; - } - if (state == ENPASSANT) { - state = CLOCK1; - continue; - } - if (state == CLOCK1) { - state = CLOCK2; - continue; - } - } - if (state == ENPASSANT) { - continue; - } - if (state == CASTLE) { - if (fen[i] == '-') { - castle = "-"; - continue; - } - if (toupper(fen[i]) == 'K') { - castle += fen[i]; - continue; - } - if (toupper(fen[i]) == 'Q') { - castle += fen[i]; - continue; - } - } - if (state == TURN) { - if (toupper(fen[i]) == 'W') { - b->turn = WHITE; - continue; - } - if (toupper(fen[i]) == 'B') { - b->turn = BLACK; - continue; - } - } - if (state == POSITION) { - if (isdigit(fen[i])) { - int move = fen[i] - '0'; - file += move; - if (file > 8) { - std::println("Fatal: file was to big"); - return; // malformed rank - } - continue; - } - if (fen[i] == '/') { - file = 0; - rank++; - if (rank > 8) { - std::println("Fatal: rank was to big"); - return; // malformed rank - } - continue; - } - if (toupper(fen[i]) == 'N') { - Piece piece = createPiece(KNIGHT, isupper(fen[i])); - b->pieces[rank * 8 + file] = piece; - file++; - continue; - } - if (toupper(fen[i]) == 'R') { - Piece piece = createPiece(ROOK, isupper(fen[i])); - b->pieces[rank * 8 + file] = piece; - file++; - continue; - } - if (toupper(fen[i]) == 'Q') { - Piece piece = createPiece(QUEEN, isupper(fen[i])); - b->pieces[rank * 8 + file] = piece; - file++; - continue; - } - if (toupper(fen[i]) == 'B') { - Piece piece = createPiece(BISHOP, isupper(fen[i])); - b->pieces[rank * 8 + file] = piece; - file++; - continue; - } - if (toupper(fen[i]) == 'P') { - Piece piece = createPiece(PAWN, isupper(fen[i])); - b->pieces[rank * 8 + file] = piece; - file++; - continue; - } - if (toupper(fen[i]) == 'K') { - Piece piece = createPiece(KING, isupper(fen[i])); - b->pieces[rank * 8 + file] = piece; - file++; - continue; - } - } - break; - } - b->castle = castle.empty() ? "-" : castle; -} +// void PlayMove(Move move, board *b) { return; } |
