diff --git a/include/field.h b/include/field.h index 8134048..ccd0160 100755 --- a/include/field.h +++ b/include/field.h @@ -32,7 +32,7 @@ public: void updateScore(); Score getScore(); - bool canMove() const; + bool hasMoves() const; bool move(Direction direction); std::vector getEmptyTiles() const; diff --git a/main.cpp b/main.cpp index 968d0e8..c635edd 100755 --- a/main.cpp +++ b/main.cpp @@ -14,24 +14,24 @@ int main() { field.spawnTile(field.getEmptyTiles()); field.spawnTile(field.getEmptyTiles()); - while (canMove) { + while (field.hasMoves()) { field.debug(); std::cin >> action; switch (action) { case 'w': - canMove = field.move(Direction::up); + field.move(Direction::up); field.spawnTile(field.getEmptyTiles()); break; case 's': - canMove = field.move(Direction::down); + field.move(Direction::down); field.spawnTile(field.getEmptyTiles()); break; case 'a': - canMove = field.move(Direction::left); + field.move(Direction::left); field.spawnTile(field.getEmptyTiles()); break; case 'd': - canMove = field.move(Direction::right); + field.move(Direction::right); field.spawnTile(field.getEmptyTiles()); break; default: diff --git a/source/field.cpp b/source/field.cpp index c8b4f04..6460f7c 100755 --- a/source/field.cpp +++ b/source/field.cpp @@ -39,7 +39,7 @@ Field::Field(size_t fieldSize) : mt(std::random_device{}()), FieldSize(fieldSize void Field::updateScore() { for (size_t i = 0; i < this->FieldSize; ++i) { - this->score += this->field[i].score; + this->score = this->field[i].score; } } @@ -47,17 +47,20 @@ Score Field::getScore() { return this->score; } -bool Field::canMove() const { +bool Field::hasMoves() const { if (!this->getEmptyTiles().empty()) { return true; } - for (int x = 0; x < this->field.size(); ++x) { - for (int y = 0; y < this->field.size(); ++y) { - // TODO: implement - // if (x + 1 < ) + for (size_t x = 0; x < this->field.size(); ++x) { + for (size_t y = 0; y < this->field.size(); ++y) { + if (this->field[x][y] == this->field[x + 1][y] || + this->field[x][y] == this->field[x][y + 1] ) { + return true; + } } } + return false; } bool Field::move(Direction direction) { @@ -132,7 +135,7 @@ bool Field::spawnTile(const std::vector &empty) { void Field::debug() const { for (size_t i = 0; i < this->FieldSize; ++i) { for (size_t j = 0; j < this->FieldSize; ++j) { - printf("%5llu ", this->field[i][j]); + printf("%3llu ", this->field[i][j]); } printf("\n"); } diff --git a/source/line.cpp b/source/line.cpp index eada07a..0bb918d 100644 --- a/source/line.cpp +++ b/source/line.cpp @@ -1,14 +1,8 @@ #include "line.h" -Line::Line() { - this->line.clear(); - this->line.resize(0); -} +Line::Line() = default; -Line::Line(size_t length) { - this->line.clear(); - this->line.resize(length); -} +Line::Line(size_t length) : line(length) {} void Line::process() { std::vector result;