-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPuzzle.js
105 lines (105 loc) · 3.81 KB
/
Puzzle.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
"use strict";
exports.__esModule = true;
exports.Puzzle = void 0;
var Grid_js_1 = require("./Grid.js");
var Location_js_1 = require("./Location.js");
var Puzzle = /** @class */ (function () {
// REQUIRES: nothing.
function Puzzle() {
// TODO - verify that this works
this.grid = new Grid_js_1.Grid;
this.emptyLocation = new Location_js_1.Location(3, 3);
this.initializeAllButtons();
this.initializeArrowKeyInputs();
this.puzzleStatusElement = document.getElementById("puzzle-status");
}
//EFFECTS: updates puzzle status based on grid
Puzzle.prototype.updatePuzzleStatusElement = function () {
if (this.grid.isGridComplete()) {
this.puzzleStatusElement.innerHTML = "Completed";
return;
}
this.puzzleStatusElement.innerHTML = "Not Completed";
};
//EFFECTS: initialize buttons
Puzzle.prototype.initializeAllButtons = function () {
this.initializePuzzleMenuButtons();
this.initializeDirectionMenuButtons();
};
//EFFECTS: initialize puzzle menu buttons
Puzzle.prototype.initializePuzzleMenuButtons = function () {
var _this = this;
var shuffleButton = document.getElementById("shuffle-button");
shuffleButton.onclick = function () {
_this.shuffle();
};
var resetButton = document.getElementById("reset-button");
resetButton.onclick = function () {
_this.grid.reset();
_this.updatePuzzleStatusElement();
};
};
//EFFECTS: initialize direction menu buttons
Puzzle.prototype.initializeDirectionMenuButtons = function () {
var _this = this;
var directionMenuButtons = [
document.getElementById("up-button"),
document.getElementById("right-button"),
document.getElementById("down-button"),
document.getElementById("left-button"),
];
var _loop_1 = function (i) {
directionMenuButtons[i].onclick = function () {
_this.movePuzzle(i);
};
};
for (var i = 0; i < directionMenuButtons.length; ++i) {
_loop_1(i);
}
};
//EFFECTS: configure user arrow key inputs
Puzzle.prototype.initializeArrowKeyInputs = function () {
window.addEventListener("keydown", function (event) {
if (event.defaultPrevented) {
return;
}
switch (event.key) {
case "ArrowDown":
console.log("Arrow down");
puzzle.movePuzzle(2 /* Direction.Down */);
break;
case "ArrowUp":
puzzle.movePuzzle(0 /* Direction.Up */);
break;
case "ArrowLeft":
puzzle.movePuzzle(3 /* Direction.Left */);
break;
case "ArrowRight":
puzzle.movePuzzle(1 /* Direction.Right */);
break;
default:
return;
}
event.preventDefault();
}, true);
};
//EFFECTS: shuffles the grid
Puzzle.prototype.shuffle = function () {
for (var i = 0; i < 500; ++i) {
this.movePuzzle(Math.floor(Math.random() * 4));
this.updatePuzzleStatusElement();
}
};
//EFFECTS: moves grid in a given direction
Puzzle.prototype.movePuzzle = function (dir) {
var movedTileLocation = this.emptyLocation.getLocationInDirection(dir);
if (!movedTileLocation.isValid())
return;
this.grid.swap(this.emptyLocation, movedTileLocation);
Object.assign(this.emptyLocation, movedTileLocation);
this.updatePuzzleStatusElement();
};
return Puzzle;
}());
exports.Puzzle = Puzzle;
var puzzle = new Puzzle();