-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
291 lines (250 loc) · 7.74 KB
/
game.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
DEFAULT_WIDTH = 8
DEFAULT_HEIGHT = 8
// X is left to right
// Y is top to bottom
class BattleshipGame {
static VESSELS = [
{
id: 'carrier',
size: 5
},
{
id: 'battleship',
size: 4
},
{
id: 'cruiser',
size: 3
},
{
id: 'submarine',
size: 3
},
{
id: 'destroyer',
size: 2
}
];
constructor() {
this.player1 = new BattleshipPlayer();
this.player2 = new BattleshipPlayer();
}
}
class BattleshipPlayer {
constructor(boardWidth = DEFAULT_WIDTH, boardHeight = DEFAULT_HEIGHT) {
this.board = new BattleshipBoard(boardWidth, boardHeight);
}
/**
*
* @param {BattleshipVessel[]} vessels
*/
placeVessels(vessels) {
return this.board.placeVessels(vessels);
}
}
class BattleshipGuess {
static HIT = 'hit';
static MISS = 'miss';
constructor(x, y, result) {
if (!BattleshipGuess.checkValidity(result))
throw new Error('Invalid guess result');
this.x = x;
this.y = y;
this.result = result;
}
static checkValidity(guess) {
return guess === BattleshipGuess.HIT || guess === BattleshipGuess.MISS;
}
}
class BattleshipBoard {
constructor(width, height) {
this.ships = [];
this.width = width;
this.height = height;
this.guesses = [];
}
/**
*
* @param {number} x
* @param {number} y
* @returns {BattleshipGuess || null} The Guess if it is valid, null if invalid
*/
guess(x, y) {
if (this.guesses.find((guess) => guess.x == x && guess.y == y))
return null;
let guess = new BattleshipGuess(x, y, this.checkSpace(x, y) ? BattleshipGuess.HIT : BattleshipGuess.MISS);
this.guesses.push(guess);
return guess;
}
/**
*
* @param {BattleshipVessel} ship
* @returns {boolean} Whether the ship was successfully placed on the board
*/
placeShip(ship) {
if (String(ship.orientation).toLowerCase() == 'horizontal') {
ship.orientation = BattleshipOrientation.HORIZONTAL;
} else if (String(ship.orientation).toLowerCase() == 'vertical') {
ship.orientation = BattleshipOrientation.VERTICAL;
}
if (!BattleshipOrientation.getValidity(ship.orientation))
throw new Error('Invalid orientation');
if (ship.x < 0 || ship.y < 0)
return false;
if (ship.x >= this.width || ship.y >= this.height)
return false;
if (ship.orientation === BattleshipOrientation.HORIZONTAL && ship.x + ship.size > this.width)
return false;
if (ship.orientation === BattleshipOrientation.VERTICAL && ship.y + ship.size > this.height)
return false;
let shipObj = new BattleshipVessel(ship.x, ship.y, ship.orientation, ship.size, ship.id);
for (let otherShip of this.ships)
if (otherShip.overlapsWith(shipObj))
return false;
this.ships.push(shipObj);
return true;
}
/**
*
* @param {number} x
* @param {number} y
* @throws An error if the coordinates are out of bounds
* @returns {boolean} True if the given coordinates are occupied by a ship
*/
checkSpace(x, y) {
if (x < 0 || y < 0)
throw new Error('Coordinates out of bounds');
if (x >= this.width || y >= this.height)
throw new Error('Coordinates out of bounds');
for (let ship of this.ships)
if (ship.containsPoint(x, y))
return true;
return false;
}
/**
*
* @returns {BattleshipVessel[]} The ships not yet on the board
*/
getAvailableShips() {
return BattleshipGame.VESSELS.filter((vessel) => !this.ships.some((ship) => ship.id === vessel.id));
}
getSunkShips() {
return this.ships.filter((ship) => ship.isSunk(this));
}
isEveryShipSunk() {
return this.ships.every((ship) => ship.isSunk(this));
}
/**
*
* @param {BattleshipVessel[]} vessels
*/
placeVessels(vessels) {
for (let vessel of vessels)
if (!this.placeShip(vessel)) {
this.ships.length = 0;
return false;
}
let counter = 0;
for (let row of this.getCurrentState())
for (let cell of row)
if (cell)
counter++;
if (counter !== vessels.reduce((acc, vessel) => acc + vessel.size, 0)) {
this.ships.length = 0;
return false;
}
return true;
}
/**
* @returns {number[][]}
*/
getCurrentState() {
let board = [];
for (let y = 0; y < this.height; y++) {
board.push([]);
for (let x = 0; x < this.width; x++) {
board[y][x] = this.checkSpace(x, y);
}
// DEBUG
// console.log(board[y]);
}
return board;
}
getGuesses() {
return this.guesses;
}
}
class BattleshipVessel {
constructor(x, y, orientation, size, id) {
if (!BattleshipOrientation.getValidity(orientation))
throw new Error("Invalid orientation");
if (size < 1)
throw new Error("Size has to be >1");
this.x = x;
this.y = y;
this.orientation = orientation || BattleshipOrientation.HORIZONTAL;
this.size = size || 0;
this.id = id || "n/a";
}
/**
* This function checks whether this ship overlaps with another given ship
* Please note that this function ignores whether the ships are on the same board
* @todo Please verify that the checks work
* @param {BattleshipVessel} other
* @returns {boolean}
*/
overlapsWith(other) {
if (!(other instanceof BattleshipVessel))
throw new Error("Argument other has to be of type BattleshipVessel");
let thisBottomX, thisBottomY, otherBottomX, otherBottomY;
if (this.orientation === BattleshipOrientation.HORIZONTAL) {
thisBottomX = this.x + this.size - 1;
thisBottomY = this.y;
} else {
thisBottomX = this.x;
thisBottomY = this.y + this.size - 1;
}
if (other.orientation === BattleshipOrientation.HORIZONTAL) {
otherBottomX = other.x + other.size - 1;
otherBottomY = other.y;
} else {
otherBottomX = other.x;
otherBottomY = other.y + other.size - 1;
}
if (thisBottomX < other.x || otherBottomX < thisBottomX)
return false;
if (thisBottomY < other.y || otherBottomY < this.y)
return false;
return true;
}
containsPoint(x, y) {
if (this.orientation === BattleshipOrientation.HORIZONTAL) {
return this.x <= x && x < this.x + this.size && this.y === y;
} else {
return this.y <= y && y < this.y + this.size && this.x === x;
}
}
isSunk(board) {
let hitCount = 0;
board.guesses.forEach((guess) => {
if (this.containsPoint(guess.x, guess.y) && guess.result === BattleshipGuess.HIT)
hitCount++;
});
return hitCount === this.size;
}
}
class BattleshipOrientation {
static HORIZONTAL = 1;
static VERTICAL = 2;
static getValidity(orientation) {
return orientation === BattleshipOrientation.HORIZONTAL || orientation === BattleshipOrientation.VERTICAL;
}
}
module.exports = {
BattleshipGame,
BattleshipPlayer,
BattleshipGuess,
BattleshipBoard,
BattleshipVessel,
BattleshipOrientation
}