-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecoder.js
332 lines (287 loc) · 12 KB
/
Decoder.js
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
var rows_columns = 3;
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
// Handle keyboard controls
var key = 0;
ctx.fillStyle = "#660066";
ctx.font = "24px Arial";
ctx.textAlign = "left";
ctx.textBaseline = "top";
ctx.fillText("Press Enter to play", c.width - c.width/1.75, c.height - c.height/1.75);
var gameState = "start";
var game = setup(rows_columns);
addEventListener("keyup", function (e) { //event listener for arrow keys
key = e.keyCode;
gameState = update(game,key); //updates the game and renders every time a key is pressed.
render(game);
if (gameState == "win") { //win state handler
if (rows_columns == 8) {
alert("YOU WON THE WHOLE GAME!!! NEW GAME+!");
rows_columns = 2;
}
alert("Next Level!!");
reveal(game);
render(game);
rows_columns++;
game = setup(rows_columns);
ctx.fillStyle = "#000000";
ctx.font = "24px Arial";
ctx.textAlign = "left";
ctx.textBaseline = "top";
ctx.fillText("Press Enter to go to the next level", c.width - c.width/1.75, c.height - c.height/1.75);
}
if (gameState == "lose") { //lose state handler
alert("You Made it through " + (rows_columns-3) + " levels");
reveal(game);
render(game);
rows_columns = 3;
game = setup(rows_columns);
}
}, false);
//setup function creates the game object along with all of its characteristics.
function setup(rows_columns) {
//initilize lives
var lives = Math.ceil(rows_columns/2 + 1);
//create empty board
var board = new Array(rows_columns);
for (var i = 0; i < rows_columns; i++) {
board[i] = new Array(rows_columns);
for (var j = 0; j < rows_columns; j++) {
board[i][j] = {visibility: false, value: "empty"};
}
}
//create empty path and code
var code = new Array(rows_columns*2);
var path = new Array(rows_columns*2);
//create random start location
var x = Math.round(Math.random()*(rows_columns-1));
var y = Math.round(Math.random()*(rows_columns-1));
//update board and path
board[x][y].value = "start";
path[0] = {x: x,y: y};
//create a random path
for (i = 1; i < code.length; i++) {
//utilize the smartDirections function to create a smart path.
var AI = smartDirections(x,y,path,rows_columns);
//choose at random one of the valid directions
var next = Math.floor(Math.random()*AI.validDirections.length);
//update game objects
code[i] = AI.validDirections[next];
x = AI.validX[next];
y = AI.validY[next];
board[x][y].value = "path";
path[i] = {x: x,y: y};
}
//place end tile.
board[x][y].value = "end";
//initilize player at the start of the path.
var player = {x: path[0].x, y: path[0].y};
//create and shuffle emojis array
var emojis = [0x1F422,0x1F420,0x1F40C,0x1F32F,0x1F980,0x1F34A,0x1F43C,0x1F413,0x1F427,0x1F419,0x1F41E,0x1F346,0x1F423];
var encoding = shuffle(emojis);
//create game object and return it.
var game = {code: code,board: board,player: player,encoding: encoding, rows_columns: rows_columns, lives: lives, emojis:emojis};
return game;
}
function shuffle(array) { //standard fisher yates shuffle technique for the emojis.
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
//used to create a smart path
function smartDirections(x,y,path,rows_columns) {
//initilize possible directions
var Directions = ["west","north","east","south"];
var smartDirections = ["west","north","east","south"];
//initilize possible coords.
var possibleX = [x-1,x,x+1,x];
var possibleY = [y,y-1,y,y+1];
//check all possible directions
for (var i = 0; i < 4; i++) {
//out of bounds check.
if (possibleX[i] < 0 || possibleX[i] >= rows_columns || possibleY[i] < 0 || possibleY[i] >= rows_columns) {
delete Directions[i];
delete smartDirections[i];
}
//already on path check
for (var j = 0; j < path.length; j++) {
if (path[j] !== null) {
if (possibleX[i] == path[j].x && possibleY[i] == path[j].y) {
delete smartDirections[i];
}
}
}
}
var validDirections = [];
var validX = [];
var validY = [];
//create valid arrays
for (i = 0; i < 4; i++) {
if (smartDirections[i] !== null) {
validDirections.push(smartDirections[i]);
validX.push(possibleX[i]);
validY.push(possibleY[i]);
}
}
if (validDirections.length === 0) {
validDirections = [];
for (i = 0; i < 4; i++) {
if (Directions[i] !== null) {
validDirections.push(Directions[i]);
validX.push(possibleX[i]);
validY.push(possibleY[i]);
}
}
}
//return the valid directions.
return {validDirections: validDirections, validX: validX, validY: validY};
}
function render(game) { //rendering function updates graphics
ctx.clearRect(0, 0, c.width, c.height);
game.board[game.player.x][game.player.y].visibility = true;
ctx.fillStyle = "#ffff66";
ctx.strokeStyle = "#663300";
for (var w = 0; w < c.height; w += c.height/game.rows_columns) { //draw the empty tiles
for (var l = 0; l < c.height; l += c.height/game.rows_columns) {
ctx.strokeRect(w, l, Math.round(c.height/game.rows_columns), Math.round(c.height/game.rows_columns));
ctx.fillRect(w+1, l+1, Math.round(c.height/game.rows_columns-1), Math.round(c.height/game.rows_columns-1));
}
}
//draw the compass
ctx.fillStyle = "#00FF00";
ctx.font = "36px Arial";
ctx.textAlign = "left";
ctx.textBaseline = "top";
ctx.fillText("N", c.width - 250, 0);
ctx.fillText("W", c.width - 290, 30);
ctx.fillText("E", c.width - 220, 30);
ctx.fillText("S", c.width - 250, 60);
//draw start symbol
ctx.fillStyle = "#00ff00";
ctx.fillRect(c.width - 430, 0, 20, 20);
//draw end symbol
ctx.fillStyle = "#990099";
ctx.fillRect(c.width - 430, game.code.length*30, 20, 20);
//draw the coded path.
for (var i = 1; i < game.code.length; i++) {
ctx.fillStyle = "#ff0f00";
ctx.font = "24px Arial";
ctx.textAlign = "left";
ctx.textBaseline = "top";
if (game.code[i] == "west") {
ctx.fillText(String.fromCodePoint(game.encoding[0]), c.width - 430, i*30);
} else if (game.code[i] == "north") {
ctx.fillText(String.fromCodePoint(game.encoding[1]), c.width - 430, i*30);
} else if (game.code[i] == "east") {
ctx.fillText(String.fromCodePoint(game.encoding[2]), c.width - 430, i*30);
} else if (game.code[i] == "south") {
ctx.fillText(String.fromCodePoint(game.encoding[3]), c.width - 430, i*30);
}
}
//draw the
ctx.fillText(String.fromCodePoint(game.emojis[0]), c.width - 435, 610);
ctx.fillText(String.fromCodePoint(game.emojis[1]), c.width - 400, 610);
ctx.fillText(String.fromCodePoint(game.emojis[2]), c.width - 365, 610);
ctx.fillText(String.fromCodePoint(game.emojis[3]), c.width - 330, 610);
for (var x = 0; x < game.rows_columns; x++) {
for (var y = 0; y < game.rows_columns; y++) {
if (game.board[x][y].visibility) {
if (game.board[x][y].value == "path") {
ctx.fillStyle = "#996633";
ctx.fillRect((x*c.height/game.rows_columns + c.height/(game.rows_columns*4)), (y*c.height/game.rows_columns + c.height/(game.rows_columns*4)), c.height/(game.rows_columns*2), c.height/(game.rows_columns*2));
}
if (game.board[x][y].value == "end") {
ctx.fillStyle = "#990099";
ctx.fillRect((x*c.height/game.rows_columns + c.height/(game.rows_columns*4)), (y*c.height/game.rows_columns + c.height/(game.rows_columns*4)), c.height/(game.rows_columns*2), c.height/(game.rows_columns*2));
}
if (game.board[x][y].value == "start") {
ctx.fillStyle = "#00ff00";
ctx.fillRect((x*c.height/game.rows_columns + c.height/(game.rows_columns*4)), (y*c.height/game.rows_columns + c.height/(game.rows_columns*4)), c.height/(game.rows_columns*2), c.height/(game.rows_columns*2));
}
if (game.board[x][y].value == "empty") {
ctx.fillStyle = "#ff0000";
ctx.fillRect((x*c.height/game.rows_columns + c.height/(game.rows_columns*4)), (y*c.height/game.rows_columns + c.height/(game.rows_columns*4)), c.height/(game.rows_columns*2), c.height/(game.rows_columns*2));
}
}
}
}
ctx.fillStyle = "#00fff0";
ctx.fillRect((game.player.x*c.height/game.rows_columns + c.height/((game.rows_columns-1)*3)), (game.player.y*c.height/game.rows_columns + c.height/((game.rows_columns-1)*3)), c.height/(game.rows_columns*4), c.height/(game.rows_columns*4));
ctx.fillStyle = "#0000ff";
ctx.font = "24px Arial";
ctx.textAlign = "left";
ctx.textBaseline = "top";
ctx.fillText("lives: " + game.lives, c.width - 100, 0);
}
function update(game,key) { //updates player lives and position.
var nextMove = {x: game.player.x, y: game.player.y};
if (game.board[game.player.x][game.player.y].value == "end") {
return "win";
} else if(game.lives === 0) {
return "lose";
} else {
if (key == 37) {
nextMove.x = game.player.x-1;
if (validMove(game, nextMove)) {
game.player.x--;
}
key = 0;
}
else if (key == 38) {
nextMove.y = game.player.y-1;
if (validMove(game, nextMove)) {
game.player.y--;
}
key = 0;
}
else if (key == 39) {
nextMove.x = game.player.x+1;
if (validMove(game, nextMove)) {
game.player.x++;
game.points++;
}
key = 0;
}
else if (key == 40) {
nextMove.y = game.player.y+1;
if (validMove(game, nextMove)) {
game.player.y++;
}
key = 0;
} else {
return "playing";
}
}
}
function validMove(game, nextMove) { //valid move function used in update function.
if (nextMove.x > (game.rows_columns-1) || nextMove.x < 0 || nextMove.y > (game.rows_columns-1) || nextMove.y < 0) {
return false;
}
if (!game.board[nextMove.x][nextMove.y].visibility) {
game.board[nextMove.x][nextMove.y].visibility = true;
} else if (game.board[nextMove.x][nextMove.y].value == "empty") {
return false;
}
if (game.board[nextMove.x][nextMove.y].value != "empty") {
return true;
} else {
game.lives--;
return false;
}
}
function reveal(game) {
for (var i = 0; i < game.rows_columns; i++) {
for (var j = 0; j < game.rows_columns; j++) {
game.board[i][j].visibility = true;
}
}
}