From 119b85ea3150764d7759e9459de0919692ecb73f Mon Sep 17 00:00:00 2001 From: German Mikheev Date: Sun, 24 May 2026 14:21:35 +0300 Subject: [PATCH] refactored --- include/field.h | 9 ++---- include/types.h | 9 +++++- main.cpp | 9 +++--- source/field.cpp | 83 ++++++++++++++++++++++-------------------------- source/line.cpp | 4 +-- 5 files changed, 56 insertions(+), 58 deletions(-) diff --git a/include/field.h b/include/field.h index 4e8dfd7..ec40ac6 100755 --- a/include/field.h +++ b/include/field.h @@ -19,8 +19,8 @@ private: size_t FieldSize = 4; std::vector field; - Line& getRow(size_t rowIndex) const; - Line& getColumn(size_t columnIndex) const; + Line getRow(size_t rowIndex) const; + Line getColumn(size_t columnIndex) const; void setRow(size_t rowIndex, const Line &line); void setColumn(size_t columnIndex, const Line &line); @@ -31,10 +31,7 @@ public: Field(size_t fieldSize); bool canMove() const; - bool moveUp(); - bool moveDown(); - bool moveLeft(); - bool moveRight(); + bool move(Direction direction); std::vector getEmptyTiles() const; bool spawnTile(const std::vector &empty); diff --git a/include/types.h b/include/types.h index 0362b9e..945c78b 100644 --- a/include/types.h +++ b/include/types.h @@ -1,4 +1,11 @@ #pragma once using Tile = unsigned long long int; -using Score = unsigned long long int; \ No newline at end of file +using Score = unsigned long long int; + +enum class Direction { + up, + down, + left, + right +}; \ No newline at end of file diff --git a/main.cpp b/main.cpp index 818aaea..e19367f 100755 --- a/main.cpp +++ b/main.cpp @@ -3,6 +3,7 @@ #include #include "field.h" + using Tile = unsigned long long int; int main() { @@ -15,19 +16,19 @@ int main() { switch (action) { case 'w': - field.moveUp(); + field.move(Direction::up); field.spawnTile(field.getEmptyTiles()); break; case 's': - field.moveDown(); + field.move(Direction::down); field.spawnTile(field.getEmptyTiles()); break; case 'a': - field.moveLeft(); + field.move(Direction::left); field.spawnTile(field.getEmptyTiles()); break; case 'd': - field.moveRight(); + field.move(Direction::right); field.spawnTile(field.getEmptyTiles()); break; default: diff --git a/source/field.cpp b/source/field.cpp index 04e2d6c..c486023 100755 --- a/source/field.cpp +++ b/source/field.cpp @@ -20,11 +20,11 @@ Public methods: */ // PRIVATE -Line& Field::getRow(size_t rowIndex) const { +Line Field::getRow(size_t rowIndex) const { return this->field[rowIndex]; } -Line& Field::getColumn(size_t columnIndex) const { +Line Field::getColumn(size_t columnIndex) const { // from top to bottom Line column; for (size_t i = 0; i < this->FieldSize; ++i) { @@ -63,55 +63,48 @@ void printVector(const Line &arr) { printf("\n"); } -bool Field::moveUp() { +bool Field::move(Direction direction) { auto oldField = this->field; - for (size_t i = 0; i < this->FieldSize; ++i) { - Line column; - column = getColumn(i); - column.process(); - setColumn(i, column); - } - return this->field != oldField; -} + Line column; + Line row; -bool Field::moveDown() { - auto oldField = this->field; - for (size_t i = 0; i < this->FieldSize; ++i) { - Line column; - column = getColumn(i); - std::reverse(column.begin(), column.end()); - column.process(); - std::reverse(column.begin(), column.end()); - setColumn(i, column); + switch (direction) { + case Direction::up: + for (size_t i = 0; i < this->FieldSize; ++i) { + column = getColumn(i); + column.process(); + setColumn(i, column); + } + break; + case Direction::down: + for (size_t i = 0; i < this->FieldSize; ++i) { + column = getColumn(i); + std::reverse(column.begin(), column.end()); + column.process(); + std::reverse(column.begin(), column.end()); + setColumn(i, column); + } + break; + case Direction::left: + for (size_t i = 0; i < this->FieldSize; ++i) { + row = getRow(i); + row.process(); + setRow(i, row); + } + case Direction::right: + for (size_t i = 0; i < this->FieldSize; ++i) { + row = getRow(i); + std::reverse(row.begin(), row.end()); + row.process(); + std::reverse(row.begin(), row.end()); + setRow(i, row); + } + default: + throw std::exception(); } return this->field != oldField; } -bool Field::moveLeft() { - auto oldField = this->field; - Line row; - for (size_t i = 0; i < this->FieldSize; ++i) { - row = getRow(i); - row.process(); - setRow(i, row); - } - return this->field != oldField; -} - -bool Field::moveRight() { - auto oldField = this->field; - Line row; - for (size_t i = 0; i < this->FieldSize; ++i) { - row = getRow(i); - std::reverse(row.begin(), row.end()); - row.process(); - std::reverse(row.begin(), row.end()); - setRow(i, row); - } - return this->field != oldField; -} - - std::vector Field::getEmptyTiles() const { std::vector empty; for (size_t i = 0; i < this->FieldSize; ++i) { diff --git a/source/line.cpp b/source/line.cpp index 92755f6..3aa1882 100644 --- a/source/line.cpp +++ b/source/line.cpp @@ -8,12 +8,12 @@ Fields: Line::Line() { this->line.clear(); - this->line.reserve(4); + this->line.resize(4); } Line::Line(size_t length) { this->line.clear(); - this->line.reserve(length); + this->line.resize(length); } void Line::process() {