cleanup & architectural changes
This commit is contained in:
+3
-1
@@ -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);
|
||||
|
||||
|
||||
@@ -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) {}
|
||||
};
|
||||
@@ -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) {}
|
||||
};
|
||||
@@ -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;
|
||||
|
||||
+21
-20
@@ -1,21 +1,5 @@
|
||||
#include "field.h"
|
||||
|
||||
/*
|
||||
Fields:
|
||||
std::vector<std::vector<int>> field;
|
||||
|
||||
Private methods:
|
||||
std::vector<Tile> processLine(const std::vector<Tile> &sourceLine);
|
||||
|
||||
std::vector<Tile> getRow(size_t idx);
|
||||
std::vector<Tile> 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);
|
||||
}
|
||||
@@ -1,11 +1,5 @@
|
||||
#include "line.h"
|
||||
|
||||
/*
|
||||
Fields:
|
||||
std::vector<Tile> line;
|
||||
Score score;
|
||||
*/
|
||||
|
||||
Line::Line() {
|
||||
this->line.clear();
|
||||
this->line.resize(0);
|
||||
|
||||
Reference in New Issue
Block a user