allow resetting games
This commit is contained in:
@@ -19,6 +19,19 @@ Bag::Bag(std::shared_ptr<PiecesList> piecesList) :
|
|||||||
this->prepareNext();
|
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() {
|
Piece Bag::lookNext() {
|
||||||
return this->piecesList->getPiece(this->next);
|
return this->piecesList->getPiece(this->next);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,6 +25,11 @@ class Bag {
|
|||||||
*/
|
*/
|
||||||
Bag(std::shared_ptr<PiecesList> piecesList);
|
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
|
* Looks at what the next picked piece will be, without removing it from the bag
|
||||||
* @return The next piece
|
* @return The next piece
|
||||||
|
|||||||
@@ -16,11 +16,7 @@ Board::Board(int width, int height) :
|
|||||||
this->emptyRow.push_back(NOTHING);
|
this->emptyRow.push_back(NOTHING);
|
||||||
}
|
}
|
||||||
|
|
||||||
// initialize grid
|
this->clearBoard();
|
||||||
this->grid.clear();
|
|
||||||
for (int j = 0; j < height; j++) {
|
|
||||||
this->grid.push_back(this->emptyRow);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Board::changeBlock(const Position& position, Block block) {
|
void Board::changeBlock(const Position& position, Block block) {
|
||||||
@@ -76,6 +72,13 @@ int Board::clearRows() {
|
|||||||
return clearedLines;
|
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 {
|
Block Board::getBlock(const Position& position) const {
|
||||||
if (position.x < 0 || position.x >= this->width || position.y < 0) return OUT_OF_BOUNDS;
|
if (position.x < 0 || position.x >= this->width || position.y < 0) return OUT_OF_BOUNDS;
|
||||||
|
|
||||||
|
|||||||
@@ -39,7 +39,12 @@ class Board {
|
|||||||
int clearRows();
|
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;
|
Block getBlock(const Position& position) const;
|
||||||
|
|
||||||
|
|||||||
@@ -20,16 +20,29 @@ Game::Game(Gamemode gamemode, const Player& controls, int boardWidth, int boardH
|
|||||||
parameters(gamemode, controls),
|
parameters(gamemode, controls),
|
||||||
board(boardWidth, boardHeight, bag, parameters.getNextQueueLength()) {
|
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->started = false;
|
||||||
this->lost = false;
|
this->lost = false;
|
||||||
|
|
||||||
// initialize stats
|
|
||||||
this->score = 0;
|
this->score = 0;
|
||||||
this->framesPassed = 0;
|
this->framesPassed = 0;
|
||||||
this->B2BChain = 0;
|
this->B2BChain = 0;
|
||||||
|
|
||||||
// nothing happened yet
|
|
||||||
this->heldActions.clear();
|
this->heldActions.clear();
|
||||||
this->initialActions.clear();
|
this->initialActions.clear();
|
||||||
this->heldDAS = 0;
|
this->heldDAS = 0;
|
||||||
@@ -40,11 +53,6 @@ Game::Game(Gamemode gamemode, const Player& controls, int boardWidth, int boardH
|
|||||||
this->totalForcedLockDelay = 0;
|
this->totalForcedLockDelay = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Game::start() {
|
|
||||||
this->started = true;
|
|
||||||
this->lost = this->board.spawnNextPiece();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Game::nextFrame(const std::set<Action>& playerActions) {
|
void Game::nextFrame(const std::set<Action>& playerActions) {
|
||||||
if (this->lost || this->hasWon()) return;
|
if (this->lost || this->hasWon()) return;
|
||||||
|
|
||||||
|
|||||||
@@ -40,6 +40,19 @@ class Game {
|
|||||||
*/
|
*/
|
||||||
void start();
|
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,
|
* Advances to the next frame while excecuting the actions taken by the player,
|
||||||
* this is where the main game logic takes place
|
* this is where the main game logic takes place
|
||||||
|
|||||||
@@ -16,10 +16,25 @@ GameBoard::GameBoard(int boardWidth, int boardHeight, const std::shared_ptr<Piec
|
|||||||
generator(pieceList),
|
generator(pieceList),
|
||||||
nextQueueLength(nextQueueLength) {
|
nextQueueLength(nextQueueLength) {
|
||||||
|
|
||||||
|
this->initialize();
|
||||||
|
}
|
||||||
|
|
||||||
|
void GameBoard::reset() {
|
||||||
|
this->board.clearBoard();
|
||||||
|
this->generator.jumpToNextBag();
|
||||||
|
|
||||||
|
this->initialize();
|
||||||
|
}
|
||||||
|
|
||||||
|
void GameBoard::initialize() {
|
||||||
this->nextQueue.clear();
|
this->nextQueue.clear();
|
||||||
for (int i = 0; i < nextQueueLength; i++) {
|
for (int i = 0; i < nextQueueLength; i++) {
|
||||||
this->nextQueue.push_back(this->generator.getNext());
|
this->nextQueue.push_back(this->generator.getNext());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this->activePiece = nullptr;
|
||||||
|
this->heldPiece = nullptr;
|
||||||
|
this->isLastMoveKick = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GameBoard::moveLeft() {
|
bool GameBoard::moveLeft() {
|
||||||
|
|||||||
@@ -29,6 +29,18 @@ class GameBoard {
|
|||||||
*/
|
*/
|
||||||
GameBoard(int boardWidth, int boardHeight, const std::shared_ptr<PiecesList>& bag, int nextQueueLength);
|
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
|
* Tries moving the piece one position to the left
|
||||||
* @return If it suceeded
|
* @return If it suceeded
|
||||||
|
|||||||
@@ -8,6 +8,10 @@ GameParameters::GameParameters(Gamemode gamemode, const Player& controls) :
|
|||||||
gamemode(gamemode),
|
gamemode(gamemode),
|
||||||
controls(controls) {
|
controls(controls) {
|
||||||
|
|
||||||
|
this->reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
void GameParameters::reset() {
|
||||||
// initialize lines and level
|
// initialize lines and level
|
||||||
this->clearedLines = 0;
|
this->clearedLines = 0;
|
||||||
switch (this->gamemode) {
|
switch (this->gamemode) {
|
||||||
|
|||||||
@@ -31,6 +31,11 @@ class GameParameters {
|
|||||||
*/
|
*/
|
||||||
GameParameters(Gamemode gamemode, const Player& controls);
|
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
|
* Counts the newly cleared lines and update level and stats if needed
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user