début de la classe PiecesFile
This commit is contained in:
@@ -7,28 +7,19 @@
|
||||
#include <iostream>
|
||||
|
||||
|
||||
static constexpr std::vector<Block> getEmptyRow(int width) {
|
||||
std::vector<Block> emptyRow;
|
||||
for (int i = 0; i < width; i ++) {
|
||||
emptyRow.push_back(NOTHING);
|
||||
}
|
||||
return emptyRow;
|
||||
}
|
||||
|
||||
|
||||
Board::Board(int width, int height) :
|
||||
width(width),
|
||||
height(height) {
|
||||
|
||||
std::vector<Block> emptyRow;
|
||||
this->emptyRow = std::vector<Block>(width);
|
||||
for (int i = 0; i < width; i ++) {
|
||||
emptyRow.push_back(NOTHING);
|
||||
this->emptyRow.push_back(NOTHING);
|
||||
}
|
||||
|
||||
// initialize grid
|
||||
this->grid.clear();
|
||||
for (int j = 0; j < height; j++) {
|
||||
this->grid.push_back(emptyRow);
|
||||
this->grid.push_back(this->emptyRow);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,12 +29,8 @@ void Board::addBlock(const Position& position, Block block) {
|
||||
|
||||
// resize the grid if needed
|
||||
if (position.y >= this->grid.size()) {
|
||||
std::vector<Block> emptyRow;
|
||||
for (int i = 0; i < width; i ++) {
|
||||
emptyRow.push_back(NOTHING);
|
||||
}
|
||||
for (int j = this->grid.size(); j <= position.y; j++) {
|
||||
this->grid.push_back(emptyRow);
|
||||
this->grid.push_back(this->emptyRow);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,24 +38,23 @@ void Board::addBlock(const Position& position, Block block) {
|
||||
}
|
||||
|
||||
int Board::clearRows() {
|
||||
std::vector<Block> emptyRow;
|
||||
for (int i = 0; i < width; i ++) {
|
||||
emptyRow.push_back(NOTHING);
|
||||
}
|
||||
|
||||
// check from top to bottom, so that erasing lines don't screw up the looping
|
||||
int clearedLines = 0;
|
||||
for (int j = this->grid.size() - 1; j >= 0; j--) {
|
||||
bool lineIsFull = true;
|
||||
for (int i = 0; i < this->width; i++) {
|
||||
int i = 0;
|
||||
while (lineIsFull && (i < width)) {
|
||||
if (this->grid.at(j).at(i) == NOTHING) {
|
||||
lineIsFull = false;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
if (lineIsFull) {
|
||||
this->grid.erase(this->grid.begin() + j);
|
||||
if(this->grid.size() < height) this->grid.push_back(emptyRow);
|
||||
if(this->grid.size() < height) {
|
||||
this->grid.push_back(this->emptyRow);
|
||||
}
|
||||
clearedLines++;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user