allow resetting games

This commit is contained in:
2025-03-01 19:13:49 +01:00
parent 03281a6d70
commit d443b25de1
10 changed files with 97 additions and 14 deletions

View File

@@ -19,6 +19,19 @@ Bag::Bag(std::shared_ptr<PiecesList> 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<int, int>& pieceIndex : this->nextBag) {
this->currentBag.emplace_back(pieceIndex);
}
this->nextBag.clear();
}
Piece Bag::lookNext() {
return this->piecesList->getPiece(this->next);
}

View File

@@ -25,6 +25,11 @@ class Bag {
*/
Bag(std::shared_ptr<PiecesList> 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

View File

@@ -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;

View File

@@ -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;

View File

@@ -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<Action>& playerActions) {
if (this->lost || this->hasWon()) return;

View File

@@ -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

View File

@@ -16,10 +16,25 @@ GameBoard::GameBoard(int boardWidth, int boardHeight, const std::shared_ptr<Piec
generator(pieceList),
nextQueueLength(nextQueueLength) {
this->initialize();
}
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() {

View File

@@ -29,6 +29,18 @@ class GameBoard {
*/
GameBoard(int boardWidth, int boardHeight, const std::shared_ptr<PiecesList>& 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

View File

@@ -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) {

View File

@@ -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
*/