-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
351 lines (305 loc) · 9.23 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
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
/*
== battleship game ==
prerequisite:
1. render board (10 x 10) row = A - J, col = 1 - 10
2. two player
feature:
1. add only a ship each type (carrier, destroyer, patrol, submarine, battleship)
2. attack the ship
3. show attack status (miss or hit the ship)
*/
const boardEl = document.getElementById("board")
const descEl = document.getElementById("description")
const playEl = document.getElementById("play")
const rowNames = Array(10).fill(0).map((x, idx) => String.fromCharCode(65+idx) )
const colNames = Array(10).fill(0).map((x, idx) => idx)
let playerOneBoard = []
let playerTwoBoard = []
let hitCounter = 0
const shipData = {
carrier: { size: 5 },
battleship: { size: 4 },
destroyer: { size: 3 },
submarine: { size: 3 },
patrol: { size: 2 },
}
let playerOneShip = []
let playerTwoShip = []
let playNow = false
let playerOneTurn = true
let cheatMode = false
document.getElementById("submit").addEventListener("click", onAddShip)
document.getElementById("changePlayer").addEventListener("click", changePlayer)
document.getElementById("play").addEventListener("click", changePlayStatus)
function updateBoard () {
playerOneBoard = rowNames.map(x => Array(colNames.length).fill({type: "water", hit: false}))
playerTwoBoard = rowNames.map(x => Array(colNames.length).fill({type: "water", hit: false}))
}
function renderBoard () {
boardEl.innerHTML = ""
if (playerOneShip.length > 4 && playerTwoShip.length > 4) {
playEl.style.display = "block"
} else if (playerOneShip.length > 4) {
} else if (playerTwoShip.length > 4) {
}
if (playNow) {
if (playerOneTurn) {
if (!cheatMode) descEl.innerText = "Now playing: Player One"
} else {
if (!cheatMode) descEl.innerText = "Now playing: Player Two"
}
} else {
if (playerOneTurn) {
descEl.innerText = `${playerOneShip.length > 4 ? "READY" : "PREPARE"} for WAR: Player One`
} else {
descEl.innerText = `${playerTwoShip.length > 4 ? "READY" : "PREPARE"} for WAR: Player Two`
}
}
for (var i = 0; i < rowNames.length; i++) {
renderRow(i)
for (var j = 0; j < colNames.length; j++) {
renderBox(i, j)
}
}
}
function renderRow (row) {
if (row == 0) {
const coordDiv = document.createElement("DIV")
coordDiv.style.display = "flex"
coordDiv.style.flexDirection = "row"
for (var i = 0; i < colNames.length + 1; i++) {
const colDiv = document.createElement("DIV")
const colText = document.createTextNode(Number(i))
colDiv.classList.add("coord")
colDiv.appendChild(colText)
coordDiv.appendChild(colDiv)
}
boardEl.appendChild(coordDiv)
}
var div = document.createElement("DIV")
div.setAttribute("id", "box-" + row)
div.classList.add("row")
boardEl.appendChild(div)
}
function renderBox (row, col) {
if (col == 0) {
const rowDiv = document.createElement("DIV")
const rowText = document.createTextNode(rowNames[row])
rowDiv.classList.add("coord")
rowDiv.appendChild(rowText)
document.getElementById("box-" + row).appendChild(rowDiv)
}
const rowOfBoxes = document.getElementById("box-" + row)
const box = document.createElement("BUTTON")
let selectedBox
if (playNow) {
if (playerOneTurn) {
selectedBox = playerTwoBoard[row][col]
} else {
selectedBox = playerOneBoard[row][col]
}
if (selectedBox.hit == true) {
if (selectedBox.type != "water") {
box.classList.add("hit-ship")
box.appendChild(document.createTextNode(selectedBox.type[0]))
} else {
box.classList.add("hit")
}
}
} else {
if (playerOneTurn) {
selectedBox = playerOneBoard[row][col]
} else {
selectedBox = playerTwoBoard[row][col]
}
if (selectedBox.type != "water") {
box.classList.add("ship")
box.classList.add(selectedBox.type)
}
}
box.classList.add("box")
box.setAttribute("id", "box-"+row+"-"+col)
box.addEventListener("click", attack)
rowOfBoxes.appendChild(box)
}
function attack (e) {
if (!playNow) return false
if (playerOneTurn) {
if (isWin()) alert("Player 1 WIN")
} else {
if (isWin()) alert("Player 2 WIN")
}
const id = e.target.id
const ids = id.split("-")
const row = ids[1]
const col = ids[2]
if (playerOneTurn) {
const selectedBox = playerTwoBoard[row][col]
if (selectedBox.hit == true) {
document.getElementById("status").innerText = "sudah menembak koordinat ini"
} else if (selectedBox.type != "water") {
document.getElementById("status").innerText = "you hit " + selectedBox.type
} else {
document.getElementById("status").innerText = "your attack is miss!"
}
playerTwoBoard[row][col] = { ...selectedBox, hit: true}
} else {
const selectedBox = playerOneBoard[row][col]
if (selectedBox.hit == true) {
document.getElementById("status").innerText = "sudah menembak koordinat ini"
} else if (selectedBox.type != "water") {
document.getElementById("status").innerText = "you hit " + selectedBox.type
} else {
document.getElementById("status").innerText = "your attack is miss!"
}
playerOneBoard[row][col] = { ...selectedBox, hit: true}
}
if (cheatMode) {
hitCounter++
if (hitCounter == 5) {
changePlayer()
}
} else {
// changePlayer()
}
renderBoard()
}
function onAddShip (e) {
if (playNow) return false
const direction = document.getElementsByName("direction")[0].value
const shipName = document.getElementsByName("ship")[0].value
const addedShips = playerOneTurn ? playerOneShip : playerTwoShip
if (addedShips.indexOf(shipName) > -1) {
alert('Hanya boleh satu kapal untuk tiap jenis kapal')
return false
}
const startCoord = prompt("Enter start coordinate for your ship:")
if (startCoord) {
var regCol = /[0-9]+/
var regRow = /[a-zA-Z]+/
const detectedNumber = startCoord.match(regCol)
const detectedRow = startCoord.match(regRow)
const col = detectedNumber.length > 0 ? parseInt(detectedNumber[0]) - 1 : -1
const row = detectedRow.length > 0 ? rowNames.indexOf(detectedRow[0].toUpperCase()) : -1
if (col > -1 && row > -1) {
const start = { col, row }
const { status, message } = checkShip(shipName, start, direction)
if (status) {
alert(message)
return false
} else {
addShip(shipName, start, direction)
}
} else { alert('Hanya koordinat di arena tempur yang diperbolehkan') }
}
}
function addShip (shipName, start, direction) {
if (playNow) return false
const { size } = shipData[shipName]
const { row, col } = start
if (direction == 'hor') {
for (var i = col; i < col + size; i++ ) {
if (playerOneTurn) {
playerOneBoard[row][i] = { type: shipName, hit: false }
} else {
playerTwoBoard[row][i] = { type: shipName, hit: false }
}
}
} else if (direction == 'ver') {
for (var i = row; i < row + size; i++ ) {
if (playerOneTurn) {
playerOneBoard[i][col] = { type: shipName, hit: false }
} else {
playerTwoBoard[i][col] = { type: shipName, hit: false }
}
}
}
if (playerOneTurn) {
playerOneShip.push(shipName)
} else {
playerTwoShip.push(shipName)
}
renderBoard()
}
function checkShip (ship, start, direction) {
let existShip = []
let message
let status = false
const { row, col } = start
const { size } = shipData[ship]
if (direction == 'hor') {
if (col + size - 1> colNames.length - 1) {
return {
status: true,
message: 'Kapal tidak boleh keluar dari arena'
}
}
for (var i = col; i < col + size; i++ ) {
if (playerOneTurn) {
const selectedBox = playerOneBoard[row][i]
if (selectedBox.type != "water") {
existShip.push(selectedBox.type)
}
} else {
const selectedBox = playerTwoBoard[row][i]
if (selectedBox.type != "water") {
existShip.push(selectedBox.type)
}
}
}
} else if (direction == 'ver') {
if (row + size - 1 > rowNames.length - 1) {
return {
status: true,
message: 'Kapal tidak boleh keluar dari arena'
}
}
for (var i = row; i < row + size; i++ ) {
if (playerOneTurn) {
const selectedBox = playerOneBoard[i][col]
if (selectedBox.type != "water") {
existShip.push(selectedBox.type)
}
} else {
const selectedBox = playerTwoBoard[i][col]
if (selectedBox.type != "water") {
existShip.push(selectedBox.type)
}
}
}
}
return {
status: existShip.length > 0,
message: "Kapal menabrak dengan kapal lain"
}
}
function changePlayer () {
playerOneTurn = !playerOneTurn
hitCounter = 0
renderBoard()
}
function changePlayStatus () {
// playNow = status
playNow = true
renderBoard()
}
function toggleHint () {
cheatMode = !cheatMode
playerOneTurn = !playerOneTurn
playNow = !playNow
renderBoard()
}
function isWin (player) {
const playerTurn = player == 'p1' ? true : false
const selectedBoard = playerTurn ? playerTwoBoard : playerOneBoard
const filtered = selectedBoard.map(val => {
return val.filter(x => x.type != "water" && x.hit == false)
})
const existEnemy = filtered.flat()
return existEnemy.length < 1
}
function init () {
updateBoard()
renderBoard()
}
init()