From 8d6935a0d39d5ea95bf7aeb65029fceacf4334ee Mon Sep 17 00:00:00 2001 From: eay Date: Mon, 28 Aug 2023 08:16:30 -0700 Subject: [PATCH] add terminal colors --- include/tile_utils.h | 14 +++++++------- src/Player.cc | 20 ++++++++++++++------ src/main.cc | 3 +++ 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/include/tile_utils.h b/include/tile_utils.h index 905fb1a..1eca065 100644 --- a/include/tile_utils.h +++ b/include/tile_utils.h @@ -7,18 +7,18 @@ enum TileColor { BLUE, GREEN, YELLOW, - BLACK, + WHITE, NUMCOLORS }; const std::string TileColorStrings[NUMCOLORS] = { - "red", - "blue", - "green", - "yellow", - "black" + "\x1B[1;41mR\x1B[0m",//"red", + "\x1B[1;44mB\x1B[0m",//"blue", + "\x1B[1;42mG\x1B[0m",//"green", + "\x1B[1;30;103mY\x1B[0m",//"yellow", + "\x1B[1;30;107mW\x1B[0m",//"white" }; -const char TileColorSyms[NUMCOLORS] = { 'r', 'b', 'g', 'y', 'k' }; +const char TileColorSyms[NUMCOLORS] = { 'r', 'b', 'g', 'y', 'w' }; const int MAXPLAYERS = 4; const std::string REQ_TYPE_DRAW_FROM_FACTORY = "DRAW_FROM_FACTORY"; diff --git a/src/Player.cc b/src/Player.cc index 452c402..dfb628d 100644 --- a/src/Player.cc +++ b/src/Player.cc @@ -276,7 +276,6 @@ std::string Player::printMyBoard() const { std::ostringstream oss; oss << "*******************************\n"; oss << "PLAYER: " << myName << "\n"; - oss << *myBoardPtr << "\n\n"; for (int ii = 0; ii < azool::NUMCOLORS; ++ii) { oss << ii+1 << ") "; for (int jj = azool::NUMCOLORS; jj > (ii+1); --jj) { @@ -297,7 +296,7 @@ std::string Player::printMyBoard() const { // color = row + column % 5 char color = (ii + jj) % 5; if (myGrid[ii][jj]) { - oss << static_cast(azool::TileColorSyms[color] - 32) << "|"; + oss << azool::TileColorStrings[color] << "|"; } else { oss << azool::TileColorSyms[color] << "|"; @@ -305,7 +304,14 @@ std::string Player::printMyBoard() const { } oss << "\n"; } // iterate over rows - oss << "Penalties: " << myNumPenaltiesForRound << "\n"; + oss << "Penalties: " << myNumPenaltiesForRound; + if (myTookPoolPenaltyThisRound) { + oss << "*"; + } + if (myNumPenaltiesForRound > 0) { + oss << " (-" << getScorePenalty() << ")"; + } + oss << "\n"; oss << "Score: " << myScore << "\n"; oss << "-------------------------------\n"; return oss.str(); @@ -374,7 +380,7 @@ int promptForFactoryIdx(int maxFactIdx) { return factIdx; } azool::TileColor promptForColor() { - static const char* promptColorDraw = "Which color? [r|b|g|y|k]\n"; + static const char* promptColorDraw = "Which color? [r|b|g|y|w]\n"; char colorInput = '\0'; std::cout << promptColorDraw << std::flush; std::cin >> colorInput; @@ -391,8 +397,8 @@ azool::TileColor promptForColor() { case 'y': return azool::YELLOW; break; - case 'k': - return azool::BLACK; + case 'w': + return azool::WHITE; break; default: return azool::NONE; @@ -523,6 +529,7 @@ void Player::takeTurn() { } // discard from pool } else if (drawType == 'P') { + std::cout << *myBoardPtr << "\n\n"; std::cout << printMyBoard(); } else { @@ -530,6 +537,7 @@ void Player::takeTurn() { } } // while !fullinput // options: take tile from pool or take tile from factory + std::cout << "\033c" << std::flush; std::cout << printMyBoard() << std::flush; // flush out any inputs still in the buffer std::cin.ignore(std::numeric_limits::max(), '\n'); diff --git a/src/main.cc b/src/main.cc index 4a3a992..c800e81 100644 --- a/src/main.cc +++ b/src/main.cc @@ -23,7 +23,9 @@ void playGame(GameBoard* game) { while (!game->endOfRound()) { // TODO figure out how order will work for > 2 players firstPlayer->takeTurn(); + sleep(1); secondPlayer->takeTurn(); + sleep(1); } // check who took penalty // needs to be done before calling player->endRound() @@ -41,6 +43,7 @@ void playGame(GameBoard* game) { secondPlayer = players[1]; } std::cout << "End of round!" << std::endl; + std::cout << "\033c"; players[0]->endRound(p0EndsGame); players[1]->endRound(p1EndsGame); }