diff --git a/include/field.h b/include/field.h index ec40ac6..8134048 100755 --- a/include/field.h +++ b/include/field.h @@ -1,6 +1,5 @@ #pragma once -#include "tilePosition.h" #include "line.h" #include "types.h" @@ -30,6 +29,9 @@ private: public: Field(size_t fieldSize); + void updateScore(); + Score getScore(); + bool canMove() const; bool move(Direction direction); diff --git a/include/tilePosition.h b/include/tilePosition.h deleted file mode 100755 index 991d03f..0000000 --- a/include/tilePosition.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -struct Position { - int x; - int y; - - Position() : x(0), y(0) {} - Position(int x, int y) : x(x), y(y) {} -}; \ No newline at end of file diff --git a/include/types.h b/include/types.h index 945c78b..4f9c837 100644 --- a/include/types.h +++ b/include/types.h @@ -8,4 +8,12 @@ enum class Direction { down, left, right +}; + +struct Position { + int x; + int y; + + Position() : x(0), y(0) {} + Position(int x, int y) : x(x), y(y) {} }; \ No newline at end of file diff --git a/main.cpp b/main.cpp index aa2a9e4..968d0e8 100755 --- a/main.cpp +++ b/main.cpp @@ -9,34 +9,38 @@ using Tile = unsigned long long int; int main() { Field field(4); char action; + bool canMove = true; field.spawnTile(field.getEmptyTiles()); field.spawnTile(field.getEmptyTiles()); - while (true) { + while (canMove) { field.debug(); std::cin >> action; - switch (action) - { + switch (action) { case 'w': - field.move(Direction::up); + canMove = field.move(Direction::up); field.spawnTile(field.getEmptyTiles()); break; case 's': - field.move(Direction::down); + canMove = field.move(Direction::down); field.spawnTile(field.getEmptyTiles()); break; case 'a': - field.move(Direction::left); + canMove = field.move(Direction::left); field.spawnTile(field.getEmptyTiles()); break; case 'd': - field.move(Direction::right); + canMove = field.move(Direction::right); field.spawnTile(field.getEmptyTiles()); break; default: return 0; } + field.updateScore(); + } + if (!canMove) { + printf("Game Over! Score: %llu", field.getScore()); } return 0; diff --git a/source/field.cpp b/source/field.cpp index 5430bdd..c8b4f04 100755 --- a/source/field.cpp +++ b/source/field.cpp @@ -1,21 +1,5 @@ #include "field.h" -/* -Fields: - std::vector> field; - -Private methods: - std::vector processLine(const std::vector &sourceLine); - - std::vector getRow(size_t idx); - std::vector getColumn(size_t idx); - void setRow(size_t idx); - void setColumn(size_t idx); - -Public methods: - bool move(Direction direction); -*/ - // PRIVATE Line Field::getRow(size_t rowIndex) const { return this->field[rowIndex]; @@ -53,11 +37,27 @@ Field::Field(size_t fieldSize) : mt(std::random_device{}()), FieldSize(fieldSize this->reset(); } -void printVector(const Line &arr) { - for (size_t i = 0; i < arr.size(); ++i) { - printf("%llu ", arr[i]); +void Field::updateScore() { + for (size_t i = 0; i < this->FieldSize; ++i) { + this->score += this->field[i].score; + } +} + +Score Field::getScore() { + return this->score; +} + +bool Field::canMove() 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 < ) + } } - printf("\n"); } bool Field::move(Direction direction) { @@ -136,4 +136,5 @@ void Field::debug() const { } printf("\n"); } + printf("Current score: %llu", this->score); } \ No newline at end of file diff --git a/source/line.cpp b/source/line.cpp index 5c58619..eada07a 100644 --- a/source/line.cpp +++ b/source/line.cpp @@ -1,11 +1,5 @@ #include "line.h" -/* -Fields: - std::vector line; - Score score; -*/ - Line::Line() { this->line.clear(); this->line.resize(0);