Compare commits

...

2 Commits

Author SHA1 Message Date
emclrk
8736aee27c lint/style changes 2023-08-12 17:00:24 -07:00
emclrk
4404eb21e9 small cleanup 2023-06-16 17:07:26 -07:00
6 changed files with 94 additions and 84 deletions

View File

@ -22,7 +22,9 @@ public:
bool takeTilesFromPool(azool::TileColor color, int& numTiles, bool& poolPenalty); bool takeTilesFromPool(azool::TileColor color, int& numTiles, bool& poolPenalty);
void returnTilesToBag(int numTiles, azool::TileColor color); void returnTilesToBag(int numTiles, azool::TileColor color);
void dealTiles(); void dealTiles();
int numFactories() { return tileFactories.size(); } int numFactories() {
return tileFactories.size();
}
bool endOfRound() { bool endOfRound() {
// round ends when the pool and tile factories are empty // round ends when the pool and tile factories are empty
for (int ii = 0; ii < azool::NUMCOLORS; ++ii) { for (int ii = 0; ii < azool::NUMCOLORS; ++ii) {

View File

@ -15,10 +15,16 @@ public:
void placeTiles(int rowIdx, azool::TileColor color, int numTiles); void placeTiles(int rowIdx, azool::TileColor color, int numTiles);
void endRound(bool& fullRow); void endRound(bool& fullRow);
void finalizeScore(); void finalizeScore();
int getScore() const { return myScore; } int getScore() const {
return myScore;
}
std::string printMyBoard() const; std::string printMyBoard() const;
bool tookPenalty() const { return myTookPoolPenaltyThisRound; } bool tookPenalty() const {
const std::string getPlayerName() const { return myName; } return myTookPoolPenaltyThisRound;
}
const std::string getPlayerName() const {
return myName;
}
private: private:
Player(const Player&) = delete; Player(const Player&) = delete;

View File

@ -1,23 +1,23 @@
#ifndef TILE_UTILS_H_ #ifndef TILE_UTILS_H_
#define TILE_UTILS_H_ #define TILE_UTILS_H_
namespace azool { namespace azool {
enum TileColor { enum TileColor {
NONE = -1, NONE = -1,
RED = 0, RED = 0,
BLUE, BLUE,
GREEN, GREEN,
YELLOW, YELLOW,
BLACK, BLACK,
NUMCOLORS NUMCOLORS
}; };
const std::string TileColorStrings[NUMCOLORS] = { const std::string TileColorStrings[NUMCOLORS] = {
"red", "red",
"blue", "blue",
"green", "green",
"yellow", "yellow",
"black" "black"
}; };
const char TileColorSyms[NUMCOLORS] = { 'r', 'b', 'g', 'y', 'k' }; const char TileColorSyms[NUMCOLORS] = { 'r', 'b', 'g', 'y', 'k' };
} }
#endif // TILE_UTILS_H_ #endif // TILE_UTILS_H_

View File

@ -10,9 +10,9 @@ GameBoard::GameBoard(int numPlayers) :
tileBag(), tileBag(),
lastRound(false), lastRound(false),
rng(std::default_random_engine( rng(std::default_random_engine(
std::chrono::system_clock::now().time_since_epoch().count())) { std::chrono::system_clock::now().time_since_epoch().count())) {
resetBoard(); resetBoard();
} // GameBoard::GameBoard } // GameBoard::GameBoard
std::ostream& operator<<(std::ostream& out, const GameBoard& board) { std::ostream& operator<<(std::ostream& out, const GameBoard& board) {
// user will input 1-indexed value, even though we 0-index internally // user will input 1-indexed value, even though we 0-index internally
@ -100,7 +100,7 @@ void GameBoard::dealTiles() {
} }
tileFactories.push_back(fact); tileFactories.push_back(fact);
} }
} // GameBoard::dealTiles } // GameBoard::dealTiles
void GameBoard::resetBoard() { void GameBoard::resetBoard() {
memset(pool, 0, azool::NUMCOLORS*sizeof(int)); memset(pool, 0, azool::NUMCOLORS*sizeof(int));

View File

@ -11,13 +11,13 @@ Player::Player(GameBoard* const board, std::string name) :
myScore(0), myScore(0),
myNumPenaltiesForRound(0), myNumPenaltiesForRound(0),
myTookPoolPenaltyThisRound(false) { myTookPoolPenaltyThisRound(false) {
int gridSize = azool::NUMCOLORS * azool::NUMCOLORS; int gridSize = azool::NUMCOLORS * azool::NUMCOLORS;
memset(myGrid, 0, gridSize*sizeof(bool)); memset(myGrid, 0, gridSize*sizeof(bool));
for (int ii = 0; ii < azool::NUMCOLORS; ++ii) { for (int ii = 0; ii < azool::NUMCOLORS; ++ii) {
myRows[ii].first = 0; myRows[ii].first = 0;
myRows[ii].second = azool::NONE; myRows[ii].second = azool::NONE;
} // initialize rows } // initialize rows
} // Player::Player } // Player::Player
bool Player::checkValidMove(azool::TileColor color, int rowIdx) const { bool Player::checkValidMove(azool::TileColor color, int rowIdx) const {
// check if valid move // check if valid move
@ -250,7 +250,7 @@ std::string Player::printMyBoard() const {
oss << "_"; oss << "_";
} }
else if (jj < myRows[ii].first) { else if (jj < myRows[ii].first) {
oss << azool::TileColorSyms[myRows[ii].second]; oss << azool::TileColorSyms[myRows[ii].second];
} }
} }
// print grid row // print grid row
@ -296,54 +296,54 @@ bool Player::discardFromPool(azool::TileColor color) {
} // Player::discardFromPool } // Player::discardFromPool
namespace { namespace {
int promptForFactoryIdx(int maxNumFactories) { int promptForFactoryIdx(int maxNumFactories) {
static const char* promptFactoryIdxDraw = "Which factory? enter index\n"; static const char* promptFactoryIdxDraw = "Which factory? enter index\n";
char factInput; // TODO can we safely say there will never be more than 9 possible? char factInput; // TODO can we safely say there will never be more than 9 possible?
std::cout << promptFactoryIdxDraw << std::flush; std::cout << promptFactoryIdxDraw << std::flush;
std::cin >> factInput; std::cin >> factInput;
int factIdx = std::atoi(&factInput); int factIdx = std::atoi(&factInput);
if (factIdx < 1 or factIdx > maxNumFactories) { if (factIdx < 1 or factIdx > maxNumFactories) {
return -1; return -1;
}
return factIdx;
} }
azool::TileColor promptForColor() { return factIdx;
static const char* promptColorDraw = "Which color? [r|b|g|y|k]\n"; }
char colorInput = '\0'; azool::TileColor promptForColor() {
std::cout << promptColorDraw << std::flush; static const char* promptColorDraw = "Which color? [r|b|g|y|k]\n";
std::cin >> colorInput; char colorInput = '\0';
switch(colorInput) { std::cout << promptColorDraw << std::flush;
case 'r': std::cin >> colorInput;
return azool::RED; switch(colorInput) {
break; case 'r':
case 'b': return azool::RED;
return azool::BLUE; break;
break; case 'b':
case 'g': return azool::BLUE;
return azool::GREEN; break;
break; case 'g':
case 'y': return azool::GREEN;
return azool::YELLOW; break;
break; case 'y':
case 'k': return azool::YELLOW;
return azool::BLACK; break;
break; case 'k':
default: return azool::BLACK;
return azool::NONE; break;
} // end switch default:
return azool::NONE; return azool::NONE;
} // end switch
return azool::NONE;
}
int promptForRow() {
static const char* promptRowPlacement = "Place on which row? enter number [1-5]\n";
char rowInput;
std::cout << promptRowPlacement << std::flush;
std::cin >> rowInput;
int rowIdx = std::atoi(&rowInput);
if (rowIdx < 1 or rowIdx > azool::NUMCOLORS) {
return -1;
} }
int promptForRow() { return rowIdx;
static const char* promptRowPlacement = "Place on which row? enter number [1-5]\n"; }
char rowInput;
std::cout << promptRowPlacement << std::flush;
std::cin >> rowInput;
int rowIdx = std::atoi(&rowInput);
if (rowIdx < 1 or rowIdx > azool::NUMCOLORS) {
return -1;
}
return rowIdx;
}
} // anonymous namespace } // anonymous namespace
void Player::takeTurn() { void Player::takeTurn() {

View File

@ -44,12 +44,14 @@ void playGame(GameBoard* game) {
players[0]->endRound(p0EndsGame); players[0]->endRound(p0EndsGame);
players[1]->endRound(p1EndsGame); players[1]->endRound(p1EndsGame);
} }
players[0]->finalizeScore(); std::cout << " Final scores:\n";
players[1]->finalizeScore();
std::cout << " Final scores:\n" << players[0]->getScore() << "\n" << players[1]->getScore() << "\n" << std::flush;
std::cout << players[0]->printMyBoard();
std::cout << players[1]->printMyBoard();
for (auto player : players) { for (auto player : players) {
player->finalizeScore();
std::cout << player->getPlayerName() << ": " << player->getScore() << "\n" << std::flush;
}
// separate loops b/c we want scores to print before printing boards
for (auto player : players) {
std::cout << player->printMyBoard() << std::flush;
if (player) delete player; if (player) delete player;
} }
} }