ajoute la possiblilité d'ajouter des lignes de garbage

This commit is contained in:
2025-03-01 18:41:09 +01:00
parent 66c2cf1013
commit 03281a6d70
4 changed files with 40 additions and 4 deletions

View File

@@ -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 the block is out of bounds we discard it
if (position.x < 0 || position.x >= this->width || position.y < 0) return; 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; this->grid.at(position.y).at(position.x) = block;
} }
void Board::insertRow(int height, int holePosition, Block blockType) {
std::vector<Block> 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() { int Board::clearRows() {
// check from top to bottom, so that erasing lines don't screw up the looping // check from top to bottom, so that erasing lines don't screw up the looping
int clearedLines = 0; int clearedLines = 0;

View File

@@ -23,9 +23,14 @@ class Board {
Board(int width, int height); 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 * Clears any complete row and moves down the rows on top

View File

@@ -8,6 +8,7 @@
#include <vector> #include <vector>
#include <set> #include <set>
#include <memory> #include <memory>
#include <cstdlib>
GameBoard::GameBoard(int boardWidth, int boardHeight, const std::shared_ptr<PiecesList>& pieceList, int nextQueueLength) : GameBoard::GameBoard(int boardWidth, int boardHeight, const std::shared_ptr<PiecesList>& pieceList, int nextQueueLength) :
@@ -215,12 +216,23 @@ LineClear GameBoard::lockPiece() {
&& this->isActivePieceInWall(Position{-1, 0}) && this->isActivePieceInWall(Position{0, -1})); && this->isActivePieceInWall(Position{-1, 0}) && this->isActivePieceInWall(Position{0, -1}));
for (Position position : this->activePiece->getPositions()) { 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}; 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 { Board GameBoard::getBoard() const {
return this->board; return this->board;
} }

View File

@@ -85,6 +85,11 @@ class GameBoard {
*/ */
LineClear lockPiece(); 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 * @return A copy of the board
*/ */