-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
324 lines (270 loc) · 11.3 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
document.addEventListener('DOMContentLoaded', () => {
const userGrid = document.querySelector('.grid-user');
const computerGrid = document.querySelector('.grid-computer');
const displayGrid = document.querySelector('.grid-display');
const ships = document.querySelectorAll('.ship');
const destroyerContainer = document.querySelector('.destroyer-container');
const submarineContainer = document.querySelector('.submarine-container');
const cruiserContainer = document.querySelector('.cruiser-container');
const battleshipContainer = document.querySelector('.battleship-container');
const carrierContainer = document.querySelector('.carrier-container');
const startBtn = document.getElementById('start');
const rotateBtn = document.getElementById('rotate');
const turnDisplay = document.getElementById('whose-go');
const infoDisplay = document.getElementById('info');
const setupButtons = document.getElementById('setup-buttons');
const GRID_WIDTH = 10;
const userSquares = [];
const computerSquares = [];
let isHorizontal = true;
let isGameOver = false;
let isPlayersTurn = true;
let selectedShipNameWithIndex;
let draggedShip;
let draggedShipLength;
let destroyerCount = 0;
let submarineCount = 0;
let cruiserCount = 0;
let battleshipCount = 0;
let carrierCount = 0;
let cpuDestroyerCount = 0;
let cpuSubmarineCount = 0;
let cpuCruiserCount = 0;
let cpuBattleshipCount = 0;
let cpuCarrierCount = 0;
const shipArray = [
{
name: 'destroyer',
directions: [
[0, 1],
[0, GRID_WIDTH]
]
},
{
name: 'submarine',
directions: [
[0, 1, 2],
[0, GRID_WIDTH, GRID_WIDTH*2]
]
},
{
name: 'cruiser',
directions: [
[0, 1, 2],
[0, GRID_WIDTH, GRID_WIDTH*2]
]
},
{
name: 'battleship',
directions: [
[0, 1, 2, 3],
[0, GRID_WIDTH, GRID_WIDTH*2, GRID_WIDTH*3]
]
},
{
name: 'carrier',
directions: [
[0, 1, 2, 3, 4],
[0, GRID_WIDTH, GRID_WIDTH*2, GRID_WIDTH*3, GRID_WIDTH*4]
]
},
]
const checkForWins = () => {
if (destroyerCount === 2) {
infoDisplay.innerHTML = `You sunk the the enemy's destroyer`
destroyerCount = 10
}
if (submarineCount === 3) {
infoDisplay.innerHTML = `You sunk the the enemy's submarine`
submarineCount = 10
}
if (cruiserCount === 3) {
infoDisplay.innerHTML = `You sunk the the enemy's cruiser`
cruiserCount = 10
}
if (battleshipCount === 4) {
infoDisplay.innerHTML = `You sunk the the enemy's battleship`
battleshipCount = 10
}
if (carrierCount === 5) {
infoDisplay.innerHTML = `You sunk the the enemy's carrier`
carrierCount = 10
}
if (cpuDestroyerCount === 2) {
infoDisplay.innerHTML = `The enemy sunk your destroyer`
cpuDestroyerCount = 10
}
if (cpuSubmarineCount === 3) {
infoDisplay.innerHTML = `The enemy sunk your submarine`
cpuSubmarineCount = 10
}
if (cpuCruiserCount === 3) {
infoDisplay.innerHTML = `The enemy sunk your cruiser`
cpuCruiserCount = 10
}
if (cpuBattleshipCount === 4) {
infoDisplay.innerHTML = `The enemy sunk your battleship`
cpuBattleshipCount = 10
}
if (cpuCarrierCount === 5) {
infoDisplay.innerHTML = `The enemy sunk your carrier`
cpuCarrierCount = 10
}
if ((destroyerCount + submarineCount + cruiserCount + battleshipCount + carrierCount) === 50) {
infoDisplay.innerHTML = "YOU WIN"
gameOver()
}
if ((cpuDestroyerCount + cpuSubmarineCount + cpuCruiserCount + cpuBattleshipCount + cpuCarrierCount) === 50) {
infoDisplay.innerHTML = `ENEMY WINS`
gameOver()
}
}
const computerGo = () => {
let random = Math.floor(Math.random() * userSquares.length);
if (!userSquares[random].classList.contains('boom')) {
const hit = userSquares[random].classList.contains('taken');
userSquares[random].classList.add(hit ? 'boom' : 'miss')
if (userSquares[random].classList.contains('destroyer')) cpuDestroyerCount++;
if (userSquares[random].classList.contains('submarine')) cpuSubmarineCount++;
if (userSquares[random].classList.contains('cruiser')) cpuCruiserCount++;
if (userSquares[random].classList.contains('battleship')) cpuBattleshipCount++;
if (userSquares[random].classList.contains('carrier')) cpuCarrierCount++;
checkForWins();
} else computerGo();
isPlayersTurn = !isPlayersTurn;
displayTurn(isPlayersTurn);
}
const createBoard = (grid, squares, width) => {
for (let i = 0; i < width*width; i++) {
const square = document.createElement('div')
square.dataset.id = i
grid.appendChild(square)
squares.push(square)
}
}
const displayTurn = (isPlayersTurn) => {
if (isPlayersTurn) {
turnDisplay.innerHTML = 'Your Go';
} else {
turnDisplay.innerHTML = 'Computers Go';
}
}
const gameOver = () => {
isGameOver = true
setupButtons.style.display = 'block';
}
const generate = (ship) => {
let randomDirection = Math.floor(Math.random() * ship.directions.length)
let current = ship.directions[randomDirection]
if (randomDirection === 0) direction = 1
if (randomDirection === 1) direction = 10
let randomStart = Math.abs(Math.floor(Math.random() * computerSquares.length - (ship.directions[0].length * direction)))
const isTaken = current.some(index => computerSquares[randomStart + index].classList.contains('taken'))
const isAtRightEdge = current.some(index => (randomStart + index) % GRID_WIDTH === GRID_WIDTH - 1)
const isAtLeftEdge = current.some(index => (randomStart + index) % GRID_WIDTH === 0)
if (!isTaken && !isAtRightEdge && !isAtLeftEdge) current.forEach(index => computerSquares[randomStart + index].classList.add('taken', ship.name))
else generate(ship)
}
const playGame = () => {
if (isGameOver) return;
displayTurn(isPlayersTurn);
if (isPlayersTurn) {
computerSquares.forEach(square => square.addEventListener('click', function(e) {
shotFired = square.dataset.id
userGo(square);
}))
} else {
setTimeout(computerGo(), 1000);
}
}
const rotate = () => {
destroyerContainer.classList.toggle('destroyer-container-vertical')
submarineContainer.classList.toggle('submarine-container-vertical')
cruiserContainer.classList.toggle('cruiser-container-vertical')
battleshipContainer.classList.toggle('battleship-container-vertical')
carrierContainer.classList.toggle('carrier-container-vertical')
isHorizontal = !isHorizontal;
}
const userGo = (square) => {
if (square.classList.contains('miss') || square.classList.contains('boom')) {
return
}
if (square.classList.contains('destroyer')) destroyerCount++;
if (square.classList.contains('submarine')) submarineCount++;
if (square.classList.contains('cruiser')) cruiserCount++;
if (square.classList.contains('battleship')) battleshipCount++;
if (square.classList.contains('carrier')) carrierCount++;
checkForWins();
if (square.classList.contains('taken')) {
square.classList.add('boom')
} else {
square.classList.add('miss')
}
isPlayersTurn = !isPlayersTurn;
console.log('isPlayersTurn', isPlayersTurn)
playGame();
}
rotateBtn.addEventListener('click', () => rotate());
startBtn.addEventListener('click', () => {
generate(shipArray[0]);
generate(shipArray[1]);
generate(shipArray[2]);
generate(shipArray[3]);
generate(shipArray[4]);
setupButtons.classList.toggle('hidden');
turnDisplay.classList.toggle('hidden');
playGame();
});
createBoard(userGrid, userSquares, GRID_WIDTH);
createBoard(computerGrid, computerSquares, GRID_WIDTH);
ships.forEach(ship => ship.addEventListener('dragstart', dragStart))
userSquares.forEach(square => square.addEventListener('dragstart', dragStart))
userSquares.forEach(square => square.addEventListener('dragover', dragOver))
userSquares.forEach(square => square.addEventListener('dragenter', dragEnter))
userSquares.forEach(square => square.addEventListener('drop', dragDrop))
ships.forEach(ship => ship.addEventListener('mousedown', (e) => {
selectedShipNameWithIndex = e.target.id;
}))
const getDirection = (shipPosition, shipLength) => {
if (shipPosition === 0) return 'start';
if (shipPosition === shipLength - 1) return 'end';
return 'middle';
}
function dragStart() {
draggedShip = this;
draggedShipLength = this.childNodes.length;
}
function dragOver(e) {
e.preventDefault();
}
function dragEnter(e) {
e.preventDefault();
}
function dragDrop() {
let shipNameWithLastId = draggedShip.lastElementChild.id;
let shipClass = shipNameWithLastId.slice(0, -2);
let lastShipIndex = parseInt(shipNameWithLastId.substr(-1));
let shipLastId = lastShipIndex + parseInt(this.dataset.id);
const notAllowedHorizontal = [100, 0,10,20,30,40,50,60,70,80,90,1,11,21,31,41,51,61,71,81,91,2,22,32,42,52,62,72,82,92,3,13,23,33,43,53,63,73,83,93]
const notAllowedVertical = [99,98,97,96,95,94,93,92,91,90,89,88,87,86,85,84,83,82,81,80,79,78,77,76,75,74,73,72,71,70,69,68,67,66,65,64,62,61,60]
let newNotAllowedHorizontal = notAllowedHorizontal.splice(0, 10 * lastShipIndex)
let newNotAllowedVertical = notAllowedVertical.splice(0, 10 * lastShipIndex)
selectedShipIndex = parseInt(selectedShipNameWithIndex.substr(-1));
shipLastId = shipLastId - selectedShipIndex;
if (userSquares[parseInt(this.dataset.id)].classList.contains('taken')) return;
if (isHorizontal && !newNotAllowedHorizontal.includes(shipLastId)) {
for (let i = 0; i < draggedShipLength; i++) {
let directionClass = getDirection(i, draggedShipLength);
userSquares[parseInt(this.dataset.id) - selectedShipIndex + i]
.classList.add('taken', 'horizontal', directionClass, shipClass);
}
} else if(!isHorizontal && !newNotAllowedVertical.includes(shipLastId)) {
for (let i = 0; i < draggedShipLength; i++) {
let directionClass = getDirection(i, draggedShipLength);
userSquares[parseInt(this.dataset.id) - selectedShipIndex + GRID_WIDTH * i]
.classList.add('taken', 'vertical', directionClass, shipClass);
}
} else return;
displayGrid.removeChild(draggedShip);
}
})