add terminal colors

This commit is contained in:
eay 2023-08-28 08:16:30 -07:00
parent cfcd99b0db
commit 8d6935a0d3
3 changed files with 24 additions and 13 deletions

View File

@ -7,18 +7,18 @@ enum TileColor {
BLUE, BLUE,
GREEN, GREEN,
YELLOW, YELLOW,
BLACK, WHITE,
NUMCOLORS NUMCOLORS
}; };
const std::string TileColorStrings[NUMCOLORS] = { const std::string TileColorStrings[NUMCOLORS] = {
"red", "\x1B[1;41mR\x1B[0m",//"red",
"blue", "\x1B[1;44mB\x1B[0m",//"blue",
"green", "\x1B[1;42mG\x1B[0m",//"green",
"yellow", "\x1B[1;30;103mY\x1B[0m",//"yellow",
"black" "\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 int MAXPLAYERS = 4;
const std::string REQ_TYPE_DRAW_FROM_FACTORY = "DRAW_FROM_FACTORY"; const std::string REQ_TYPE_DRAW_FROM_FACTORY = "DRAW_FROM_FACTORY";

View File

@ -276,7 +276,6 @@ std::string Player::printMyBoard() const {
std::ostringstream oss; std::ostringstream oss;
oss << "*******************************\n"; oss << "*******************************\n";
oss << "PLAYER: " << myName << "\n"; oss << "PLAYER: " << myName << "\n";
oss << *myBoardPtr << "\n\n";
for (int ii = 0; ii < azool::NUMCOLORS; ++ii) { for (int ii = 0; ii < azool::NUMCOLORS; ++ii) {
oss << ii+1 << ") "; oss << ii+1 << ") ";
for (int jj = azool::NUMCOLORS; jj > (ii+1); --jj) { for (int jj = azool::NUMCOLORS; jj > (ii+1); --jj) {
@ -297,7 +296,7 @@ std::string Player::printMyBoard() const {
// color = row + column % 5 // color = row + column % 5
char color = (ii + jj) % 5; char color = (ii + jj) % 5;
if (myGrid[ii][jj]) { if (myGrid[ii][jj]) {
oss << static_cast<char>(azool::TileColorSyms[color] - 32) << "|"; oss << azool::TileColorStrings[color] << "|";
} }
else { else {
oss << azool::TileColorSyms[color] << "|"; oss << azool::TileColorSyms[color] << "|";
@ -305,7 +304,14 @@ std::string Player::printMyBoard() const {
} }
oss << "\n"; oss << "\n";
} // iterate over rows } // 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 << "Score: " << myScore << "\n";
oss << "-------------------------------\n"; oss << "-------------------------------\n";
return oss.str(); return oss.str();
@ -374,7 +380,7 @@ int promptForFactoryIdx(int maxFactIdx) {
return factIdx; return factIdx;
} }
azool::TileColor promptForColor() { 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'; char colorInput = '\0';
std::cout << promptColorDraw << std::flush; std::cout << promptColorDraw << std::flush;
std::cin >> colorInput; std::cin >> colorInput;
@ -391,8 +397,8 @@ azool::TileColor promptForColor() {
case 'y': case 'y':
return azool::YELLOW; return azool::YELLOW;
break; break;
case 'k': case 'w':
return azool::BLACK; return azool::WHITE;
break; break;
default: default:
return azool::NONE; return azool::NONE;
@ -523,6 +529,7 @@ void Player::takeTurn() {
} // discard from pool } // discard from pool
} }
else if (drawType == 'P') { else if (drawType == 'P') {
std::cout << *myBoardPtr << "\n\n";
std::cout << printMyBoard(); std::cout << printMyBoard();
} }
else { else {
@ -530,6 +537,7 @@ void Player::takeTurn() {
} }
} // while !fullinput } // while !fullinput
// options: take tile from pool or take tile from factory // options: take tile from pool or take tile from factory
std::cout << "\033c" << std::flush;
std::cout << printMyBoard() << std::flush; std::cout << printMyBoard() << std::flush;
// flush out any inputs still in the buffer // flush out any inputs still in the buffer
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

View File

@ -23,7 +23,9 @@ void playGame(GameBoard* game) {
while (!game->endOfRound()) { while (!game->endOfRound()) {
// TODO figure out how order will work for > 2 players // TODO figure out how order will work for > 2 players
firstPlayer->takeTurn(); firstPlayer->takeTurn();
sleep(1);
secondPlayer->takeTurn(); secondPlayer->takeTurn();
sleep(1);
} }
// check who took penalty // check who took penalty
// needs to be done before calling player->endRound() // needs to be done before calling player->endRound()
@ -41,6 +43,7 @@ void playGame(GameBoard* game) {
secondPlayer = players[1]; secondPlayer = players[1];
} }
std::cout << "End of round!" << std::endl; std::cout << "End of round!" << std::endl;
std::cout << "\033c";
players[0]->endRound(p0EndsGame); players[0]->endRound(p0EndsGame);
players[1]->endRound(p1EndsGame); players[1]->endRound(p1EndsGame);
} }