-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogic.js
84 lines (73 loc) · 2.51 KB
/
logic.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
define(['lodash'], function(_) {
var Ship = function(x, y, orientation, length) {
this.points = [];
for (var i = 0; i < length; i++) {
this.points.push({ x: x, y: y });
if (orientation === 'horizontal') {
x++;
}
else {
y++;
}
}
};
Ship.prototype.isMatch = function(x, y) {
return _.find(this.points, function(point) { return point.x === x && point.y === y; });
};
const boardSize = 10;
var Game = function(ships) {
this.ships = ships;
this.guesses = [];
};
Game.prototype.guess = function(x, y) {
var found = _.find(this.ships, function(ship) { return ship.isMatch(x, y); });
var guess = { x: x, y: y, state: found ? 'ship' : 'empty' };
this.guesses.push(guess);
};
function setStatusAt(board, x, y, newStatus) {
if (0 <= x && x < boardSize && 0 <= y && y < boardSize && board[y][x]) {
if (board[y][x] === 'unknown') {
board[y][x] = newStatus;
}
}
}
Game.prototype.getMatrix = function(showSolution, showHints) {
var ret = [];
for (var i = 0; i < boardSize; i++) {
var row = [];
for (var j = 0; j < boardSize; j++) {
var found = _.find(this.guesses, function(guess) { return i === guess.y && j === guess.x; });
row.push(found ? found.state : 'unknown');
}
ret.push(row);
}
// show all ships
if (showSolution) {
_.forEach(this.ships, function (ship) {
_.forEach(ship.points, function (point) {
if (ret[point.y][point.x] === 'unknown') {
ret[point.y][point.x] = 'ship-revealed';
}
});
});
}
// show all revealed-empty ships
if (showHints) {
for (var r = 0; r < ret.length; r++) {
for (var c = 0; c < ret[r].length; c++) {
if (ret[r][c] === 'ship') {
setStatusAt(ret, c-1, r-1, 'empty-revealed');
setStatusAt(ret, c+1, r-1, 'empty-revealed');
setStatusAt(ret, c-1, r+1, 'empty-revealed');
setStatusAt(ret, c+1, r+1, 'empty-revealed');
}
}
}
}
return ret;
};
return {
Game: Game,
Ship: Ship
}
});