-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.js
62 lines (60 loc) · 1.79 KB
/
ui.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
var UI = {
gameState: ko.observable(new jsgo.GameState()),
forward: function () {
var next = UI.gameState().children[0];
if (next)
UI.gameState(next);
},
backward: function () {
var prev = UI.gameState().parent;
if (prev)
UI.gameState(prev);
},
fastForward: function () {
var current = UI.gameState(),
next;
while (next = current.children[0])
current = next;
UI.gameState(current);
},
rewind: function () {
var current = UI.gameState(),
prev;
while (prev = current.parent)
current = prev;
UI.gameState(current);
},
makeMove: function (cell) {
UI.gameState(UI.gameState().place(new jsgo.Vec(cell.x, cell.y)));
}
},
keyDownActions = {
"40": function () { UI.backward() }, // down
"38": function () { UI.forward() }, // up
"37": function () { UI.backward() }, // left
"39": function () { UI.forward() }, // right
"36": function () { UI.rewind() }, // home
"35": function () { UI.fastForward() } // end
};
$(function () {
$(window).keydown(function (e) {
if (keyDownActions.hasOwnProperty("" + e.which)) {
keyDownActions["" + e.which](e);
e.preventDefault();
return false;
}
});
UI.forwardPossible = ko.computed(function () {
return UI.gameState().children.length > 0;
});
UI.backwardPossible = ko.computed(function () {
return UI.gameState().parent;
});
UI.positions = [];
for (var y = 0; y < 19; y++) {
for (var x = 0; x < 19; x++) {
UI.positions.push(new jsgo.Vec(x, y));
}
}
ko.applyBindings(UI, $("#html")[0]);
});