cleanup & architectural changes
This commit is contained in:
+3
-1
@@ -1,6 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "tilePosition.h"
|
|
||||||
#include "line.h"
|
#include "line.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
@@ -30,6 +29,9 @@ private:
|
|||||||
public:
|
public:
|
||||||
Field(size_t fieldSize);
|
Field(size_t fieldSize);
|
||||||
|
|
||||||
|
void updateScore();
|
||||||
|
Score getScore();
|
||||||
|
|
||||||
bool canMove() const;
|
bool canMove() const;
|
||||||
bool move(Direction direction);
|
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,
|
down,
|
||||||
left,
|
left,
|
||||||
right
|
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() {
|
int main() {
|
||||||
Field field(4);
|
Field field(4);
|
||||||
char action;
|
char action;
|
||||||
|
bool canMove = true;
|
||||||
|
|
||||||
field.spawnTile(field.getEmptyTiles());
|
field.spawnTile(field.getEmptyTiles());
|
||||||
field.spawnTile(field.getEmptyTiles());
|
field.spawnTile(field.getEmptyTiles());
|
||||||
|
|
||||||
while (true) {
|
while (canMove) {
|
||||||
field.debug();
|
field.debug();
|
||||||
std::cin >> action;
|
std::cin >> action;
|
||||||
switch (action)
|
switch (action) {
|
||||||
{
|
|
||||||
case 'w':
|
case 'w':
|
||||||
field.move(Direction::up);
|
canMove = field.move(Direction::up);
|
||||||
field.spawnTile(field.getEmptyTiles());
|
field.spawnTile(field.getEmptyTiles());
|
||||||
break;
|
break;
|
||||||
case 's':
|
case 's':
|
||||||
field.move(Direction::down);
|
canMove = field.move(Direction::down);
|
||||||
field.spawnTile(field.getEmptyTiles());
|
field.spawnTile(field.getEmptyTiles());
|
||||||
break;
|
break;
|
||||||
case 'a':
|
case 'a':
|
||||||
field.move(Direction::left);
|
canMove = field.move(Direction::left);
|
||||||
field.spawnTile(field.getEmptyTiles());
|
field.spawnTile(field.getEmptyTiles());
|
||||||
break;
|
break;
|
||||||
case 'd':
|
case 'd':
|
||||||
field.move(Direction::right);
|
canMove = field.move(Direction::right);
|
||||||
field.spawnTile(field.getEmptyTiles());
|
field.spawnTile(field.getEmptyTiles());
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
field.updateScore();
|
||||||
|
}
|
||||||
|
if (!canMove) {
|
||||||
|
printf("Game Over! Score: %llu", field.getScore());
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
+21
-20
@@ -1,21 +1,5 @@
|
|||||||
#include "field.h"
|
#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
|
// PRIVATE
|
||||||
Line Field::getRow(size_t rowIndex) const {
|
Line Field::getRow(size_t rowIndex) const {
|
||||||
return this->field[rowIndex];
|
return this->field[rowIndex];
|
||||||
@@ -53,11 +37,27 @@ Field::Field(size_t fieldSize) : mt(std::random_device{}()), FieldSize(fieldSize
|
|||||||
this->reset();
|
this->reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
void printVector(const Line &arr) {
|
void Field::updateScore() {
|
||||||
for (size_t i = 0; i < arr.size(); ++i) {
|
for (size_t i = 0; i < this->FieldSize; ++i) {
|
||||||
printf("%llu ", arr[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) {
|
bool Field::move(Direction direction) {
|
||||||
@@ -136,4 +136,5 @@ void Field::debug() const {
|
|||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
printf("Current score: %llu", this->score);
|
||||||
}
|
}
|
||||||
@@ -1,11 +1,5 @@
|
|||||||
#include "line.h"
|
#include "line.h"
|
||||||
|
|
||||||
/*
|
|
||||||
Fields:
|
|
||||||
std::vector<Tile> line;
|
|
||||||
Score score;
|
|
||||||
*/
|
|
||||||
|
|
||||||
Line::Line() {
|
Line::Line() {
|
||||||
this->line.clear();
|
this->line.clear();
|
||||||
this->line.resize(0);
|
this->line.resize(0);
|
||||||
|
|||||||
Reference in New Issue
Block a user