-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
554 lines (465 loc) · 16 KB
/
index.html
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/phaser@3.15.1/dist/phaser-arcade-physics.min.js"></script>
<script src="color_lock.js"></script>
<script src="constants.js"></script>
<link rel="stylesheet" href="stylesheet.css">
</head>
<body>
<div class="game-view">
<div class="game-info">
<div>
Optimal solution:
</div>
<div id="optimal-solution-number">
LOADING...
</div>
<div>
Current attempt:
</div>
<div id="number-of-attempts">
LOADING...
</div>
<div>
<br>
KEYBINDS
<br>
R - Restarts current game
<br>
Space - New game
<br>
S - Show best move
</div>
</div>
<div id="game"></div>
</div>
<script>
var config = {
type: Phaser.AUTO,
width: WIDTH,
height: HEIGHT,
scale: {parent:"game"},
parent: "game",
scene: {
preload: preload,
create: create,
update: update
}
};
var game = new Phaser.Game(config);
let colorLock = new ColorLock();
function preload ()
{
const SIZES = ["big", "small"];
const COLORS = ["green", "yellow", "blue", "red", "blank"];
const VERTICAL_ORIENTATIONS = ["top", "bottom"];
const HORIZONTAL_ORIENTATIONS = ["left", "right"];
const IMAGES_DIR = "assets/";
const PNG_EXTENSION = ".png";
let color_images = []
for (let i = 0; i < SIZES.length; i++)
{
for (let j = 0; j < COLORS.length; j++)
{
for (let k = 0; k < VERTICAL_ORIENTATIONS.length; k++)
{
for (let l = 0; l < HORIZONTAL_ORIENTATIONS.length; l++)
{
imageName = SIZES[i] + "_" + COLORS[j] + "_" + VERTICAL_ORIENTATIONS[k] + "_" + HORIZONTAL_ORIENTATIONS[l];
imagePath = IMAGES_DIR + imageName + PNG_EXTENSION;
this.load.image(imageName, imagePath);
}
}
}
};
this.load.image("base", IMAGES_DIR + "base" + PNG_EXTENSION);
this.load.image("open", IMAGES_DIR + "open_lock" + PNG_EXTENSION);
}
function colorTranslator(color)
{
switch (color)
{
case 0:
return "blank";
case 1:
return "green";
case 2:
return "yellow";
case 3:
return "blue";
case 4:
return "red";
default:
return undefined;
}
}
function getImageName(radialCoord, angularCoord, color)
{
let name = "";
if (radialCoord == 0)
{
name += "small_";
}
else
{
name += "big_";
}
name += colorTranslator(color) + "_";
if (angularCoord == 0 || angularCoord == 3)
{
name += "top_";
}
else
{
name += "bottom_";
}
if (angularCoord == 0 || angularCoord == 1)
{
name += "left";
}
else
{
name += "right";
}
return name;
}
function renderColorLock(grid, game)
{
imageController["open"].destroy();
for (let radialCoord = 0; radialCoord < 2; radialCoord++)
{
for (let angularCoord = 0; angularCoord < 4; angularCoord++)
{
coordString = JSON.stringify([radialCoord, angularCoord]);
if (!firstTimeLoad)
{
imageController[coordString].destroy();
}
let color = grid[radialCoord][angularCoord];
imageController[coordString] = game.add.image(0, 0, getImageName(radialCoord, angularCoord, color)).setOrigin(0, 0).setScale(IMAGES_SCALE);
}
}
if (firstTimeLoad)
{
firstTimeLoad = false;
}
}
// Angles must be in radians and as an array [angle1, angle2]
function pointInArc(point, centerX, centerY, minRadius, maxRadius, angles)
{
let x = point[0];
let y = point[1];
// Write cartesian with origin in the center of the color lock
x = x - centerX;
y = centerY - y;
// Convert to polar
let radius = Math.sqrt(x**2 + y**2);
// x = 0 means that we are in the exact center, which isn't part of any arc, and it would give a division error
// So I'm exiting out earlier
if (x == 0)
{
return false;
}
let angle = Math.atan(y / x);
// Fixing calculator output
if (x < 0)
{
angle += Math.PI;
}
else if (y < 0)
{
angle += 2 * Math.PI;
}
let startAngle = angles[0];
let endAngle = angles[1];
return (radius > minRadius && radius < maxRadius && angle > startAngle && angle < endAngle);
}
// Function detects the angular coordinate and gives an array of angles corresponding to
// the start and end angles of that "quadrant"
function getAnglesFromAngularCoord(angularCoord)
{
switch (angularCoord)
{
case 0:
return [Math.PI / 2, Math.PI];
case 1:
return [Math.PI, 3 * Math.PI / 2];
case 2:
return [3 * Math.PI / 2, 2 * Math.PI];
case 3:
return [0, Math.PI / 2];
default:
return undefined;
}
}
// This function takes as an input a coordinate pair [x, y]
// And returns the coordinate of the slot this position correspond to
// By coordinate of the slot, I mean in the color lock board, like [0, 0] is the inner circle top left
function getSlotCoordinate(point)
{
for (let radialCoord = 0; radialCoord < 2; radialCoord++)
{
for (let angularCoord = 0; angularCoord < 4; angularCoord++)
{
let minRadius, maxRadius;
if (radialCoord === 1)
{
minRadius = BIG_SLOT_MIN_RADIUS;
maxRadius = BIG_SLOT_MAX_RADIUS;
}
else
{
minRadius = SMALL_SLOT_MIN_RADIUS;
maxRadius = SMALL_SLOT_MAX_RADIUS;
}
if (pointInArc(point, BOARD_CENTER_WIDTH, BOARD_CENTER_HEIGHT, minRadius, maxRadius, getAnglesFromAngularCoord(angularCoord)))
{
return [radialCoord, angularCoord];
}
}
}
return undefined;
}
function isInClickableArea(point)
{
return pointInArc(point, BOARD_CENTER_WIDTH, BOARD_CENTER_HEIGHT, SMALL_SLOT_MIN_RADIUS, BIG_SLOT_MAX_RADIUS, [0, 2 * Math.PI]);
}
function updateColorLock(clickedPosition, targetPosition, colorClicked, game, lock)
{
// First destroy old ones to avoid lag
clickedString = JSON.stringify(clickedPosition);
targetString = JSON.stringify(targetPosition);
imageController[clickedString].destroy();
imageController[targetString] = game.add.image(0, 0, getImageName(targetPosition[0], targetPosition[1], colorClicked)).setOrigin(0, 0).setScale(IMAGES_SCALE);
if (showSolutionMode)
{
destroySolutionCircle();
drawSolutionCircle(game, lock.grid);
}
}
function updateOpenLock(game)
{
imageController["open"] = game.add.image(0, 0, "open").setOrigin(0, 0).setScale(IMAGES_SCALE);
destroySolutionCircle();
}
function resetLock(game)
{
colorLock = new ColorLock();
renderColorLock(colorLock.grid, game)
}
// Loading the solutions
let solutions = {};
let keyR;
let keySpace;
let keyS;
let solutionCircle;
let solutionEmptyCircle;
let solutionOuterRing;
let firstTimeLoad = true;
let imageController = {};
function create ()
{
fetch('solutions.tsv')
.then(response => response.text())
.then(text =>
{
let rows = text.split('\n');
for (let i = 0; i < rows.length; i++)
{
cols = rows[i].split('\t');
solutions[cols[0]] = cols[1];
}
});
this.add.image(0, 0, "base").setOrigin(0, 0).setScale(IMAGES_SCALE);
imageController["open"] = this.add.image(0, 0, "open").setOrigin(0, 0).setScale(IMAGES_SCALE);
imageController["open"].destroy();
renderColorLock(colorLock.grid, this);
let grid = colorLock.grid;
keyR = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.R);
keySpace = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE);
keyS = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.S);
}
// Initialize variables for Update
let mouseClicked = false;
let currentlyWinning = false;
let pressedR = false;
let pressedSpace = false;
let pressedS = false;
let showSolutionMode = false;
function drawSolutionCircle(game, grid)
{
// This statement will stop the circle from being rendered if the game is over
if (JSON.stringify(grid) == JSON.stringify([[0, 2, 3, 4], [1, 2, 3, 4]]))
{
showSolutionMode = false;
return;
}
// Give shape to circle
solutionCircle = game.add.graphics();
solutionCircle.fillStyle(0x00ff00, 0.5);
solutionOuterRing = game.add.graphics();
solutionOuterRing.lineStyle(4, 0xffffff, 1);
let radius;
let move = JSON.parse(solutions[JSON.stringify(grid)])[0];
// Set radius based on the radial coordinate
if (move[0] === 0)
{
radius = (LOCK_CIRCLE_LEFT - INNER_LEFT_EDGE_WIDTH);
distance = SMALL_SLOT_MIN_RADIUS + radius/2;
}
else
{
radius = (INNER_LEFT_EDGE_WIDTH - BOARD_LEFT_EDGE_WIDTH);
distance = BIG_SLOT_MIN_RADIUS + radius/2;
}
// Set angle
let angle;
switch (move[1])
{
case 0:
angle = 3 * Math.PI / 4;
break;
case 1:
angle = 5 * Math.PI / 4;
break;
case 2:
angle = 7 * Math.PI / 4;
break;
case 3:
angle = Math.PI / 4;
break;
default:
angle = 0;
break;
}
x = BOARD_CENTER_WIDTH + distance * Math.cos(angle);
y = BOARD_CENTER_HEIGHT - distance * Math.sin(angle);
//solutionOuterRing.fillCircle(x, y, 80);
solutionOuterRing.beginPath();
solutionOuterRing.arc(x, y, radius/4, 0, Math.PI * 2);
solutionOuterRing.closePath();
solutionOuterRing.strokePath();
solutionOuterRing.beginPath();
solutionOuterRing.arc(x, y, radius/6, 0, Math.PI * 2);
solutionOuterRing.closePath();
solutionOuterRing.strokePath();
solutionCircle.fillCircle(x, y, radius/3);
}
function destroySolutionCircle()
{
solutionCircle.destroy();
solutionOuterRing.destroy();
solutionOuterRing.destroy();
}
function update()
{
let optimalSolutionsDiv = document.getElementById("optimal-solution-number");
let totalAttemptsDiv = document.getElementById("number-of-attempts");
// This is not very efficient, loading big dictionary every time, fix later
if (solutions !== {})
{
let JSONstring;
// Need to catch it because for a split second it gets undefined
try
{
JSONstring = JSON.parse(solutions[JSON.stringify(colorLock.startingPosition)]);
optimalSolutionsDiv.innerText = JSONstring.length + " moves";
}
catch
{
JSONstring = "Error";
}
}
//optimalSolutionsDiv.innerText = JSON.parse(solutions[JSON.stringify(colorLock.startingPosition)]);
totalAttemptsDiv.innerText = colorLock.totalMoves + " moves";
if (keyR.isDown && !pressedR)
{
colorLock.restartGrid();
renderColorLock(colorLock.grid, this)
pressedR = true;
}
if (!keyR.isDown)
{
pressedR = false;
}
if (keySpace.isDown && !pressedSpace)
{
// Putting restart grid simply to avoid wincondition
colorLock.restartGrid()
resetLock(this);
pressedSpace = true;
}
if (!keySpace.isDown)
{
pressedSpace = false;
}
if (keyS.isDown && !pressedS )
{
// change mode
showSolutionMode = ! showSolutionMode;
// Need to remove the circle from the screen
if (!showSolutionMode)
{
destroySolutionCircle();
}
else
{
drawSolutionCircle(this, colorLock.grid);
}
pressedS = true;
}
if (!keyS.isDown)
{
pressedS = false;
}
if (colorLock.isWinCondition() && !currentlyWinning)
{
currentlyWinning = true;
updateOpenLock(this);
this.input.setDefaultCursor('default');
}
if (!colorLock.isWinCondition() && currentlyWinning)
{
currentlyWinning = false;
}
let mouseX = this.input.mousePointer.x;
let mouseY = this.input.mousePointer.y;
let pointer = this.input.activePointer;
let leftButtonDown = pointer.isDown && (pointer.rightButtonDown() === 0)
// Put main code inside of here
// to make it so you can't click after you have won
if (!currentlyWinning)
{
if (isInClickableArea([mouseX, mouseY]))
{
this.input.setDefaultCursor('pointer');
let rightButtonDown = pointer.rightButtonDown()
// Using a flag 'mouseClicked' to make sure it only runs once
if (leftButtonDown && !mouseClicked)
{
mouseClicked = true;
let lockCoord = getSlotCoordinate([mouseX, mouseY]);
if (colorLock.isValidMove(lockCoord[0], lockCoord[1]))
{
let colorClicked = colorLock.getColor(lockCoord[0],lockCoord[1]);
let targetPosition = colorLock.findEmptySlot();
colorLock.clickColor(lockCoord[0], lockCoord[1]);
updateColorLock(lockCoord, targetPosition, colorClicked, this, colorLock);
}
}
}
else
{
this.input.setDefaultCursor('default');
}
}
// Reseting the click flag
if (!leftButtonDown)
{
mouseClicked = false;
}
}
</script>
</body>
</html>