-
Notifications
You must be signed in to change notification settings - Fork 0
/
battleship.js
425 lines (415 loc) · 16.1 KB
/
battleship.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
const model = {
player1Board: [0,0,0,0,0, // 0 = empty
0,0,0,0,0, // 1 = ship
0,0,0,0,0, // 2 = hit
0,0,0,0,0, // 3 = miss
0,0,0,0,0],
player2Board: [0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0,
0,0,0,0,0],
player1Ships: 0,
player2Ships: 0,
player1LoadedShips: false,
player2LoadedShips: false,
player1Keys: [],
player2Keys: [],
playerAlreadyWon: false,
turn: 'player1',
alreadySunkTestBoolean1: false,
alreadySunkTestBoolean2: false,
alreadyMissTestBoolean1: false,
alreadyMissTestBoolean2: false,
winFromDeadTestBoolean: false,
}
const view = {
displayMessage: msg => {
const node = document.createElement("LI")
const time = new Date()
const timeStamp = time.toLocaleTimeString('en-US')
const textnode = document.createTextNode(`${timeStamp}: ${msg}`)
node.appendChild(textnode)
document.getElementById("messages").appendChild(node)
//scroll down after new message
const elem = document.getElementById("message_area");
elem.scrollTop = elem.scrollHeight;
},
}
const controller = {
player1BoardKeyClick: num => {
if ((model.turn === 'player1' && !model.player1LoadedShips) || (model.turn === 'player2' && model.player1LoadedShips && model.player2LoadedShips)) {
// fire
if (model.player1LoadedShips) {
switch (model.player1Board[num]) {
case 0:
model.player1Board[num] = 3
view.displayMessage('Player 2 missed!')
document.getElementById(`player1_cell_${num}`).setAttribute("class", 'shipMiss')
model.turn = 'player1'
break;
case 1:
model.player1Board[num] = 2
view.displayMessage('Player 2 hit a ship!')
document.getElementById(`player1_cell_${num}`).setAttribute("class", 'shipHit')
model.player1Ships -= 1
if (model.player1Ships > 0) {
if (model.player1Ships > 1) {
view.displayMessage(`Player 2 has Sunk a Ship! Player 1 has ${model.player1Ships} ships left!`)
} else {
view.displayMessage(`Player 1's forces grow weak, they have 1 ship left!`)
}
} else {
if (!model.playerAlreadyWon) {
view.displayMessage(`Congratulations Player 2! You won!`)
view.displayMessage(`To start a new game click the "new game" button at the top of the page`)
createVictor()
model.playerAlreadyWon = true
} else {
view.displayMessage(`Player 2 won from the dead!`)
model.winFromDeadTestBoolean = true
}
}
model.turn = 'player1'
break;
case 2:
view.displayMessage('Sunk: They\'re already dead! Have mercy and attack somewhere else!')
model.alreadySunkTestBoolean2 = true
break;
case 3:
view.displayMessage('Already Taken: All ships in this war cannot move, rest assured that no one has moved here')
model.alreadyMissTestBoolean2 = true
break;
default:
console.log('error unknown case: battleship.js line 62')
}
}
//loading ships
if (!model.player1LoadedShips && model.player1Ships < 4) {
if (model.player1Board[num] === 0) {
model.player1Board[num] = 1
model.player1Ships += 1
if (model.player1Ships > 1) {
view.displayMessage(`Player 1 has placed ${model.player1Ships} ships!`)
} else {
view.displayMessage(`Player 1 has placed 1 ship!`)
}
} else {
view.displayMessage('Tile Occupied: Due to complaints from ship captains, we are no longer allowed to stack ships on top of each other. Please place your ship in an empty tile.')
}
if (model.player1Ships === 3) {
model.player1LoadedShips = true
model.turn = 'player2'
view.displayMessage(`Player 1 has placed all ships, Player 2's turn to place ships`)
}
}
} else {
view.displayMessage('It is not your turn! Even in stressful times of war, we must not cheat.')
}
console.log('PASS: click on p1 tile: ',num)
},
player2BoardKeyClick: num => {
if ((model.turn === 'player2' && !model.player2LoadedShips) || (model.turn === 'player1' && model.player2LoadedShips)) {
//fire
if (model.player2LoadedShips) {
switch (model.player2Board[num]) {
case 0:
model.player2Board[num] = 3
view.displayMessage('Player 1 missed!')
document.getElementById(`player2_cell_${num}`).setAttribute("class", 'shipMiss')
model.turn = 'player2'
break;
case 1:
model.player2Board[num] = 2
view.displayMessage('Player 1 hit a ship!')
document.getElementById(`player2_cell_${num}`).setAttribute("class", 'shipHit')
model.player2Ships -= 1
if (model.player2Ships > 0) {
if (model.player2Ships > 1) {
view.displayMessage(`Player1 has Sunk a Ship! Player 2 has ${model.player2Ships} ships left!`)
} else {
view.displayMessage(`Player 2's forces grow weak, they have 1 ship left!`)
}
} else {
if (!model.playerAlreadyWon) {
view.displayMessage(`Congratulations Player 1! You won!`)
view.displayMessage(`To start a new game click the "new game" button at the top of the page`)
createVictor()
model.playerAlreadyWon = true
} else {
view.displayMessage(`Player 1 won from the dead!`)
model.winFromDeadTestBoolean = true
}
}
model.turn = 'player2'
break;
case 2:
view.displayMessage('Sunk: They\'re already dead! Have mercy and attack somewhere else!')
model.alreadySunkTestBoolean1 = true
break;
case 3:
view.displayMessage('Already Taken: All ships in this war cannot move, rest assured that no one has moved here')
model.alreadyMissTestBoolean1 = true
break;
default:
console.log('error unknown case: battleship.js line 62')
}
}
//loading ships
if (!model.player2LoadedShips && model.player2Ships < 4) {
if (model.player2Board[num] === 0) {
model.player2Board[num] = 1
model.player2Ships += 1
if (model.player2Ships > 1) {
view.displayMessage(`Player 2 has placed ${model.player2Ships} ships!`)
} else {
view.displayMessage(`Player 2 has placed 1 ship!`)
}
} else {
view.displayMessage('Tile Occupied: Due to complaints from ship captains, we are no longer allowed to stack ships on top of each other. Please place your ship in an empty tile.')
}
if (model.player2Ships === 3) {
model.player2LoadedShips = true
model.turn = 'player1'
view.displayMessage(`Player 2 has placed all ships, Player 1 start by attacking Player 2's board!`)
}
}
} else {
view.displayMessage('It is not your turn! Even in stressful times of war, we must not cheat.')
}
console.log('PASS: click on p2 tile: ', num)
}
}
const bindBoardClickToKeys = () => {
for (let i = 0; i < 25; i += 1) {
//player 1
let key = document.getElementById(`player1_cell_${i.toString()}`)
model.player1Keys.push(key.addEventListener("click", () => controller.player1BoardKeyClick(i)))
//player 2
key = document.getElementById(`player2_cell_${i.toString()}`)
model.player2Keys.push(key.addEventListener("click", () => controller.player2BoardKeyClick(i)))
}
}
const createVictor = () => {
const imgNode = document.createElement("IMG")
const classAttr = document.createAttribute("src")
classAttr.value = "stickFigure.png"
imgNode.setAttributeNode(classAttr)
imgNode.setAttribute("id", "victor")
const imgTextNode = document.createElement("H4")
const text = document.createTextNode("Victor")
imgTextNode.appendChild(text)
imgTextNode.setAttribute("id", "victor_title")
document.getElementById("instruction_text").appendChild(imgNode)
document.getElementById("instruction_text").appendChild(imgTextNode)
}
const init = () => {
bindBoardClickToKeys()
view.displayMessage('Welcome! Please read instructions..')
console.log('finish init render')
return 'complete'
}
window.onload = init
// Testing with Vanilla JS
// Check Developer Console after running
const runTestingSuite = () => {
const Board = [0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0]
//make sure all models load correctly at start
assertArraysEqual(model.player1Board, Board, 'Expect player1 empty board to render on start')
assertArraysEqual(model.player2Board, Board, 'Expect player2 empty board to render on start')
assertEquals(model.player1Ships, 0, 'Expect player1 to have 0 ships placed on start')
assertEquals(model.player2Ships, 0, 'Expect player2 to have 0 ships placed on start')
assertEquals(model.player1LoadedShips, false, 'Expect player1 to have not loaded any ships on start')
assertEquals(model.player2LoadedShips, false, 'Expect player2 to have not loaded any ships on start')
assertEquals(model.player1Keys.length, 25, 'Expect player1 to have all click binded keys on start')
assertEquals(model.player2Keys.length, 25, 'Expect player2 to have all click binded keys on start')
assertEquals(model.playerAlreadyWon, false, 'Expect playerAlreadyWon to be false')
assertEquals(model.turn, 'player1', 'Expect first turn to be player1')
//check if message function works
view.displayMessage('testing... Please check Developer Console for information')
assertEquals(document.getElementById("messages").childNodes.length, 3, 'Expect view.displayMessage to display messages')
//check place ships work player 1
controller.player1BoardKeyClick(0)
controller.player1BoardKeyClick(1)
controller.player1BoardKeyClick(2)
if (model.player1LoadedShips) {
console.log('PASS: Expect Player1 ship placements to work')
} else {
console.log('FAIL: Expect Player1 ship placements to work')
}
//check player turn works after player1 places ships
if (model.turn === "player2") {
console.log(`PASS: Expect turn to switch after player1 places ships`)
} else {
console.log(`FAIL: Expected turns to switch after player1 places ships`)
}
//check place ships work player 2
controller.player2BoardKeyClick(0)
controller.player2BoardKeyClick(1)
controller.player2BoardKeyClick(2)
if (model.player2LoadedShips) {
console.log('PASS: Expect Player2 ship placements to work')
} else {
console.log('FAIL: Expect Player2 ship placements to work')
}
//check player turn works after player2 places ships
if (model.turn === "player1") {
console.log(`PASS: Expect turn to switch after player2 places ships`)
} else {
console.log(`FAIL: Expected turns to switch after player2 places ships`)
}
//player 2 click to miss
controller.player2BoardKeyClick(5)
if (model.player2Board[5] === 3) {
console.log(`PASS: Expect Player2 Board click from empty to miss`)
} else {
console.log(`FAIL: Expected Player2 Board click on empty spot does not equal miss`)
}
//Check Turn Swap Works
if (model.turn === "player2") {
console.log(`PASS: Expect turn to switch after player1 miss fire`)
} else {
console.log(`FAIL: Expected turn to switch after player1 miss fire`)
}
//check if miss/click working properly for player 1
controller.player1BoardKeyClick(5)
if (model.player1Board[5] === 3) {
console.log(`PASS: Expect Player1 Board click from empty to miss`)
} else {
console.log(`FAIL: Expeceted Player1 Board click on empty spot to equal miss`)
}
//Check Turn Swap Works
if (model.turn === "player1") {
console.log(`PASS: Expect turn to switch after player2 miss fire`)
} else {
console.log(`FAIL: Expected turn to switch after player2 miss fire`)
}
//player1 hit
controller.player2BoardKeyClick(0)
if (model.player2Board[0] === 2) {
console.log('PASS: Expect Player2 Board click to go from empty to hit')
} else {
console.log('FAIL: Expected player2 Board click to go from empty to hit')
}
//Check Turn Swap Works
if (model.turn === "player2") {
console.log(`PASS: Expect turn to switch after player2 hit fire`)
} else {
console.log(`FAIL: Expected turn to switch after player2 hit fire`)
}
//player2 hit
controller.player1BoardKeyClick(0)
if (model.player1Board[0] === 2) {
console.log('PASS: Expect Player1 Board click to go from empty to hit')
} else {
console.log('PASS: Expected Player1 Board click to go from empty to hit')
}
//Check Turn Swap Works
if (model.turn === "player1") {
console.log(`PASS: Expect turn to switch after player1 miss fire`)
} else {
console.log(`FAIL: Expected turn to switch after Player1 miss fire`)
}
//Check already hit player 2 board
controller.player2BoardKeyClick(0)
if (model.alreadySunkTestBoolean1) {
console.log('PASS: Expect already sunk to work for player2 board')
} else {
console.log('FAIL: Expected already sunk to have work for player2 board')
}
// check turn still same player
if (model.turn === "player1") {
console.log('PASS: Expected turn to stay on Player1')
} else {
console.log('FAIL: Expected turn to still be Player1')
}
//check already miss player 2 board
controller.player2BoardKeyClick(5)
if (model.alreadyMissTestBoolean1) {
console.log('PASS: Expect already miss to work on player2 board')
} else {
console.log('FAIL: Expected already miss to work on player2 board')
}
//check turn still same player
if (model.turn === "player1") {
console.log('PASS: Expected turn to stay on Player1')
} else {
console.log('FAIL: Expected turn to still be player1')
}
model.turn = 'player2'
//check already hit player 1 board
controller.player1BoardKeyClick(0)
if (model.alreadySunkTestBoolean2) {
console.log('PASS: Expect already sunk test to work Player1 board')
} else {
console.log('FAIL: Expected already sunk test to work on Player1 board')
}
//check turn still same player
if (model.turn === "player2") {
console.log('PASS: Expected turn to stay on Player2')
} else {
console.log('FAIL: Expected turn to still be Player2')
}
// check already miss player 1 board
controller.player1BoardKeyClick(5)
if (model.alreadyMissTestBoolean2) {
console.log('PASS: Expect already miss test to work Player1 board')
} else {
console.log('FAIL: Expected already miss test to work on Player1 board')
}
// check turn still player2
if (model.turn === "player2") {
console.log('PASS: Expected turn to stay on Player2')
} else {
console.log('FAIL: Expected turn to still be Player2')
}
controller.player1BoardKeyClick(1)
model.turn = 'player2'
controller.player1BoardKeyClick(2)
// check if win works
if (model.playerAlreadyWon) {
console.log('PASS: Expect win to work')
} else {
console.log('FAIL: Expected win to work')
}
// check if win from dead works
controller.player2BoardKeyClick(1)
model.turn = 'player1'
controller.player2BoardKeyClick(2)
if (model.winFromDeadTestBoolean) {
console.log('PASS: Expect After death win to work')
} else {
console.log('FAIL: Expected After death win to work')
}
console.log('All Tests completed')
view.displayMessage('Testing complete, please check log for more information..')
}
// check if a = b
const assertEquals = (a, b, string) => {
if (a == b) {
console.log(`PASS: ${string}`)
return true
} else {
console.log(`FAIL ${string}: Expected ${a} to equal ${b}`)
return false
}
}
// arrays have to be same order to pass
const assertArraysEqual = (arr1, arr2, string) => {
if (arr1.length !== arr2.length) {
console.log(`FAIL ${string}: Expected ${arr1} to equal ${arr2}`)
return false
}
for (let i = 0; i < arr1.length; i += 1) {
if (arr1[i] !== arr2[i]) {
console.log(`FAIL ${string}: Expected ${arr1} to equal ${arr2}`)
return false
}
}
console.log(`PASS ${string}`)
return true
}