From 178190a723730485c0450552bfdef4ec7c8fac38 Mon Sep 17 00:00:00 2001 From: Adam Date: Mon, 27 Jul 2026 16:13:32 +0200 Subject: making board nicer --- src/board.cpp | 62 +++++++++++++++++++++++++++++++---------------------------- src/board.hpp | 2 +- 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/src/board.cpp b/src/board.cpp index 0d884a6..d7fe87b 100644 --- a/src/board.cpp +++ b/src/board.cpp @@ -1,32 +1,30 @@ #include "board.hpp" #include "bot.hpp" #include -#include #include #include #include #include -char toChar(PieceType type) { +std::string toChar(PieceType type, bool white) { switch (type) { case NONE: - return '.'; - case PAWN: - return 'P'; - case KNIGHT: - return 'N'; - case BISHOP: - return 'B'; - case ROOK: - return 'R'; - case QUEEN: - return 'Q'; + return " "; case KING: - return 'K'; + return white ? "♚" : "♔"; + case QUEEN: + return white ? "♛" : "♕"; + case ROOK: + return white ? "♜" : "♖"; + case BISHOP: + return white ? "♝" : "♗"; + case KNIGHT: + return white ? "♞" : "♘"; + case PAWN: + return white ? "♟" : "♙"; default: - std::cout << "\n" << type << "\n"; - assert(false && "Unknown piece" && type); - return '?'; + assert(false && "Unknown piece"); + return " "; } } @@ -38,9 +36,17 @@ Piece createPiece(PieceType type, bool color, bool moved) { return piece; } void printBoard(Board *b) { - std::string line = " +-----------------+\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::string line = " +------------------------+\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"; + + // const char *lightSquare = "\033[48;5;145m"; + // // const char *darkSquare = "\033[48;5;28m"; + // const char *lightSquare = "\033[48;5;240m"; // Dark gray + // const char *darkSquare = "\033[48;5;22m"; // Forest green + const char *lightSquare = "\033[48;5;245m"; // Medium gray + const char *darkSquare = "\033[48;5;29m"; // Rich green + const char *reset = "\033[0m"; std::cout << (b->turn ? whiteLetters : blackLetters); std::cout << line; @@ -51,15 +57,14 @@ void printBoard(Board *b) { for (int file = 0; file < 8; file++) { Piece piece = b->pieces[rank * 8 + file]; - char symbol = toChar(piece.type); + std::string symbol = toChar(piece.type, piece.color); - if (!piece.color) - symbol = std::tolower(static_cast(symbol)); + const char *bg = ((rank + file) % 2 == 0) ? lightSquare : darkSquare; - std::printf(" %c", symbol); + std::cout << bg << " " << symbol << " " << reset; } - std::printf(" | %d\n", 8 - rank); + std::printf("| %d\n", 8 - rank); } } else { for (int rank = 7; rank >= 0; rank--) { @@ -67,12 +72,11 @@ void printBoard(Board *b) { for (int file = 7; file >= 0; file--) { Piece piece = b->pieces[rank * 8 + file]; - char symbol = toChar(piece.type); + std::string symbol = toChar(piece.type, piece.color); - if (!piece.color) - symbol = std::tolower(static_cast(symbol)); + const char *bg = ((rank + file) % 2 == 0) ? lightSquare : darkSquare; - std::printf(" %c", symbol); + std::cout << bg << " " << symbol << " " << reset; } std::printf(" | %d\n", 8 - rank); diff --git a/src/board.hpp b/src/board.hpp index 44d32ee..e782be5 100644 --- a/src/board.hpp +++ b/src/board.hpp @@ -20,7 +20,7 @@ enum GameState { STALEMATE, DRAW, }; -char toChar(PieceType type); +std::string toChar(PieceType type, bool white); struct Piece { bool color; -- cgit v1.2.3