bugfixes for starting new round / finishing game

also rename some variables for clarity
This commit is contained in:
eay 2023-06-04 15:41:56 -07:00
parent 0f5928266b
commit 6d6716acbf
4 changed files with 48 additions and 41 deletions

View File

@ -17,7 +17,7 @@ public:
void finalizeScore();
int getScore() const { return myScore; }
std::string printMyBoard() const;
bool tookPenalty() const { return myTookPoolPenaltyThisTurn; }
bool tookPenalty() const { return myTookPoolPenaltyThisRound; }
const std::string getPlayerName() const { return myName; }
private:
@ -33,8 +33,8 @@ private:
TileRow myRows[azool::NUMCOLORS];
GameBoard* const myBoardPtr;
int myScore;
int myNumPenaltiesForTurn;
bool myTookPoolPenaltyThisTurn;
int myNumPenaltiesForRound;
bool myTookPoolPenaltyThisRound;
const std::vector<int> PenaltyPoints = {0, 1, 2, 3, 5, 7, 10, 13, 15};
std::string myName;
}; // class Player

View File

@ -1,5 +1,6 @@
#include "GameBoard.h"
#include <algorithm>
#include <chrono>
GameBoard::GameBoard(int numPlayers) :
tileFactories(),
@ -8,7 +9,8 @@ GameBoard::GameBoard(int numPlayers) :
whiteTileInPool(true),
tileBag(),
lastRound(false),
rng(std::default_random_engine()) {
rng(std::default_random_engine(
std::chrono::system_clock::now().time_since_epoch().count())) {
resetBoard();
} // GameBoard::GameBoard

View File

@ -9,8 +9,8 @@ Player::Player(GameBoard* const board, std::string name) :
myBoardPtr(board),
myName(name),
myScore(0),
myNumPenaltiesForTurn(0),
myTookPoolPenaltyThisTurn(false) {
myNumPenaltiesForRound(0),
myTookPoolPenaltyThisRound(false) {
int gridSize = azool::NUMCOLORS * azool::NUMCOLORS;
memset(myGrid, 0, gridSize*sizeof(bool));
for (int ii = 0; ii < azool::NUMCOLORS; ++ii) {
@ -62,12 +62,13 @@ bool Player::takeTilesFromPool(azool::TileColor color, int rowIdx) {
return false; // couldn't get that tile from the pool
}
if (poolPenalty) {
myTookPoolPenaltyThisTurn = poolPenalty;
myNumPenaltiesForTurn++;
myTookPoolPenaltyThisRound = true;
myNumPenaltiesForRound++;
}
placeTiles(rowIdx, color, numTiles);
return true;
} // Player::takeTilesFromPool
void Player::placeTiles(int rowIdx, azool::TileColor color, int numTiles) {
// increment row with # of new tiles
myRows[rowIdx].first += numTiles;
@ -76,7 +77,7 @@ void Player::placeTiles(int rowIdx, azool::TileColor color, int numTiles) {
int maxNumInRow = rowIdx + 1;
// if tiles overflow the row, take penalty(ies)
if (myRows[rowIdx].first > maxNumInRow) {
myNumPenaltiesForTurn += (myRows[rowIdx].first - maxNumInRow);
myNumPenaltiesForRound += (myRows[rowIdx].first - maxNumInRow);
myRows[rowIdx].first = maxNumInRow;
}
} // Player::placeTiles
@ -103,18 +104,19 @@ void Player::endRound(bool& fullRow) {
myRows[rowIdx].second = azool::NONE;
}
}
#ifdef DEBUG
std::cout << "Had " << myNumPenaltiesForTurn << " penalties this turn" << std::endl;
#endif
if (myNumPenaltiesForTurn >= PenaltyPoints.size()) {
if (myNumPenaltiesForRound >= PenaltyPoints.size()) {
myScore -= PenaltyPoints[PenaltyPoints.size() - 1];
}
else {
myScore -= PenaltyPoints[myNumPenaltiesForTurn];
myScore -= PenaltyPoints[myNumPenaltiesForRound];
}
// reset for next turn
myTookPoolPenaltyThisTurn = false;
myNumPenaltiesForTurn = 0;
// FOR THIS REASON
// main loop needs to check who took penalty BEFORE calling this function
// I don't love that, but not sure what else to do
myTookPoolPenaltyThisRound = false;
myNumPenaltiesForRound = 0;
// Check if there's a full row on the grid
for (int rowIdx = 0; rowIdx < azool::NUMCOLORS; ++rowIdx) {
fullRow = true;
@ -176,7 +178,9 @@ int Player::scoreTile(int tileRow, int tileCol) {
}
tileScore++;
} // iterate from current column to right
#ifdef DEBUG
std::cout << " Final Tile Score: " << tileScore << std::endl;
#endif
return tileScore;
} // Player::scoreTile
@ -184,6 +188,7 @@ void Player::finalizeScore() {
int numRows = 0;
int numCols = 0;
// compute bonus for rows
// TODO: print bonus info
for (int rowIdx = 0; rowIdx < azool::NUMCOLORS; ++rowIdx) {
bool fullRow = true;
for (int colIdx = 0; colIdx < azool::NUMCOLORS; ++colIdx) {
@ -262,7 +267,7 @@ std::string Player::printMyBoard() const {
}
oss << "\n";
} // iterate over rows
oss << "Penalties: " << myNumPenaltiesForTurn << "\n";
oss << "Penalties: " << myNumPenaltiesForRound << "\n";
oss << "Score: " << myScore << "\n";
return oss.str();
// TODO(feature) - print penalty tiles (?)
@ -271,7 +276,7 @@ std::string Player::printMyBoard() const {
bool Player::discardFromFactory(int factoryIdx, azool::TileColor color) {
int numTiles = -1;
if (myBoardPtr->takeTilesFromFactory(factoryIdx, color, numTiles)) {
myNumPenaltiesForTurn += numTiles;
myNumPenaltiesForRound += numTiles;
return true;
}
return false;
@ -282,9 +287,9 @@ bool Player::discardFromPool(azool::TileColor color) {
int numTiles = -1;
if (myBoardPtr->takeTilesFromPool(color, numTiles, poolPenalty)) {
if (poolPenalty) {
myNumPenaltiesForTurn++;
myNumPenaltiesForRound++;
}
myNumPenaltiesForTurn += numTiles;
myNumPenaltiesForRound += numTiles;
return true;
}
return false;
@ -293,10 +298,10 @@ bool Player::discardFromPool(azool::TileColor color) {
namespace {
int promptForFactoryIdx(int maxNumFactories) {
static const char* promptFactoryIdxDraw = "Which factory? enter index\n";
char factInput[256]; // wayy more than we need
char factInput; // TODO can we safely say there will never be more than 9 possible?
std::cout << promptFactoryIdxDraw << std::flush;
std::cin >> factInput;
int factIdx = std::atoi(factInput);
int factIdx = std::atoi(&factInput);
if (factIdx < 1 or factIdx > maxNumFactories) {
return -1;
}
@ -330,10 +335,10 @@ namespace {
}
int promptForRow() {
static const char* promptRowPlacement = "Place on which row? enter number [1-5]\n";
char rowInput[256] = {0};
std::cout << promptRowPlacement;
char rowInput;
std::cout << promptRowPlacement << std::flush;
std::cin >> rowInput;
int rowIdx = std::atoi(rowInput);
int rowIdx = std::atoi(&rowInput);
if (rowIdx < 1 or rowIdx > azool::NUMCOLORS) {
return -1;
}
@ -447,5 +452,7 @@ void Player::takeTurn() {
}
} // while !fullinput
// options: take tile from pool or take tile from factory
// TODO(feature) - temporary...reprint board and hold briefly?
std::cout << printMyBoard() << std::flush;
// flush out any inputs still in the buffer
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
} // Player::takeTurn

View File

@ -12,22 +12,21 @@ void testPrint(GameBoard* game) {
}
void playGame(GameBoard* game) {
game->dealTiles();
std::vector<Player*> players = {new Player(game, "P1"), new Player(game, "P2")};
bool endOfGame = false;
Player* firstPlayer = players[0]; // pointers to keep track of first and second player
Player* secondPlayer = players[1];
while (!endOfGame) {
bool p0EndsGame = false;
bool p1EndsGame = false;
while (!(p0EndsGame or p1EndsGame)) {
game->dealTiles();
while (!game->endOfRound()) {
// TODO figure out how order will work for > 2 players
firstPlayer->takeTurn();
secondPlayer->takeTurn();
}
std::cout << "End of round!" << std::endl;
bool p0EndsGame = false;
bool p1EndsGame = false;
players[0]->endRound(p0EndsGame);
players[1]->endRound(p1EndsGame);
// check who took penalty
// nees to be done before claling player->endRound()
if (players[0]->tookPenalty()) {
firstPlayer = players[0];
secondPlayer = players[1];
@ -41,19 +40,18 @@ void playGame(GameBoard* game) {
firstPlayer = players[0];
secondPlayer = players[1];
}
if (!(p0EndsGame or p1EndsGame)) {
game->dealTiles();
}
std::cout << "End of round!" << std::endl;
players[0]->endRound(p0EndsGame);
players[1]->endRound(p1EndsGame);
}
players[0]->finalizeScore();
players[1]->finalizeScore();
std::cout << " Final scores:\n"
<< players[0]->getScore() << "\n" << std::flush;
std::cout << " Final scores:\n" << players[0]->getScore() << "\n" << players[1]->getScore() << "\n" << std::flush;
std::cout << players[0]->printMyBoard();
std::cout << players[0]->getScore() << "\n" << std::flush;
std::cout << players[1]->printMyBoard();
if (players[0]) delete players[0];
if (players[1]) delete players[1];
for (auto player : players) {
if (player) delete player;
}
}
int main() {