refactored
This commit is contained in:
+3
-6
@@ -19,8 +19,8 @@ private:
|
|||||||
size_t FieldSize = 4;
|
size_t FieldSize = 4;
|
||||||
std::vector<Line> field;
|
std::vector<Line> field;
|
||||||
|
|
||||||
Line& getRow(size_t rowIndex) const;
|
Line getRow(size_t rowIndex) const;
|
||||||
Line& getColumn(size_t columnIndex) const;
|
Line getColumn(size_t columnIndex) const;
|
||||||
void setRow(size_t rowIndex, const Line &line);
|
void setRow(size_t rowIndex, const Line &line);
|
||||||
void setColumn(size_t columnIndex, const Line &line);
|
void setColumn(size_t columnIndex, const Line &line);
|
||||||
|
|
||||||
@@ -31,10 +31,7 @@ public:
|
|||||||
Field(size_t fieldSize);
|
Field(size_t fieldSize);
|
||||||
|
|
||||||
bool canMove() const;
|
bool canMove() const;
|
||||||
bool moveUp();
|
bool move(Direction direction);
|
||||||
bool moveDown();
|
|
||||||
bool moveLeft();
|
|
||||||
bool moveRight();
|
|
||||||
|
|
||||||
std::vector<Position> getEmptyTiles() const;
|
std::vector<Position> getEmptyTiles() const;
|
||||||
bool spawnTile(const std::vector<Position> &empty);
|
bool spawnTile(const std::vector<Position> &empty);
|
||||||
|
|||||||
+8
-1
@@ -1,4 +1,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
using Tile = unsigned long long int;
|
using Tile = unsigned long long int;
|
||||||
using Score = unsigned long long int;
|
using Score = unsigned long long int;
|
||||||
|
|
||||||
|
enum class Direction {
|
||||||
|
up,
|
||||||
|
down,
|
||||||
|
left,
|
||||||
|
right
|
||||||
|
};
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include "field.h"
|
#include "field.h"
|
||||||
|
|
||||||
|
|
||||||
using Tile = unsigned long long int;
|
using Tile = unsigned long long int;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
@@ -15,19 +16,19 @@ int main() {
|
|||||||
switch (action)
|
switch (action)
|
||||||
{
|
{
|
||||||
case 'w':
|
case 'w':
|
||||||
field.moveUp();
|
field.move(Direction::up);
|
||||||
field.spawnTile(field.getEmptyTiles());
|
field.spawnTile(field.getEmptyTiles());
|
||||||
break;
|
break;
|
||||||
case 's':
|
case 's':
|
||||||
field.moveDown();
|
field.move(Direction::down);
|
||||||
field.spawnTile(field.getEmptyTiles());
|
field.spawnTile(field.getEmptyTiles());
|
||||||
break;
|
break;
|
||||||
case 'a':
|
case 'a':
|
||||||
field.moveLeft();
|
field.move(Direction::left);
|
||||||
field.spawnTile(field.getEmptyTiles());
|
field.spawnTile(field.getEmptyTiles());
|
||||||
break;
|
break;
|
||||||
case 'd':
|
case 'd':
|
||||||
field.moveRight();
|
field.move(Direction::right);
|
||||||
field.spawnTile(field.getEmptyTiles());
|
field.spawnTile(field.getEmptyTiles());
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|||||||
+38
-45
@@ -20,11 +20,11 @@ Public methods:
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
// PRIVATE
|
// PRIVATE
|
||||||
Line& Field::getRow(size_t rowIndex) const {
|
Line Field::getRow(size_t rowIndex) const {
|
||||||
return this->field[rowIndex];
|
return this->field[rowIndex];
|
||||||
}
|
}
|
||||||
|
|
||||||
Line& Field::getColumn(size_t columnIndex) const {
|
Line Field::getColumn(size_t columnIndex) const {
|
||||||
// from top to bottom
|
// from top to bottom
|
||||||
Line column;
|
Line column;
|
||||||
for (size_t i = 0; i < this->FieldSize; ++i) {
|
for (size_t i = 0; i < this->FieldSize; ++i) {
|
||||||
@@ -63,55 +63,48 @@ void printVector(const Line &arr) {
|
|||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Field::moveUp() {
|
bool Field::move(Direction direction) {
|
||||||
auto oldField = this->field;
|
auto oldField = this->field;
|
||||||
for (size_t i = 0; i < this->FieldSize; ++i) {
|
Line column;
|
||||||
Line column;
|
Line row;
|
||||||
column = getColumn(i);
|
|
||||||
column.process();
|
|
||||||
setColumn(i, column);
|
|
||||||
}
|
|
||||||
return this->field != oldField;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Field::moveDown() {
|
switch (direction) {
|
||||||
auto oldField = this->field;
|
case Direction::up:
|
||||||
for (size_t i = 0; i < this->FieldSize; ++i) {
|
for (size_t i = 0; i < this->FieldSize; ++i) {
|
||||||
Line column;
|
column = getColumn(i);
|
||||||
column = getColumn(i);
|
column.process();
|
||||||
std::reverse(column.begin(), column.end());
|
setColumn(i, column);
|
||||||
column.process();
|
}
|
||||||
std::reverse(column.begin(), column.end());
|
break;
|
||||||
setColumn(i, column);
|
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;
|
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<Position> Field::getEmptyTiles() const {
|
std::vector<Position> Field::getEmptyTiles() const {
|
||||||
std::vector<Position> empty;
|
std::vector<Position> empty;
|
||||||
for (size_t i = 0; i < this->FieldSize; ++i) {
|
for (size_t i = 0; i < this->FieldSize; ++i) {
|
||||||
|
|||||||
+2
-2
@@ -8,12 +8,12 @@ Fields:
|
|||||||
|
|
||||||
Line::Line() {
|
Line::Line() {
|
||||||
this->line.clear();
|
this->line.clear();
|
||||||
this->line.reserve(4);
|
this->line.resize(4);
|
||||||
}
|
}
|
||||||
|
|
||||||
Line::Line(size_t length) {
|
Line::Line(size_t length) {
|
||||||
this->line.clear();
|
this->line.clear();
|
||||||
this->line.reserve(length);
|
this->line.resize(length);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Line::process() {
|
void Line::process() {
|
||||||
|
|||||||
Reference in New Issue
Block a user