diff options
| author | Peter B. <peter.bezdek@gmail.com> | 2026-07-25 18:09:20 +0200 |
|---|---|---|
| committer | Peter B. <peter.bezdek@gmail.com> | 2026-07-25 18:09:20 +0200 |
| commit | 7d0364996c3d8b34d4634a163a0829fafcd42fca (patch) | |
| tree | 71e32f066b732b2ed5e43e58ea3dc3996e87ff3a /src | |
| parent | 1c6c1ed8ede9edf1860b1fbd5f2898b408689a05 (diff) | |
refactor printBoard function for improved readability and formatting
Diffstat (limited to 'src')
| -rw-r--r-- | src/board.cpp | 27 |
1 files changed, 18 insertions, 9 deletions
diff --git a/src/board.cpp b/src/board.cpp index 92704c3..1d647a4 100644 --- a/src/board.cpp +++ b/src/board.cpp @@ -1,7 +1,6 @@ #include "board.hpp" #include <cctype> #include <cstdio> -#include <iostream> #include <print> #include <string> @@ -37,16 +36,26 @@ Piece createPiece(pieceType type, bool color, bool moved) { return piece; }; void printBoard(board *b) { - for (int i = 0; i < 64; i++) { - Piece currentPiece = b->pieces[i]; - std::printf("%c%d%d ", toChar(currentPiece.type), currentPiece.moved, - currentPiece.color); - if (i % 8 == 7 && i != 0) { - std::printf("\n"); + std::println(" a b c d e f g h"); + std::println(" +-----------------+"); + + 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 = piece.type == NONE ? '.' : toChar(piece.type); + if (piece.type != NONE && piece.color == BLACK) { + symbol = std::tolower(static_cast<unsigned char>(symbol)); + } + std::printf(" %c", symbol); } + std::printf(" | %d\n", 8 - rank); } - std::printf("%d to play\n", b->turn); - std::cout << "castle: " << b->castle << "\n"; + + std::println(" +-----------------+"); + std::println(" a b c d e f g h"); + std::println("\nTurn: {}", b->turn == WHITE ? "White" : "Black"); + std::println("Castling: {}", b->castle.empty() ? "-" : b->castle); } void setBoardFen(std::string fen, board *b) { // example fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1 |
