-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
332 lines (279 loc) · 6.98 KB
/
Copy pathscript.js
File metadata and controls
332 lines (279 loc) · 6.98 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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
///////////////
/// VARIABLES
///////////////
// Enumerations equivalent
var UP=1, DOWN=2, LEFT=4, RIGHT=8;
// Labyrinth description
var tab_labyrinth;
// Checked cases are cases the mouses has already been (true or false)
var tab_case_checked = new Array();
// Array containing all td labyrinth's td elements
var td_elements = document.getElementsByTagName('td');
// Mouse and cheese's positions
var pos_x, cheese_x;
var pos_y, cheese_y;
// Labyrinthe's size
var size_x;
var size_y;
// history of moves
var stack = new Array();
/////////////////////////////////
/// VARIABLES INITIALISATIONS
/////////////////////////////////
tab_labyrinth =
[
[UP+LEFT+DOWN, UP+DOWN, UP+DOWN, UP+DOWN, UP+DOWN, UP+DOWN, UP+RIGHT],
[UP+LEFT, UP+RIGHT, UP+LEFT, UP, UP+DOWN, UP+DOWN, DOWN+RIGHT],
[LEFT+RIGHT, LEFT+RIGHT, LEFT+RIGHT, LEFT+DOWN, UP+DOWN, UP+DOWN, UP+RIGHT],
[LEFT+RIGHT, LEFT+RIGHT, LEFT+RIGHT, LEFT+UP, UP, UP+DOWN, DOWN+RIGHT],
[LEFT+RIGHT+DOWN, LEFT+DOWN, RIGHT, LEFT+DOWN+RIGHT,DOWN+LEFT, UP+DOWN, UP+DOWN+RIGHT],
[LEFT+UP, UP+DOWN, DOWN, UP+DOWN, UP+RIGHT, UP+LEFT, UP+RIGHT],
[LEFT+RIGHT, UP+LEFT+RIGHT, LEFT+UP, UP+RIGHT, LEFT+RIGHT, LEFT+RIGHT, LEFT+RIGHT],
[LEFT+RIGHT, LEFT+RIGHT, LEFT+RIGHT, LEFT+RIGHT, LEFT+RIGHT, LEFT+RIGHT, LEFT+RIGHT],
[LEFT+RIGHT+DOWN, LEFT+DOWN, DOWN+RIGHT, LEFT+DOWN, DOWN, DOWN+RIGHT, DOWN+LEFT+RIGHT]
];
size_x = tab_labyrinth[0].length;
size_y = tab_labyrinth.length;
// Displays the labyrinth's borders (walls)
for (var y=0; y < size_y ; y++)
{
for (var x=0; x < size_x ; x++)
{
setCaseBorders(x, y, td_elements[x+y*(size_y-2)]);
}
}
// Fills the checked cases with false statement
for (var i=0; i < size_x*size_y ; i++)
{
tab_case_checked[i] = false;
}
// Initialisation of the positions
placeCheese();
placeMouse();
///////////////
/// FUNCTIONS
///////////////
// Main function
function seek_cheese()
{
if (success())
{
document.getElementById('txt_success').style.visibility = "visible";
return;
}
var ok = false;
check_position();
for (var dir=1; dir<=8 && ! ok ; dir*=2) // UP, DOWN, LEFT then RIGHT
{
if (move(dir))
{
if (case_checked()) // If case already visited...
move(opposit_direction(dir)); // ...cancel the move
else
{
ok = true;
stack.push(dir)
if (success())
{
document.getElementById('txt_success').style.visibility = "visible";
}
else
{
check_position();
}
}
}
}
if(! ok)
move(opposit_direction(stack.pop()));
}
// Returns an integer between a et b
function rand(a, b)
{
return Math.floor( Math.random() * (b-a+1)) + a;
}
// Set the classes 'u' 'd' 'l' 'r' to each case for having the borders
function setCaseBorders(x, y, td_element)
{
td_element.setAttribute("class","");
var case_border = tab_labyrinth[y][x];
if (case_border - RIGHT >= 0)
{
td_element.className += 'r ';
case_border -= RIGHT;
}
if (case_border - LEFT >= 0)
{
td_element.className += 'l ';
case_border -= LEFT;
}
if (case_border - DOWN >= 0)
{
td_element.className += 'd ';
case_border -= DOWN;
}
if (case_border - UP >= 0)
{
td_element.className += 'u ';
case_border -= UP;
}
}
// Moves the mouse if possible (returns true when success, else false)
function move(direction)
{
switch (direction)
{
case UP:
if (wall(UP))
return false;
else
pos_y--;
break;
case DOWN:
if (wall(DOWN))
return false;
else
pos_y++;
break;
case LEFT:
if (wall(LEFT))
return false;
else
pos_x--;
break;
case RIGHT:
if (wall(RIGHT))
return false;
else
pos_x++;
break;
}
refresh();
return true;
}
// Returns if the actual case has already been visited (true/false)
function case_checked()
{
return tab_case_checked[pos_x+pos_y*size_x];
}
// Returns if there's a wall at the actual position in the paramater's direction
function wall(direction)
{
// Labyrinth's extremes
if ( (pos_x == 0 && direction == LEFT)
|| (pos_y == 0 && direction == UP)
|| (pos_x == size_x-1 && direction == RIGHT)
|| (pos_y == size_y-1 && direction == DOWN))
{
return true; // Extremity = wall
}
var position_now = tab_labyrinth[pos_y][pos_x];
if (position_now - RIGHT >= 0)
{
position_now -= RIGHT;
if (direction == RIGHT)
return true;
}
if (position_now - LEFT >= 0)
{
position_now -= LEFT;
if (direction == LEFT)
return true;
}
if (position_now - DOWN >= 0)
{
position_now -= DOWN;
if (direction == DOWN)
return true;
}
if (position_now - UP >= 0)
{
if (direction == UP)
return true;
}
return false;
}
// Places the cheese at random position in the labyrinth
function placeCheese()
{
cheese_x = rand(0,size_x-1);
cheese_y = rand(0,size_y-1);
td_elements[cheese_x+cheese_y*size_x].setAttribute("id","cheese");
}
// Places the mouse at random position in the labyrinth
function placeMouse()
{
do
{
pos_x = rand(0,size_x-1);
pos_y = rand(0,size_y-1);
} while (pos_x == cheese_x && pos_y == cheese_y);
td_elements[pos_x+pos_y*size_x].setAttribute("id","mouse");
}
// Returns if the mouse's position equals the cheese's position (true/false)
function success()
{
if (pos_x == cheese_x && pos_y == cheese_y)
return true;
else
return false;
}
// Returns the opposit direction
function opposit_direction(direction)
{
switch (direction)
{
case UP:
return DOWN;
case DOWN:
return UP;
case RIGHT:
return LEFT;
case LEFT:
return RIGHT;
}
}
// Refresh the HTML properties (class, id) for having the screen updated
function refresh()
{
for (var y=0; y < size_y ; y++)
{
for (var x=0; x < size_x ; x++)
{
var elem = td_elements[x+y*size_x];
if (x == pos_x && y == pos_y)
elem.setAttribute("id","mouse"); // Mouse
else if (x == cheese_x && y == cheese_y)
elem.setAttribute("id","cheese"); // Cheese
else
elem.removeAttribute("id");
// Adds or takes away the '.checked' class
if (elem.className.indexOf('checked ') != -1 && tab_case_checked[x+y*size_x] == false)
{
elem.className = elem.className.replace('checked ','');
}
else if (elem.className.indexOf('checked ') == -1 && tab_case_checked[x+y*size_x] == true)
{
elem.className += 'checked ';
}
}
}
}
// Checks the actual position as visited
function check_position()
{
tab_case_checked[pos_x+pos_y*size_x] = true;
if (td_elements[pos_x+pos_y*size_x].className.indexOf('checked ') == -1)
td_elements[pos_x+pos_y*size_x].className += "checked ";
}
// Reset the checked positions
function reset_check()
{
for (var i=0; i < size_x*size_y ; i++)
{
tab_case_checked[i] = false;
}
refresh();
// Hides the success text
document.getElementById('txt_success').style.visibility = "hidden";
}