-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
executable file
·109 lines (102 loc) · 2.6 KB
/
main.js
File metadata and controls
executable file
·109 lines (102 loc) · 2.6 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
var grid, gridTable;
function main() {
"use strict";
gridTable = document.querySelector("#grid");
document.setting.onsubmit = function (e) {
e.preventDefault();
initGrid();
};
document.setting.up.onclick = function () {
"use strict";
var p, t;
p = Math.log(parseInt(document.setting.target.value))/Math.log(2);
p += 1;
t = Math.pow(2, p);
document.setting.target.value = t + "";
if (grid && !grid.fail) {
grid.target = t;
}
};
document.setting.down.onclick = function () {
"use strict";
var p, t;
p = Math.log(parseInt(document.setting.target.value))/Math.log(2);
p -= 1;
t = Math.pow(2, p);
document.setting.target.value = t + "";
if (grid && !grid.fail) {
grid.target = t;
}
};
}
function enableKeys() {
"use strict";
window.onkeydown = function (e) {
var res;
switch (e.keyCode) {
case 37:
grid.moveLeft();
break;
case 38:
grid.moveUp();
break;
case 39:
grid.moveRight();
break;
case 40:
grid.moveDown();
break;
default:
return null;
}
update();
done();
};
}
function done() {
"use strict";
var res;
res = grid.check();
if (!res) {
alert("Loser");
disableKeys();
} else if (res === 2) {
alert("Winner!");
disableKeys();
}
}
function initGrid() {
"use strict";
var l, w, t;
l = parseInt(document.setting.rows.value);
w = parseInt(document.setting.columns.value);
t = parseInt(document.setting.target.value);
grid = new Grid(l, w, t);
if (grid.fail) {
alert("Enter a valid target. It should be a power of 2.");
return;
}
grid.insert(2);
update();
enableKeys();
}
function update() {
"use strict";
var i, j;
while (gridTable.hasChildNodes()) {
gridTable.removeChild(gridTable.firstChild);
}
for (i = 0; i < grid.length; i += 1) {
gridTable.insertRow(i);
for (j = 0; j < grid.width; j += 1) {
gridTable.rows[i].insertCell(j);
gridTable.rows[i].cells[j].appendChild(document.createElement("span"));
gridTable.rows[i].cells[j].firstChild.innerHTML = grid.cells[i][j].value || "";
}
}
}
function disableKeys() {
"use strict";
window.onkeydown = null;
}
addEventListener("load", main, false);