-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
225 lines (207 loc) · 7.66 KB
/
script.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
GRID_SIZE = 9;
SHIPS = [
{shipType: "Aircraft carrier", shipSize: 5},
{shipType: "Battleship", shipSize: 4},
{shipType: "Submarine", shipSize: 3},
{shipType: "Destroyer", shipSize: 3},
{shipType: "Patrol boat", shipSize: 2}
]
// Knockout.js stuff
var AppViewModel = function() {
// Observables
this.instructions = ko.observable("Place ships:");
this.mode = ko.observable("PLACE");
this.placedShips = ko.observable(0);
this.hits = ko.observable(0);
this.shots = ko.observable(0);
this.range = ko.observableArray([]);
this.rangeLength = ko.computed(function() {
return this.range().length;
}, this);
this.accuracy = ko.computed(function() {
if (this.shots() < 1) return 0;
return Math.floor(this.hits()/this.shots() * 100);
}, this);
// Other variables
this.ships = [];
this.shipPos = [];
for(i = 0; i < SHIPS.length; i++)
this.ships.push({ship: SHIPS[i], placed: ko.observable(false)});
this.ships.pendingSizes = ko.utils.arrayMap(this.ships, function(el) {
return el.ship.shipSize;
});
// Click functions
this.play = function() {
// remove hover handler and rebind the click handler
$("div#container").off().on("click", "span.cell", eventHandlers.play);
// hide ships
$("div#container").find("span.cell").removeClass("placed valid miss");
// enter play mode
this.instructions("PLAY!");
this.mode("PLAY");
};
this.playAgain = function() {
// reset
location.reload();
};
// Custom binding handlers
ko.bindingHandlers.strike = {
update: function(element, valueAccessor) {
if (valueAccessor()) {
$(element).addClass("strikeout");
var rootContext = ko.contextFor(element).$root;
rootContext.increment(rootContext.placedShips);
}
}
};
// Helper functions
this.increment = function(observable) {
// avoid registering dependency by using peek
observable(observable.peek()+1);
};
this.shipPos.getNumOfCells = function() {
// cached for better perf.
var res = this.getNumOfCells.cache || null;
if (res) return res;
this.getNumOfCells.cache = ko.utils.arrayFilter(this, function(el) {
return el;
}).length;
return this.getNumOfCells.cache;
};
}
// create globally accessible ref to the view model
viewModel = new AppViewModel();
// Click/hover handlers
var eventHandlers = {
place: {
start: null,
range: viewModel.range,
click: function() {
var self = this;
$("div#container").on("mouseenter mouseleave", "span.cell", self.hover());
return function(event) {
// set starting cell or reset on subsequent click
self.start = self.start ? null : this.id;
var rangeLen = viewModel.rangeLength.peek();
if (!self.start && rangeUtils.isValid(rangeLen)) {
// Player just placed ship
// add ship position
for (var i in self.range()) {
viewModel.shipPos[self.range()[i]] = true;
}
// remove range length from pendinding sizes
var ps = viewModel.ships.pendingSizes;
ps.splice(ps.indexOf(rangeLen), 1);
// mark next ship with fitting size as placed
for (var i in viewModel.ships) {
var ship = viewModel.ships[i];
if (ship.placed.peek()) continue;
if (ship.ship.shipSize == rangeLen) {
ship.placed(true);
rangeUtils.addClass(self.range(), "placed");
break;
}
}
// clean up
self.range.removeAll();
}
// always clear 'temp' styles on click
$("span.cell").removeClass("miss valid hit");
};
},
hover: function() {
var self = this;
return function(event) {
// don't do anything if start cell hasn't been chosen
if (!self.start) return;
// check if cell is in the same row/column
if (this.id[0].indexOf(self.start[0]) !== -1 ||
this.id[1].indexOf(self.start[1]) !== -1) {
// unary plus to convert str -> num
self.range(rangeUtils.getCellRange(+self.start, +this.id));
// check if range corresponds to a yet-to-be-placed ship
var cssClass = rangeUtils.isValid(self.range().length) ? "valid" : "miss";
// check if new range doesnt collide with already placed ships
var collision = false;
for (var i in self.range()) {
if (viewModel.shipPos[self.range()[i]]) {
cssClass = "hit";
collision = true;
break;
}
}
// reset cell coloring
$("span.cell").removeClass("miss valid hit");
rangeUtils.addClass(self.range(), cssClass);
// disallow placing ship if it collides with another
if (collision)
self.range([]);
} else {
self.range.removeAll();
$("span.cell").removeClass("miss valid hit");
}
};
}
},
play: function(event) {
// don't coutn already clicked cells
if ($(this).hasClass("hit") || $(this).hasClass("miss"))
return;
var id = this.id;
var hit = viewModel.shipPos[+id];
// increment hits and shots
viewModel.increment(viewModel.shots);
if (hit) {
viewModel.increment(viewModel.hits);
if (viewModel.hits() == viewModel.shipPos.getNumOfCells()) {
// game over!
viewModel.instructions("You win! Such amaze! Wow!");
viewModel.mode("FINISHED");
// unbind event handlers
$("div#container").off();
}
}
// change color of the cell
$(this).addClass(hit ? "hit" : "miss");
}
}
// Range utils
var rangeUtils = {
getCellRange: function(start, end) {
// swap if start > end
if (start > end) {
var t = start;
start = end;
end = t;
}
var res = [];
if (end - start > GRID_SIZE-1) {
// vertical range
for (; start <= end;) {
res.push(start);
start += 10;
}
return res;
}
// otherwise horizontal range
return ko.utils.range(start, end);
},
isValid: function(rangeLen) {
return viewModel.ships.pendingSizes.indexOf(rangeLen) !== -1;
},
addClass: function(range, cssClass) {
for (var i = 0; i < range.length; i++) {
var id = range[i].toString();
// add padding zero if necessary
if (id.length < 2)
id = 0+id;
$("span.cell#"+id).addClass(cssClass);
}
}
}
$(document).ready(function() {
// activate click handler
$("div#container").on("click", "span.cell", eventHandlers.place.click());
// activate knockout.js
ko.applyBindings(viewModel);
});