-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGame.h
271 lines (214 loc) · 7.78 KB
/
Game.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#ifndef GAME_H
#define GAME_H
#include "Grid.h"
#include "EEPROMAnything.h"
class Vertex{
public:
int x,y;
//int index; //int index(){return 10*y+x; };
VertexType type;
Link links[4]; //The elements of this array are the index of other linked vertices stored in order as LRUD (Left, Right, Up, Down)
boolean visited;
Vertex(){
};
static int getIndex(int m,int n){
return m+10*n;
}
static int getX(int k){
return k%10;
}
static int getY(int k){
return k/10;
}
static Orientation dx(int index1,int index2){
return Math::sgn((index1%10)-(index2%10));
}
static Orientation dy(int index1,int index2){
return Math::sgn((index1/10)-(index2/10));
}
};
class Game{
public:
RunMode mode;
Vertex vertex[50];
Checkpoint lastCheckpoint;
int segmentStart;
int completedSegments;
int xOrient, yOrient;
Orientation arrow;
Vertex lastVertex;
unsigned long lastDetectedTime;
float speedFactor; //To decrease speed at some cases
int wetPath[50];
static const int dryPath[18]; //Might be good to make this array not static variable and initialize later, to provide flexibility.
void initializeDryRun(){
initializeVertex();
mode = DRY;
lastCheckpoint = STARTZONE;
segmentStart = Game::dryPath[0];
completedSegments = 0;
lastVertex.x = -1;
lastVertex.y = 0;
xOrient = EAST;
yOrient = NOCHANGE;
};
void game(){
lastDetectedTime = 0;
}
class Path{
int index[20];
int leng;
};
int findShortest(int from, int to){
int leng[50], via[50];
Visit scan[50];
int tempIndex, tempLong, prevFrom;
for(int i=0; i<50; i++){
leng[i] = INF;
via[i] = from;
scan[i] = UNVISITED;
}
leng[from] = 0;
//Serial.println("From:");
do{
//Serial.println(from);
for (int i=0; i<4; i++){ //i = LEFT : 0, RIGHT : 1, UP : 2, DOWN : 3
tempIndex = vertex[from].links[i];
if (vertex[tempIndex].type < NODE){
if (vertex[tempIndex].visited != VISITED && leng[from]+1 < leng[tempIndex]){ //1 is the weight of graph
leng[tempIndex] = leng[from]+1;
via[tempIndex] = from;
}else{
}
}
}
scan[from] = VISITED;
tempLong = INF;
prevFrom = from;
for (int i=0; i<50; i++){
if (scan[i] == UNVISITED && (vertex[i].type < REDPIT || to == i)){
if (leng[i]<tempLong) {
tempLong = leng[i];
from = i;
}
}
}
if (prevFrom == from) break;
}while (from != to);
if (leng[to] == INF) {
//Serial.println("Impossible");
}else{
int lastInd = to;
for (int i=0; i<leng[to]; i++){
lastInd = via[lastInd]; //Track back in Dijkstra Table for shortest Path
wetPath[leng[to]-i-1] = lastInd;
}
wetPath[leng[to]] = to;
//Serial.println("SP");
for (int i=0; i<leng[to]; i++){
//Serial.println(wetPath[i]);
}
}
if (leng[to] == INF) Serial.println("PathLength=INF");
return leng[to];
};
void simulateDryCompletion(){
int t;
arrow = SOUTH;
t = Vertex::getIndex(2, 0);
vertex[t].type = NODE;
t = Vertex::getIndex(7, 0);
vertex[t].type = NODE;
t = Vertex::getIndex(0, 4);
vertex[t].type = BLOCK;
t = Vertex::getIndex(3, 1);
vertex[t].type = NODE;
t = Vertex::getIndex(8, 1);
vertex[t].type = NODE;
t = Vertex::getIndex(1, 2);
vertex[t].type = NODE;
t = Vertex::getIndex(3,2);
vertex[t].type = BLOCK;
t = Vertex::getIndex(9,2);
vertex[t].type = NODE;
};
void initializeVertex(){
int t;
for (int i=0; i<10; i++){
for (int j=0; j<5; j++){
t = Vertex::getIndex(i,j);
vertex[t].x=i;
vertex[t].y=j;
(i!=0 && i!=6 && i!=5 && i!= 4) ? vertex[t].links[LEFT]=Vertex::getIndex(i-1,j) : vertex[t].links[LEFT] = NOPATH; //to set the leftmost and central gap path to nopath
(i!=9 && i!=3 && i!=5 && i!= 4) ? vertex[t].links[RIGHT]=Vertex::getIndex(i+1,j) : vertex[t].links[RIGHT] = NOPATH; //to set the rightmost and central gap path to nopath
(j!=4 && i!=5 && i!= 4) ? vertex[t].links[UP] = Vertex::getIndex(i,j+1) : vertex[t].links[UP] = NOPATH; //to set the uppermost path to nopath
(j!=0 && i!=5 && i!= 4) ? vertex[t].links[DOWN] = Vertex::getIndex(i,j-1) : vertex[t].links[DOWN] = NOPATH; //to set the lowermost path to nopath
vertex[t].type=VERTEX;
vertex[t].visited=UNVISITED;
//vertex[t].index=t;
}
}
t = Vertex::getIndex(0,1);
vertex[t].type = TRANSFERZONE;
t = Vertex::getIndex(0,4);
vertex[t].type = TRANSFERZONE;
//======================================
t = Vertex::getIndex(1,4);
vertex[t].type = REDPIT;
t = Vertex::getIndex(5,4); //between 4 and 5 so selecting the value of coordinate to 5 arbitaryly ... use 4 if u want
vertex[t].type = BLUEPIT;
t = Vertex::getIndex(8,4);
vertex[t].type = PIT3;
//======================================== above this are co ordinate for pits
//========================================below this are coordinate for PHOTOPOINT
t = Vertex::getIndex(0,2);
vertex[t].type = PHOTOPOINT;
t = Vertex::getIndex(6,0);
vertex[t].type = PHOTOPOINT;
t = Vertex::getIndex(6,1);
vertex[t].type = PHOTOPOINT;
t = Vertex::getIndex(6,2);
vertex[t].type = PHOTOPOINT;
//=====================FOR SPECIAL POINTS
t = Vertex::getIndex(2,3);
vertex[t].links[UP] = NOPATH;
t = Vertex::getIndex(2,4);
vertex[t].links[DOWN] = NOPATH;
t = Vertex::getIndex(3,3);
vertex[t].links[UP] = NOPATH;
t = Vertex::getIndex(3,4);
vertex[t].links[DOWN] = NOPATH;
t = Vertex::getIndex(4,4);
vertex[t].links[LEFT] = Vertex::getIndex(3,4);
vertex[t].links[RIGHT] = Vertex::getIndex(5,4);
t = Vertex::getIndex(5,4);
vertex[t].links[LEFT] = Vertex::getIndex(4,4);
vertex[t].links[RIGHT] = Vertex::getIndex(6,4);
t = Vertex::getIndex(6,3);
vertex[t].links[UP] = NOPATH;
t = Vertex::getIndex(7,3);
vertex[t].links[RIGHT] = NOPATH;
vertex[t].links[UP] = NOPATH;
t = Vertex::getIndex(3,4);
vertex[t].links[RIGHT] = Vertex::getIndex(4,4);
t = Vertex::getIndex(8,3);
vertex[t].links[LEFT] = NOPATH;
vertex[t].links[RIGHT] = NOPATH;
t = Vertex::getIndex(9,3);
vertex[t].links[LEFT] = NOPATH;
t = Vertex::getIndex(6,4);
vertex[t].links[LEFT] = Vertex::getIndex(5,4);
vertex[t].links[RIGHT] = Vertex::getIndex(7,4);
vertex[t].links[DOWN] = NOPATH;
t = Vertex::getIndex(7,4);
vertex[t].links[DOWN] = NOPATH;
vertex[t].links[LEFT] = NOPATH;
vertex[t].links[RIGHT] = NOPATH;
//TODO --> The Links of following vertices depends upon direction of inclination of See-Saw
//#Review
t = Vertex::getIndex(8,2); vertex[t].links[UP] = NOPATH;
t = Vertex::getIndex(8,4); vertex[t].links[DOWN] = Vertex::getIndex(8,3);
}; //end of initialize
};
const int Game::dryPath[18]= {0,3,13,10,20,23,33,30,40,49,9,8,28,27,7,6,36,37};
#endif