minor improvementsw
This commit is contained in:
+4
-6
@@ -19,12 +19,10 @@ private:
|
|||||||
size_t FieldSize = 4;
|
size_t FieldSize = 4;
|
||||||
std::vector<Line> field;
|
std::vector<Line> field;
|
||||||
|
|
||||||
std::vector<Tile> processLine(const std::vector<Tile> &sourceLine, Score &score);
|
Line& getRow(size_t rowIndex) const;
|
||||||
|
Line& getColumn(size_t columnIndex) const;
|
||||||
Line getRow(size_t rowIndex) const;
|
void setRow(size_t rowIndex, const Line &line);
|
||||||
std::vector<Tile> getColumn(size_t columnIndex) const;
|
void setColumn(size_t columnIndex, const Line &line);
|
||||||
void setRow(size_t rowIndex, const std::vector<Tile> &line);
|
|
||||||
void setColumn(size_t columnIndex, const std::vector<Tile> &line);
|
|
||||||
|
|
||||||
void reset();
|
void reset();
|
||||||
void setSize(size_t size);
|
void setSize(size_t size);
|
||||||
|
|||||||
@@ -10,4 +10,18 @@ struct Line {
|
|||||||
Line(size_t length);
|
Line(size_t length);
|
||||||
|
|
||||||
void process();
|
void process();
|
||||||
|
|
||||||
|
void push_back(Tile tile);
|
||||||
|
size_t size();
|
||||||
|
size_t size() const;
|
||||||
|
|
||||||
|
Tile& operator[](size_t index);
|
||||||
|
const Tile& operator[](size_t index) const;
|
||||||
|
bool operator==(const Line &other) const;
|
||||||
|
bool operator!=(const Line &other) const;
|
||||||
|
|
||||||
|
std::vector<Tile>::iterator begin();
|
||||||
|
std::vector<Tile>::iterator end();
|
||||||
|
std::vector<Tile>::const_iterator begin() const;
|
||||||
|
std::vector<Tile>::const_iterator end() const;
|
||||||
};
|
};
|
||||||
@@ -16,19 +16,19 @@ int main() {
|
|||||||
{
|
{
|
||||||
case 'w':
|
case 'w':
|
||||||
field.moveUp();
|
field.moveUp();
|
||||||
field.spawnTile();
|
field.spawnTile(field.getEmptyTiles());
|
||||||
break;
|
break;
|
||||||
case 's':
|
case 's':
|
||||||
field.moveDown();
|
field.moveDown();
|
||||||
field.spawnTile();
|
field.spawnTile(field.getEmptyTiles());
|
||||||
break;
|
break;
|
||||||
case 'a':
|
case 'a':
|
||||||
field.moveLeft();
|
field.moveLeft();
|
||||||
field.spawnTile();
|
field.spawnTile(field.getEmptyTiles());
|
||||||
break;
|
break;
|
||||||
case 'd':
|
case 'd':
|
||||||
field.moveRight();
|
field.moveRight();
|
||||||
field.spawnTile();
|
field.spawnTile(field.getEmptyTiles());
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
+18
-51
@@ -20,60 +20,24 @@ Public methods:
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
// PRIVATE
|
// PRIVATE
|
||||||
std::vector<Tile> Field::processLine(const std::vector<Tile>& sourceLine, Score &score) {
|
Line& Field::getRow(size_t rowIndex) const {
|
||||||
std::vector<Tile> result;
|
|
||||||
Tile pending = 0;
|
|
||||||
bool hasPending = false;
|
|
||||||
|
|
||||||
for (Tile element : sourceLine) {
|
|
||||||
if (element == 0) continue;
|
|
||||||
|
|
||||||
if (!hasPending) {
|
|
||||||
pending = element;
|
|
||||||
hasPending = true;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (element == pending) {
|
|
||||||
result.push_back(pending * 2);
|
|
||||||
score += pending * 2;
|
|
||||||
hasPending = false;
|
|
||||||
pending = 0;
|
|
||||||
} else {
|
|
||||||
result.push_back(pending);
|
|
||||||
pending = element;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hasPending) {
|
|
||||||
result.push_back(pending);
|
|
||||||
}
|
|
||||||
|
|
||||||
while (result.size() < sourceLine.size()) {
|
|
||||||
result.push_back(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
Line Field::getRow(size_t rowIndex) const {
|
|
||||||
return this->field[rowIndex];
|
return this->field[rowIndex];
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<Tile> Field::getColumn(size_t columnIndex) const {
|
Line& Field::getColumn(size_t columnIndex) const {
|
||||||
// from top to bottom
|
// from top to bottom
|
||||||
std::vector<Tile> column;
|
Line column;
|
||||||
for (size_t i = 0; i < this->FieldSize; ++i) {
|
for (size_t i = 0; i < this->FieldSize; ++i) {
|
||||||
column.push_back(this->field[i][columnIndex]);
|
column.push_back(this->field[i][columnIndex]);
|
||||||
}
|
}
|
||||||
return column;
|
return column;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Field::setRow(size_t rowIndex, const std::vector<Tile> &line) {
|
void Field::setRow(size_t rowIndex, const Line &line) {
|
||||||
this->field[rowIndex] = line;
|
this->field[rowIndex] = line;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Field::setColumn(size_t columnIndex, const std::vector<Tile> &line) {
|
void Field::setColumn(size_t columnIndex, const Line &line) {
|
||||||
// from top to bottom (same)
|
// from top to bottom (same)
|
||||||
for (size_t i = 0; i < this->FieldSize; ++i) {
|
for (size_t i = 0; i < this->FieldSize; ++i) {
|
||||||
this->field[i][columnIndex] = line[i];
|
this->field[i][columnIndex] = line[i];
|
||||||
@@ -81,7 +45,10 @@ void Field::setColumn(size_t columnIndex, const std::vector<Tile> &line) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Field::reset() {
|
void Field::reset() {
|
||||||
this->field.resize(this->FieldSize, std::vector<Tile>(this->FieldSize, 0));
|
this->field.resize(this->FieldSize);
|
||||||
|
for (size_t i = 0; i < this->FieldSize; ++i) {
|
||||||
|
this->field[i] = Line(this->FieldSize);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// PUBLIC
|
// PUBLIC
|
||||||
@@ -89,7 +56,7 @@ Field::Field(size_t fieldSize) : mt(std::random_device{}()), FieldSize(fieldSize
|
|||||||
this->reset();
|
this->reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
void printVector(const std::vector<Tile> &arr) {
|
void printVector(const Line &arr) {
|
||||||
for (size_t i = 0; i < arr.size(); ++i) {
|
for (size_t i = 0; i < arr.size(); ++i) {
|
||||||
printf("%llu ", arr[i]);
|
printf("%llu ", arr[i]);
|
||||||
}
|
}
|
||||||
@@ -99,9 +66,9 @@ void printVector(const std::vector<Tile> &arr) {
|
|||||||
bool Field::moveUp() {
|
bool Field::moveUp() {
|
||||||
auto oldField = this->field;
|
auto oldField = this->field;
|
||||||
for (size_t i = 0; i < this->FieldSize; ++i) {
|
for (size_t i = 0; i < this->FieldSize; ++i) {
|
||||||
std::vector<Tile> column;
|
Line column;
|
||||||
column = getColumn(i);
|
column = getColumn(i);
|
||||||
column = processLine(column, this->score);
|
column.process();
|
||||||
setColumn(i, column);
|
setColumn(i, column);
|
||||||
}
|
}
|
||||||
return this->field != oldField;
|
return this->field != oldField;
|
||||||
@@ -110,10 +77,10 @@ bool Field::moveUp() {
|
|||||||
bool Field::moveDown() {
|
bool Field::moveDown() {
|
||||||
auto oldField = this->field;
|
auto oldField = this->field;
|
||||||
for (size_t i = 0; i < this->FieldSize; ++i) {
|
for (size_t i = 0; i < this->FieldSize; ++i) {
|
||||||
std::vector<Tile> column;
|
Line column;
|
||||||
column = getColumn(i);
|
column = getColumn(i);
|
||||||
std::reverse(column.begin(), column.end());
|
std::reverse(column.begin(), column.end());
|
||||||
column = processLine(column, this->score);
|
column.process();
|
||||||
std::reverse(column.begin(), column.end());
|
std::reverse(column.begin(), column.end());
|
||||||
setColumn(i, column);
|
setColumn(i, column);
|
||||||
}
|
}
|
||||||
@@ -122,10 +89,10 @@ bool Field::moveDown() {
|
|||||||
|
|
||||||
bool Field::moveLeft() {
|
bool Field::moveLeft() {
|
||||||
auto oldField = this->field;
|
auto oldField = this->field;
|
||||||
std::vector<Tile> row;
|
Line row;
|
||||||
for (size_t i = 0; i < this->FieldSize; ++i) {
|
for (size_t i = 0; i < this->FieldSize; ++i) {
|
||||||
row = getRow(i);
|
row = getRow(i);
|
||||||
row = processLine(row, this->score);
|
row.process();
|
||||||
setRow(i, row);
|
setRow(i, row);
|
||||||
}
|
}
|
||||||
return this->field != oldField;
|
return this->field != oldField;
|
||||||
@@ -133,11 +100,11 @@ bool Field::moveLeft() {
|
|||||||
|
|
||||||
bool Field::moveRight() {
|
bool Field::moveRight() {
|
||||||
auto oldField = this->field;
|
auto oldField = this->field;
|
||||||
std::vector<Tile> row;
|
Line row;
|
||||||
for (size_t i = 0; i < this->FieldSize; ++i) {
|
for (size_t i = 0; i < this->FieldSize; ++i) {
|
||||||
row = getRow(i);
|
row = getRow(i);
|
||||||
std::reverse(row.begin(), row.end());
|
std::reverse(row.begin(), row.end());
|
||||||
row = processLine(row, this->score);
|
row.process();
|
||||||
std::reverse(row.begin(), row.end());
|
std::reverse(row.begin(), row.end());
|
||||||
setRow(i, row);
|
setRow(i, row);
|
||||||
}
|
}
|
||||||
|
|||||||
+56
-3
@@ -1,5 +1,11 @@
|
|||||||
#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.reserve(4);
|
this->line.reserve(4);
|
||||||
@@ -10,7 +16,7 @@ Line::Line(size_t length) {
|
|||||||
this->line.reserve(length);
|
this->line.reserve(length);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Line::processLine() {
|
void Line::process() {
|
||||||
std::vector<Tile> result;
|
std::vector<Tile> result;
|
||||||
Tile pending = 0;
|
Tile pending = 0;
|
||||||
bool hasPending = false;
|
bool hasPending = false;
|
||||||
@@ -39,9 +45,56 @@ void Line::processLine() {
|
|||||||
result.push_back(pending);
|
result.push_back(pending);
|
||||||
}
|
}
|
||||||
|
|
||||||
while (result.size() < sourceLine.size()) {
|
while (result.size() < this->line.size()) {
|
||||||
result.push_back(0);
|
result.push_back(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
this->line = result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// base methods
|
||||||
|
void Line::push_back(Tile tile) {
|
||||||
|
this->line.push_back(tile);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Line::size() {
|
||||||
|
return this->line.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Line::size() const {
|
||||||
|
return this->line.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
// overloadings
|
||||||
|
Tile& Line::operator[](size_t index) {
|
||||||
|
return this->line[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
const Tile& Line::operator[](size_t index) const {
|
||||||
|
return this->line[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Line::operator==(const Line &other) const {
|
||||||
|
return this->line == other.line && this->score == other.score;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Line::operator!=(const Line &other) const {
|
||||||
|
return !(*this == other);
|
||||||
|
}
|
||||||
|
|
||||||
|
// iterators
|
||||||
|
std::vector<Tile>::iterator Line::begin() {
|
||||||
|
return this->line.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<Tile>::iterator Line::end() {
|
||||||
|
return this->line.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<Tile>::const_iterator Line::begin() const {
|
||||||
|
return this->line.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<Tile>::const_iterator Line::end() const {
|
||||||
|
return this->line.end();
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user