From d443b25de1e3fbaca7477dd16fec57466c31402e Mon Sep 17 00:00:00 2001 From: zulianc Date: Sat, 1 Mar 2025 19:13:49 +0100 Subject: [PATCH] allow resetting games --- src/Core/Bag.cpp | 13 +++++++++++++ src/Core/Bag.h | 5 +++++ src/Core/Board.cpp | 13 ++++++++----- src/Core/Board.h | 7 ++++++- src/Core/Game.cpp | 24 ++++++++++++++++-------- src/Core/Game.h | 13 +++++++++++++ src/Core/GameBoard.cpp | 15 +++++++++++++++ src/Core/GameBoard.h | 12 ++++++++++++ src/Core/GameParameters.cpp | 4 ++++ src/Core/GameParameters.h | 5 +++++ 10 files changed, 97 insertions(+), 14 deletions(-) diff --git a/src/Core/Bag.cpp b/src/Core/Bag.cpp index d3fd993..5c8d92e 100644 --- a/src/Core/Bag.cpp +++ b/src/Core/Bag.cpp @@ -19,6 +19,19 @@ Bag::Bag(std::shared_ptr piecesList) : this->prepareNext(); } +void Bag::jumpToNextBag() { + // if the bag is empty switch to the next bag + if (this->currentBag.empty()) { + std::swap(this->currentBag, this->nextBag); + } + + // get the already used pieces back to the current bag + for (const std::pair& pieceIndex : this->nextBag) { + this->currentBag.emplace_back(pieceIndex); + } + this->nextBag.clear(); +} + Piece Bag::lookNext() { return this->piecesList->getPiece(this->next); } diff --git a/src/Core/Bag.h b/src/Core/Bag.h index 1d99b27..d780bbc 100644 --- a/src/Core/Bag.h +++ b/src/Core/Bag.h @@ -25,6 +25,11 @@ class Bag { */ Bag(std::shared_ptr piecesList); + /** + * Ignores the remaining pieces in the current bag and startd fresh from a new bag + */ + void jumpToNextBag(); + /** * Looks at what the next picked piece will be, without removing it from the bag * @return The next piece diff --git a/src/Core/Board.cpp b/src/Core/Board.cpp index 901bc96..109a9f7 100644 --- a/src/Core/Board.cpp +++ b/src/Core/Board.cpp @@ -16,11 +16,7 @@ Board::Board(int width, int height) : this->emptyRow.push_back(NOTHING); } - // initialize grid - this->grid.clear(); - for (int j = 0; j < height; j++) { - this->grid.push_back(this->emptyRow); - } + this->clearBoard(); } void Board::changeBlock(const Position& position, Block block) { @@ -76,6 +72,13 @@ int Board::clearRows() { return clearedLines; } +void Board::clearBoard() { + this->grid.clear(); + for (int j = 0; j < height; j++) { + this->grid.push_back(this->emptyRow); + } +} + Block Board::getBlock(const Position& position) const { if (position.x < 0 || position.x >= this->width || position.y < 0) return OUT_OF_BOUNDS; diff --git a/src/Core/Board.h b/src/Core/Board.h index fa2c2e6..c92a9b2 100644 --- a/src/Core/Board.h +++ b/src/Core/Board.h @@ -39,7 +39,12 @@ class Board { int clearRows(); /** - * @return The block of the block at the specified position + * Deletes any block currently on the board + */ + void clearBoard(); + + /** + * @return A copy of the block at the specified position */ Block getBlock(const Position& position) const; diff --git a/src/Core/Game.cpp b/src/Core/Game.cpp index cf073b7..8eddfcf 100644 --- a/src/Core/Game.cpp +++ b/src/Core/Game.cpp @@ -20,16 +20,29 @@ Game::Game(Gamemode gamemode, const Player& controls, int boardWidth, int boardH parameters(gamemode, controls), board(boardWidth, boardHeight, bag, parameters.getNextQueueLength()) { - // the game has not yet started + this->initialize(); +} + +void Game::start() { + this->started = true; + this->lost = this->board.spawnNextPiece(); +} + +void Game::reset() { + this->initialize(); + + this->parameters.reset(); + this->board.reset(); +} + +void Game::initialize() { this->started = false; this->lost = false; - // initialize stats this->score = 0; this->framesPassed = 0; this->B2BChain = 0; - // nothing happened yet this->heldActions.clear(); this->initialActions.clear(); this->heldDAS = 0; @@ -40,11 +53,6 @@ Game::Game(Gamemode gamemode, const Player& controls, int boardWidth, int boardH this->totalForcedLockDelay = 0; } -void Game::start() { - this->started = true; - this->lost = this->board.spawnNextPiece(); -} - void Game::nextFrame(const std::set& playerActions) { if (this->lost || this->hasWon()) return; diff --git a/src/Core/Game.h b/src/Core/Game.h index d45be1e..e827f7a 100644 --- a/src/Core/Game.h +++ b/src/Core/Game.h @@ -40,6 +40,19 @@ class Game { */ void start(); + /** + * Resets the game + */ + void reset(); + + private: + /** + * Initializes the game + */ + void initialize(); + + public: + /** * Advances to the next frame while excecuting the actions taken by the player, * this is where the main game logic takes place diff --git a/src/Core/GameBoard.cpp b/src/Core/GameBoard.cpp index 4e8c939..9a3a04f 100644 --- a/src/Core/GameBoard.cpp +++ b/src/Core/GameBoard.cpp @@ -16,10 +16,25 @@ GameBoard::GameBoard(int boardWidth, int boardHeight, const std::shared_ptrinitialize(); +} + +void GameBoard::reset() { + this->board.clearBoard(); + this->generator.jumpToNextBag(); + + this->initialize(); +} + +void GameBoard::initialize() { this->nextQueue.clear(); for (int i = 0; i < nextQueueLength; i++) { this->nextQueue.push_back(this->generator.getNext()); } + + this->activePiece = nullptr; + this->heldPiece = nullptr; + this->isLastMoveKick = false; } bool GameBoard::moveLeft() { diff --git a/src/Core/GameBoard.h b/src/Core/GameBoard.h index 1ec09eb..6d34cab 100644 --- a/src/Core/GameBoard.h +++ b/src/Core/GameBoard.h @@ -29,6 +29,18 @@ class GameBoard { */ GameBoard(int boardWidth, int boardHeight, const std::shared_ptr& bag, int nextQueueLength); + /** + * Resets the board as if it was newly created + */ + void reset(); + + private: + /** + * Initializes the board + */ + void initialize(); + + public: /** * Tries moving the piece one position to the left * @return If it suceeded diff --git a/src/Core/GameParameters.cpp b/src/Core/GameParameters.cpp index f783435..23cbe7a 100644 --- a/src/Core/GameParameters.cpp +++ b/src/Core/GameParameters.cpp @@ -8,6 +8,10 @@ GameParameters::GameParameters(Gamemode gamemode, const Player& controls) : gamemode(gamemode), controls(controls) { + this->reset(); +} + +void GameParameters::reset() { // initialize lines and level this->clearedLines = 0; switch (this->gamemode) { diff --git a/src/Core/GameParameters.h b/src/Core/GameParameters.h index 495db07..c06eabc 100644 --- a/src/Core/GameParameters.h +++ b/src/Core/GameParameters.h @@ -31,6 +31,11 @@ class GameParameters { */ GameParameters(Gamemode gamemode, const Player& controls); + /** + * Resets all stats and parameters + */ + void reset(); + /** * Counts the newly cleared lines and update level and stats if needed */