-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
350 lines (313 loc) · 8.9 KB
/
main.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
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
function battleShipSolver() {
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var cellW = canvas.width / 10;
var cellH = canvas.height / 10;
var confirmMode = false;
var shotCount = 0;
var randomness = 3;
var ships = {
'battleship': {
alive: true,
length: 4,
position: 0,
orientation: 'vertical',
},
'destroyer': {
alive: true,
length: 3,
position: 0,
orientation: 'vertical',
},
'aircraftcarrier': {
alive: true,
length: 5,
position: 0,
orientation: 'vertical',
},
'submarine': {
alive: true,
length: 3,
position: 0,
orientation: 'vertical',
},
'patrolboat': {
alive: true,
length: 2,
position: 0,
orientation: 'vertical',
},
};
// Set up position array
var positions = [];
for (var i = 0; i < 100; i++) {
var row = Math.floor(i / 10),
col = i % 10;
positions[i] = {
index: i,
probability: 0,
occupied: false,
fired: false,
hit: false,
confirmed: false,
row: row,
col: col,
};
}
// Set references to other positions (for convinence & code clarity)
for (var i = 0; i < 100; i++) {
positions[i].w = (positions[i - 1] && positions[i - 1].row === positions[i].row) ? positions[i - 1] : null;
positions[i].e = (positions[i + 1] && positions[i + 1].row === positions[i].row) ? positions[i + 1] : null;
positions[i].n = positions[i - 10] ? positions[i - 10] : null;
positions[i].s = positions[i + 10] ? positions[i + 10] : null;
}
function step() {
fireNext();
updateProbabilities();
redrawBoard();
}
function updateProbabilities() {
var ship, position, lastPosition, hitStreak = 0;
var directions = {'w':null, 'n':null, 'e':null, 's':null};
for (var i = 0; i < 100; i++) {
positions[i].probability = 0;
}
for (var shipName in ships) {
ship = ships[shipName];
if (ship.alive) {
for (var i = 0; i < 100; i++) {
if (tryShipAtPosition(ship, i, 's', 'confirmed')) {
lastPosition = positions[i];
for (var j = 0; j < ship.length; j++) {
lastPosition.probability++;
lastPosition = lastPosition['s'];
}
}
if (tryShipAtPosition(ship, i, 'e', 'confirmed')) {
lastPosition = positions[i];
for (var j = 0; j < ship.length; j++) {
lastPosition.probability++;
lastPosition = lastPosition['e'];
}
}
}
}
}
for (var i = 0; i < 100; i++) {
position = positions[i];
if (position.probability > 0) {
position.probability += Math.floor(Math.random() * randomness);
}
if (position.fired) {
if (position.hit && !position.confirmed) {
for (var direction in directions) {
lastPosition = position;
hitStreak = 1;
while(lastPosition[direction] && lastPosition[direction].hit && !lastPosition[direction].confirmed) {
hitStreak++;
lastPosition = lastPosition[direction];
}
lastPosition = lastPosition[direction];
if (lastPosition && !lastPosition.fired) {
lastPosition.probability += hitStreak * 10;
}
}
}
// All positions that have already been fired at should have the lowest probability
position.probability = -1;
}
}
}
// Test if a ship will fit at a given position & direction
function tryShipAtPosition(ship, position, orientation, propertyToTest) {
var lastPosition = positions[position], fit = true;
for (var i = 0; i < ship.length; i++) {
if (!lastPosition || lastPosition[propertyToTest]) {
fit = false;
break;
}
lastPosition = lastPosition[orientation];
}
return fit;
}
function removeShip(shipName) {
var allDead = true;
ships[shipName].alive = false;
// If this is the last ship, end the game.
for (var ship in ships) {
if (ships[ship].alive) {
allDead = false;
}
}
if (allDead) {
document.getElementById('result').innerHTML = 'Game over! Move count: '+shotCount;
}
updateProbabilities();
redrawBoard();
}
// Random ship placement
// Todo: allow custom positions as params
function placeShips() {
var index, orientation, validPosition = false, lastPosition;
for(ship in ships) {
while(!validPosition) {
index = getRandomPosition();
orientation = (Math.random() * 2 > 1) ? 'e' : 's';
if (tryShipAtPosition(ships[ship], index, orientation, 'occupied')) {
validPosition = true;
}
}
lastPosition = positions[index];
for (var i = 0; i < ships[ship].length; i++) {
lastPosition.occupied = true;
lastPosition = lastPosition[orientation];
}
ships[ship].position = index;
validPosition = false;
}
}
function getRandomPosition() {
return Math.floor(Math.random() * 100);
}
// Sorts the board pieces by probability and finds out whether a hit or a miss is found at highest probability position. Re-sorts based on index when finished.
function fireNext() {
shotCount++;
positions.sort(function sortByProbability(a, b) {
if (a.fired) {
return 1;
} else {
return b.probability - a.probability;
}
});
positions[0].fired = true;
var hit = positions[0].hit = confirm('hit at '+getIndexNicename(0)+'?');
// misses are set to confirmed to improve the probability engine
positions[0].confirmed = hit ? false : true;
var index = positions[0].index;
positions[0].probability = -1;
positions.sort(function sortByIndex(a, b) {
return a.index - b.index;
});
return index;
}
function toggleConfirmMode(e) {
if (confirmMode) {
confirmMode = false;
updateProbabilities();
redrawBoard();
canvas.removeEventListener('click', updateConfirmations);
e.target.innerHTML = "Confirm hits";
} else {
confirmMode = true;
drawConfirmations();
canvas.addEventListener('click', updateConfirmations);
e.target.innerHTML = "Done";
}
}
// Fires whenever canvas receives a click if confirmMode is active
function updateConfirmations(e) {
var index = getIndexFromClickPosition(e.layerX, e.layerY);
positions[index].confirmed = positions[index].confirmed ? false : true; // flip value
redrawBoard();
drawConfirmations();
}
function getIndexFromClickPosition(x, y) {
var pos = 0;
pos += Math.floor(x / (canvas.width / 10));
pos += Math.floor(y / (canvas.width / 10)) * 10;
return pos;
}
function getIndexNicename(index) {
var letterCodes = ['A','B','C','D','E','F','G','H','I','J'];
return letterCodes[positions[index].row]+','+(positions[index].col + 1);
}
// Todo: combine draw functions into unified loop to increase efficiency & clarity
function redrawBoard() {
ctx.fillStyle = '#fff';
ctx.fillRect(0, 0, canvas.width, canvas.height);
drawGrid();
drawShips();
drawShots();
drawSuperPositions();
}
function drawConfirmations() {
ctx.fillStyle = 'rgba(45,204,71,0.6)';
for (var i = 0; i < 100; i++) {
if (positions[i].confirmed) {
ctx.fillRect(cellW * (i % 10) + 6, cellH * Math.floor(i / 10) + 6, cellW - 12, cellH - 12);
}
}
}
function drawGrid() {
ctx.strokeStyle = "#000";
for (var i = 0; i <= 100; i++) {
ctx.strokeRect(cellW * (i % 10), cellH * Math.floor(i / 10), cellW, cellH);
}
}
function drawShips() {
ctx.fillStyle = '#2E8ED8';
for (var i = 0; i < 100; i++) {
if (!positions[i].occupied) {
ctx.fillRect(cellW * (i % 10) + 1, cellH * Math.floor(i / 10) + 1, cellW - 2, cellH - 2);
}
}
}
function drawSuperPositions() {
var ceiling = 0;
for (var i = 0; i < 100; i++) {
ceiling = positions[i].probability > ceiling ? positions[i].probability : ceiling;
}
for (var i = 0; i < 100; i++) {
ctx.fillStyle = 'rgba(0,0,0,'+(positions[i].probability / ceiling * 0.6)+')';
ctx.fillRect(cellW * (i % 10) + 1, cellH * Math.floor(i / 10) + 1, cellW - 2, cellH - 2);
ctx.fillText(positions[i].probability, cellW * (i % 10) + 2, cellH * Math.floor(i / 10) + cellH - 2);
}
}
function drawShots() {
for (var i = 0; i < 100; i++) {
if (positions[i].fired) {
if (positions[i].hit) {
ctx.fillStyle = 'red';
} else {
ctx.fillStyle = 'gray';
}
ctx.fillRect(cellW * (i % 10) + 6, cellH * Math.floor(i / 10) + 6, cellW - 12, cellH - 12);
}
}
}
function eventProcessor (e) {
switch(e.target.id) {
case 'step':
step();
break;
case 'confirm-hits':
toggleConfirmMode(e);
break;
case 'sunk-battleship':
removeShip('battleship');
break;
case 'sunk-destroyer':
removeShip('destroyer');
break;
case 'sunk-aircraftcarrier':
removeShip('aircraftcarrier');
break;
case 'sunk-submarine':
removeShip('submarine');
break;
case 'sunk-patrolboat':
removeShip('patrolboat');
break;
}
}
var eventHandles = ['step', 'sunk-battleship', 'sunk-destroyer', 'sunk-aircraftcarrier', 'sunk-submarine', 'sunk-patrolboat', 'confirm-hits'];
for (var i = 0; i < eventHandles.length; i++) {
document.getElementById(eventHandles[i]).addEventListener('click', eventProcessor);
}
drawGrid();
placeShips();
updateProbabilities();
drawShips();
drawSuperPositions();
}
window.addEventListener("DOMContentLoaded", battleShipSolver);