-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.h
129 lines (106 loc) · 4.09 KB
/
grid.h
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
#include <iostream>
using namespace std;
class Grid{
private:
Element* matrix[GRID_WIDTH][GRID_HEIGHT]; // Array of pointers, pointing to Elements
public:
void reset(){
for (int x = 0; x < GRID_WIDTH; x++){
for (int y = 0; y < GRID_HEIGHT; y++){
reset(x, y);
}
}
}
void reset(int x, int y){
delete matrix[x][y];
matrix[x][y] = nullptr;
}
void resetPtr(){
for (int x = 0; x < GRID_WIDTH; x++){
for (int y = 0; y < GRID_HEIGHT; y++){
resetPtr(x, y);
}
}
}
void resetPtr(int x, int y){
matrix[x][y] = nullptr;
}
void set(int x, int y, Element *elementPtr){
delete matrix[x][y];
matrix[x][y] = elementPtr;
}
Element get(int x, int y){
return *matrix[x][y];
}
Element* getPtr(int x, int y){
return matrix[x][y];
}
// element should not be at the same position as (x, y)
void move(Element *elementPtr, int x, int y){
set(x, y, elementPtr);
resetPtr(elementPtr->x, elementPtr->y); // delete pointer of current position
matrix[x][y]->setX(x);
matrix[x][y]->setY(y);
}
void moveTo(Element *element, int x, int y){
int xPos = element->x;
int yPos = element->y;
int startX = xPos;
int startY = yPos;
int yDirection = 1;
if (y < yPos){yDirection = -1;}
if (x == xPos){ // Moving vertically
for (int i = 0; i < abs(y - startY); i++){
if (!inBounds(xPos, yPos + yDirection) || (isFull(xPos, yPos + yDirection) && get(xPos, yPos + yDirection).state != "liquid")){break;}
yPos += yDirection;
}
}
else { // Not moving vertically
float gradient = (float)(y - yPos) / (float)(x - xPos);
if (abs(gradient) > 1){
gradient = 1 / gradient;
for (int i = 1; i <= abs(y - startY); i++){
int newX = startX + round(i * gradient);
if (!inBounds(newX, yPos + yDirection) || (isFull(newX, yPos + yDirection) && get(newX, yPos + yDirection).state != "liquid")){break;}
xPos = newX;
yPos += yDirection;
}
}
else {
int xDirection = 1;
if (x < xPos){xDirection = -1;}
for (int i = 1; i <= abs(x - startX); i++){
int newY = startY + round(i * gradient);
if (!inBounds(xPos + xDirection, newY) || (isFull(xPos + xDirection, newY) && get(xPos + xDirection, newY).state != "liquid")){break;}
xPos += xDirection;
yPos = newY;
}
}
}
if (xPos != element->x || yPos != element->y){ // If the move positions have changed, move there
if (isFull(xPos, yPos)){
swap(element, xPos, yPos);
}else{
move(element, xPos, yPos);
}
}
}
void swap(Element *element, int x, int y){
Element* swappedElement = getPtr(x, y);
matrix[x][y] = matrix[element->x][element->y];
matrix[element->x][element->y] = swappedElement;
matrix[element->x][element->y]->setX(element->x);
matrix[element->x][element->y]->setY(element->y);
matrix[x][y]->setX(x);
matrix[x][y]->setY(y);
}
bool isFull(int x, int y){
return (bool) matrix[x][y];
}
bool isEmpty(int x, int y){
return !isFull(x, y);
}
bool inBounds(int x, int y){
return x >= 0 && y >=0 && x < GRID_WIDTH && y < GRID_HEIGHT;
}
};