-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.pde
167 lines (151 loc) · 4 KB
/
helpers.pde
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
int convert2Dto1D(PVector v){
return int(v.x + v.y * BOARD_COLUMNS);
}
PVector convert1Dto2D(int v){
return new PVector(v % BOARD_COLUMNS, v / BOARD_COLUMNS);
}
Candy getCandy(int position, Candy[][] b){
if(position >= 0 && position < BOARD_COLUMNS * BOARD_ROWS){
PVector pos = convert1Dto2D(position);
return b[int(pos.y)][int(pos.x)];
}
return null;
}
Candy getCandy(int gridX, int gridY, Candy[][] b){
if(gridX < BOARD_COLUMNS && gridX >= 0 && gridY < BOARD_ROWS && gridY >= 0){
return b[gridY][gridX];
}
return null;
}
int offset(int pos, String direction, int WIDTH){
switch(direction){
case "North":
return pos - WIDTH;
case "South":
return pos + WIDTH;
case "East":
return pos - 1;
case "West":
return pos + 1;
default:
return 0;
}
}
PVector offset(int px, int py, String direction){
switch(direction){
case "North":
return new PVector(px,px-1);
case "South":
return new PVector(px,py+1);
case "East":
return new PVector(px-1,py);
case "West":
return new PVector(px+1,py);
default:
return new PVector();
}
}
float interpolation(float st, float end, float percentage){
return (st + (end - st) * percentage);
}
float flip(float percentage){
return 1 - percentage;
}
float easeIn(float x){
return x * x;
}
float easeOut(float x){
return flip(easeIn(flip(x)));
}
class Pair{
int first;
int second;
Pair(int x, int y){
first = x;
second = y;
}
}
boolean isValid(Candy[][] candys, int m, int n, int x, int y){
if(x<0 || y < 0 || x >= m || y >= n){
return false;
}
return true;
}
boolean isSameType(Candy[][] candys, int x, int y, CANDYTYPES type ){
if (isValid(candys,BOARD_COLUMNS,BOARD_ROWS,x,y)){
if (type == candys[y][x].m_type){
return true;
}
return false;
}
return false;
}
ArrayList<Pair> floodFill(Candy[][] candys, int m, int n, int x, int y, CANDYTYPES type){
ArrayList<Pair> queue = new ArrayList<Pair>();
ArrayList<Pair> checked = new ArrayList<Pair>();
Pair p = new Pair(x,y);
queue.add(p);
while(queue.size() > 0){
Pair current = queue.get(queue.size()-1);
queue.remove(queue.size()-1);
int posX = current.first;
int posY = current.second;
if(isValid(candys,m,n, posX + 1, posY) && isSameType(candys,posX + 1, posY,type)){
boolean isIn = false;
for(Pair t : checked){
if(t.first == posX+1 && t.second == posY){
isIn = true;
}
}
if(isIn){
}
else{
queue.add(new Pair(posX+1,posY));
checked.add(new Pair(posX+1,posY));
}
}
if(isValid(candys,m,n, posX - 1, posY) && isSameType(candys,posX - 1, posY,type)){
boolean isIn = false;
for(Pair t : checked){
if(t.first == posX-1 && t.second == posY){
isIn = true;
}
}
if(isIn){
}
else{
queue.add(new Pair(posX-1,posY));
checked.add(new Pair(posX-1,posY));
}
}
if(isValid(candys,m,n, posX, posY+1) && isSameType(candys,posX, posY+1,type)){
boolean isIn = false;
for(Pair t : checked){
if(t.first == posX && t.second == posY+1){
isIn = true;
}
}
if(isIn){
}
else{
queue.add(new Pair(posX,posY+1));
checked.add(new Pair(posX,posY+1));
}
}
if(isValid(candys,m,n, posX, posY-1) && isSameType(candys,posX, posY-1,type)){
boolean isIn = false;
for(Pair t : checked){
if(t.first == posX && t.second == posY-1){
isIn = true;
}
}
if(isIn){
}
else{
queue.add(new Pair(posX,posY-1));
checked.add(new Pair(posX,posY-1));
}
}
}
return checked;
}