ajoute la possiblilité d'ajouter des lignes de garbage
This commit is contained in:
@@ -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<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() {
|
||||
// check from top to bottom, so that erasing lines don't screw up the looping
|
||||
int clearedLines = 0;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <memory>
|
||||
#include <cstdlib>
|
||||
|
||||
|
||||
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}));
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user