forked from jeffchen006/ve280
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid.cpp
More file actions
175 lines (145 loc) · 4.2 KB
/
Copy pathgrid.cpp
File metadata and controls
175 lines (145 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "grid.h"
#include "const.h"
#include "point.h"
#include "tile.h"
#include <iostream>
#include <iomanip>
using namespace std;
static Point checkDst(Direction dir, Point src, const Grid &grid) {
while (grid.insideGrid(adjacentPoint(src, dir)) && grid.isEmpty(adjacentPoint(src, dir))) {
src = adjacentPoint(src, dir);
}
return src;
}
Grid::Grid(unsigned int height, unsigned int width) : height(height), width(width), squares() {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
squares[i][j] = nullptr;
}
}
}
unsigned int Grid::getHeight() const {
return height;
}
unsigned int Grid::getWidth() const {
return width;
}
bool Grid::insideGrid(const Point &pt) const {
return pt.r >= 0 && pt.r < height && pt.c >= 0 && pt.c < width;
}
Tile *Grid::getSquare(const Point &pt) const {
return squares[pt.r][pt.c];
}
void Grid::setSquare(const Point &pt, Tile *tile) {
squares[pt.r][pt.c] = tile;
}
void Grid::clearSquare(const Point &pt) {
setSquare(pt, nullptr);
}
void Grid::clearSquares() {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
clearSquare({i, j});
}
}
}
bool Grid::isEmpty(const Point &pt) const {
return getSquare(pt) == nullptr;
}
void Grid::upgradeTile(const Point &pt) {
squares[pt.r][pt.c]++;
}
static Point sameTileFinder(Point src, Direction dir, const Grid &grid) {
Point dst = adjacentPoint(checkDst(dir, src, grid),dir);
if (grid.insideGrid(dst) && (grid.getSquare(src) != nullptr) && ((grid.getSquare(dst) == grid.getSquare(src)))) {
return dst;
} else {
return src;
}
}
unsigned int Grid::collapseTiles(Direction dir) {
unsigned int score = 0;
switch (dir) {
case UP:
case LEFT: {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
Point sameTile = sameTileFinder(Point{i, j}, dir, *this);
if (sameTile != Point{i, j}) {
upgradeTile(sameTile);
score += (2 * getSquare(Point{i, j})->points);
clearSquare(Point{i, j});
}
}
}
}
break;
case DOWN:
case RIGHT: {
for (int i = height - 1; i >= 0; i--) {
for (int j = width - 1; j >= 0; j--) {
Point sameTile = sameTileFinder(Point{i, j}, dir, *this);
if (sameTile != Point{i, j}) {
upgradeTile(sameTile);
score += (2 * getSquare(Point{i, j})->points);
clearSquare(Point{i, j});
}
}
}
}
}
return score;
}
bool Grid::shiftTile(const Point &dst, const Point &src) {
if (dst == src) {
return false;
}
if (!isEmpty(src)) {
setSquare(dst, getSquare(src));
clearSquare(src);
return true;
}
return false;
}
bool Grid::shiftTiles(Direction dir) {
bool flag = false;
switch (dir) {
case UP:
case LEFT: {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (shiftTile(checkDst(dir, Point{i, j}, *this), Point{i, j})) {
flag = true;
}
}
}
}
break;
case DOWN:
case RIGHT: {
for (int i = height - 1; i >= 0; i--) {
for (int j = width - 1; j >= 0; j--) {
if (shiftTile(checkDst(dir, Point{i, j}, *this), Point{i, j})) {
flag = true;
}
}
}
}
break;
}
return flag;
}
void Grid::printGrid() const {
for (int i = 0; i < height; i++) {
wcout << "|";
for (int j = 0; j < width; j++) {
if (isEmpty({i, j})) {
wcout << setw(TILEWIDTH) << " " << "\t";
} else {
squares[i][j]->printTile();
}
wcout << "|";
}
wcout << endl << endl;
}
}