From 03281a6d70ae131fd57d03b92bfe9d9674838870 Mon Sep 17 00:00:00 2001 From: zulianc Date: Sat, 1 Mar 2025 18:41:09 +0100 Subject: [PATCH] =?UTF-8?q?ajoute=20la=20possiblilit=C3=A9=20d'ajouter=20d?= =?UTF-8?q?es=20lignes=20de=20garbage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Core/Board.cpp | 16 +++++++++++++++- src/Core/Board.h | 9 +++++++-- src/Core/GameBoard.cpp | 14 +++++++++++++- src/Core/GameBoard.h | 5 +++++ 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/Core/Board.cpp b/src/Core/Board.cpp index 0bdf7d6..901bc96 100644 --- a/src/Core/Board.cpp +++ b/src/Core/Board.cpp @@ -23,7 +23,7 @@ Board::Board(int width, int height) : } } -void Board::addBlock(const Position& position, Block block) { +void Board::changeBlock(const Position& position, Block block) { // if the block is out of bounds we discard it if (position.x < 0 || position.x >= this->width || position.y < 0) return; @@ -37,6 +37,20 @@ void Board::addBlock(const Position& position, Block block) { this->grid.at(position.y).at(position.x) = block; } +void Board::insertRow(int height, int holePosition, Block blockType) { + std::vector insertedRow; + for (int i = 0; i < this->width; i++) { + if (i == holePosition) { + insertedRow.push_back(NOTHING); + } + else { + insertedRow.push_back(blockType); + } + } + + this->grid.insert(this->grid.begin() + height, insertedRow); +} + int Board::clearRows() { // check from top to bottom, so that erasing lines don't screw up the looping int clearedLines = 0; diff --git a/src/Core/Board.h b/src/Core/Board.h index 763673a..fa2c2e6 100644 --- a/src/Core/Board.h +++ b/src/Core/Board.h @@ -23,9 +23,14 @@ class Board { Board(int width, int height); /** - * Change the block of the specified block, if the block is out of bounds it is simply ignored + * Changes the block at the specified position, if the block is out of bounds it is simply ignored */ - void addBlock(const Position& position, Block block); + void changeBlock(const Position& position, Block block); + + /** + * Inserts a row of the specified block type (unless on the specified column that has a hole), at the specified height + */ + void insertRow(int height, int holePosition, Block blockType); /** * Clears any complete row and moves down the rows on top diff --git a/src/Core/GameBoard.cpp b/src/Core/GameBoard.cpp index b850c80..4e8c939 100644 --- a/src/Core/GameBoard.cpp +++ b/src/Core/GameBoard.cpp @@ -8,6 +8,7 @@ #include #include #include +#include GameBoard::GameBoard(int boardWidth, int boardHeight, const std::shared_ptr& pieceList, int nextQueueLength) : @@ -215,12 +216,23 @@ LineClear GameBoard::lockPiece() { && this->isActivePieceInWall(Position{-1, 0}) && this->isActivePieceInWall(Position{0, -1})); for (Position position : this->activePiece->getPositions()) { - this->board.addBlock(position + this->activePiecePosition, this->activePiece->getBlockType()); + this->board.changeBlock(position + this->activePiecePosition, this->activePiece->getBlockType()); } return LineClear{this->board.clearRows(), isLockedInPlace, (!isLockedInPlace) && this->isLastMoveKick}; } +void GameBoard::addGarbageRows(int number) { + int holePosition = std::rand() % this->board.getWidth(); + + for (int i = 0; i < number; i++) { + this->board.insertRow(0, holePosition, GARBAGE); + if (this->touchesGround()) { + this->activePiecePosition.y += 1; + } + } +} + Board GameBoard::getBoard() const { return this->board; } diff --git a/src/Core/GameBoard.h b/src/Core/GameBoard.h index b8ba86e..1ec09eb 100644 --- a/src/Core/GameBoard.h +++ b/src/Core/GameBoard.h @@ -85,6 +85,11 @@ class GameBoard { */ LineClear lockPiece(); + /** + * Adds a specified number of garbage rows to the bottom of the board, the hole position being random but the same for all of them + */ + void addGarbageRows(int number); + /** * @return A copy of the board */