-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
751 lines (605 loc) · 16.6 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
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
'use strict'
const pubSub = (function () {
const eventListeners = new Map()
function ensureEventArrayExists(event) {
if (!eventListeners.has(event)) eventListeners.set(event, [])
}
function register(eventName, handler) {
ensureEventArrayExists(eventName)
eventListeners.get(eventName).push(handler)
}
function unregister(eventName, handler) {
ensureEventArrayExists(eventName)
const existsHandlers = eventListeners.get(eventListeners)
const filteredHandlers = existsHandlers.filter(existsHandler => existsHandler !== handler)
eventListeners.set(eventName, filteredHandlers)
}
function notify(eventName, ...args) {
ensureEventArrayExists()
eventListeners.get(eventName).forEach(handler => handler(...args))
}
return {
register,
unregister,
notify,
}
})()
const displayController = (function () {
const MSG_TIE = 0
const MSG_WIN = 1
const msgEl = document.querySelector('.msg')
const againBtn = document.querySelector('button.again')
const roundEl = document.querySelector('.round-number')
const resetBtn = document.querySelector('.reset-btn')
const restartBtn = document.querySelector('.restart-btn')
function showMessage(content, msgType) {
msgEl.classList.remove('msg-tie', 'msg-winning')
msgEl.classList.add(msgType === MSG_WIN ? 'msg-winning' : 'msg-tie')
msgEl.textContent = content
msgEl.classList.remove('d-none')
}
function init() {
bindEvents()
}
function bindEvents() {
againBtn.addEventListener('click', handleAgainBtnClick)
resetBtn.addEventListener('click', resetBtnClicked)
restartBtn.addEventListener('click', restartBtnClicked)
}
function resetBtnClicked() {
pubSub.notify('resetButtonClicked')
}
function restartBtnClicked() {
pubSub.notify('restartButtonClicked')
}
function handleAgainBtnClick() {
pubSub.notify('restartGame')
}
function hideMessage() {
msgEl.classList.add('d-none')
}
function showAgainBtn() {
againBtn.classList.remove('d-none')
}
function hideAgainBtn() {
againBtn.classList.add('d-none')
}
function setRound(num) {
roundEl.textContent = num
}
init()
return {
MSG_TIE,
MSG_WIN,
showMessage,
hideMessage,
showAgainBtn,
hideAgainBtn,
setRound,
}
})()
const modal = (function () {
const SINGLE = 0
const MULTI = 1
const computerName = 'computer 🤖'
const el = document.querySelector('.modal')
const overlayEl = document.querySelector('.overlay')
const closeIcon = el.querySelector('.close-icon')
const [modeSingleBtn, modeMultiBtn] = el.querySelectorAll('.game-mode-ctr button')
const nameInputs = el.querySelectorAll('.player-name-input')
const nameLabels = el.querySelectorAll('.player-name-label')
const dimensionInput = el.querySelector('.board-dimension')
const actionBtn = el.querySelector('.action-btn')
let hideEnabled = false
let currentMode = SINGLE
function init() {
bindEvents()
}
function bindEvents() {
closeIcon.addEventListener('click', handleCloseClick)
overlayEl.addEventListener('click', handleCloseClick)
;[modeSingleBtn, modeMultiBtn].forEach(btn => btn.addEventListener('click', switchGameMode))
nameInputs.forEach(inp => inp.addEventListener('input', highlightInputOnError))
actionBtn.addEventListener('click', handleActionBtn)
}
function isInputValid(inp) {
const val = inp.value.trim()
return val && (currentMode === SINGLE ? val.toLowerCase() !== computerName : true)
}
function clearInputs() {
nameInputs.forEach(inp => {
inp.value = inp.dataset.initialValue
inp.classList.remove('invalid')
})
dimensionInput.value = dimensionInput.dataset.initialValue
}
function handleCloseClick(e) {
if (hideEnabled) hide()
}
function switchGameMode(e) {
if (e.target.classList.contains('selected')) return
modeSingleBtn.classList.toggle('selected')
modeMultiBtn.classList.toggle('selected')
nameLabels[1].classList.toggle('d-none')
nameInputs[1].classList.toggle('d-none')
currentMode = modeSingleBtn.classList.contains('selected') ? SINGLE : MULTI
clearInputs()
}
function highlightInputOnError(e) {
if (isInputValid(e.target)) e.target.classList.remove('invalid')
else e.target.classList.add('invalid')
}
function handleActionBtn(e) {
let isValid = true
nameInputs.forEach(inp => {
if (isInputValid(inp) || inp.classList.contains('d-none')) return
inp.classList.add('invalid')
isValid = false
})
if (!isValid) return
const opts = {
dimensions: Number(dimensionInput.value),
player1: {
name: nameInputs[0].value,
marker: 'X',
},
player2: {
name: currentMode === SINGLE ? computerName : nameInputs[1].value,
marker: 'O',
},
}
if (opts.player1.name === opts.player2.name) {
opts.player1.name += ' 1'
opts.player2.name += ' 2'
}
if (currentMode === SINGLE) opts.player2.isComputer = true
pubSub.notify('GameOptionsCompleted', opts)
}
function reset() {
clearInputs()
modeSingleBtn.classList.add('selected')
modeMultiBtn.classList.remove('selected')
nameLabels[1].classList.add('d-none')
nameInputs[1].classList.add('d-none')
currentMode = SINGLE
}
function hide(withReset = true) {
if (withReset) reset()
el.classList.add('d-none')
overlayEl.classList.add('d-none')
}
function show() {
el.classList.remove('d-none')
overlayEl.classList.remove('d-none')
}
function enableClosing() {
closeIcon.classList.remove('disabled')
hideEnabled = true
}
init()
return {
show,
hide,
reset,
enableClosing,
}
})()
const board = (function () {
const WIN_CNT = 3
const STATE_ON_PROGRESS = 0
const STATE_TIE = 1
const STATE_WIN = 2
const el = document.querySelector('.board')
const cellEls = []
const cellTemplate = el.querySelector('script.cell-template').innerHTML
let enabled = false
let isSpinning = false
let n = null
let m = null
const grid = []
const winningPositions = []
function init() {
bindEvents()
}
function bindEvents() {
el.addEventListener('click', handleBoardClick)
}
function handleBoardClick(e) {
if (!enabled || isSpinning) return
const cellEl = e.target.closest('.cell')
if (!cellEl) return
const [i, j] = cellEl.dataset.idx.split(' ').map(idx => Number(idx))
if (!isEmpty(i, j)) return
pubSub.notify('cellClick', i, j)
}
function build(newN, newM) {
destroy()
;[n, m] = [newN, newM]
el.style.cssText = `
grid-template-columns: repeat(${n}, 1fr);
grid-template-rows: repeat(${m}, 1fr);
`
for (let i = 0; i < n; i++) grid.push(new Array(m).fill(' '))
let cellsMarkup = ''
for (let i = 0; i < n; i++)
for (let j = 0; j < m; j++)
cellsMarkup += cellTemplate.replace(/{{i}}/g, i).replace(/{{j}}/g, j)
el.insertAdjacentHTML('beforeend', cellsMarkup)
const createdCellEls = el.querySelectorAll('.cell')
for (let i = 0, k = 0; i < n; i++) {
cellEls.push([])
for (let j = 0; j < m; j++) cellEls[cellEls.length - 1].push(createdCellEls[k++])
}
}
function clearBoard() {
// board: winning spinning disabled
el.classList.remove('winning', 'spinning')
}
function clearCell(i, j, render = true) {
// cell: filled cell-x cell-o winning
if (render) {
cellEls[i][j].textContent = ' '
cellEls[i][j].classList.remove('filled', 'cell-x', 'cell-o', 'winning')
}
grid[i][j] = ' '
}
function clearCells() {
cellEls.forEach((arr, i) => {
arr.forEach((_, j) => clearCell(i, j))
})
winningPositions.splice(0, winningPositions.length)
}
function clear() {
clearBoard()
clearCells()
}
function destroy() {
cellEls.forEach(arr => {
arr.forEach(cellEl => cellEl.remove())
})
cellEls.splice(0, cellEls.length)
grid.splice(0, grid.length)
winningPositions.splice(0, winningPositions.length)
clearBoard()
n = m = null
}
function enable() {
enabled = true
el.classList.remove('disabled')
}
function disable() {
enabled = false
el.classList.add('disabled')
}
function toggleSpinner() {
el.classList.toggle('spinning')
isSpinning = !isSpinning
}
function setCell(i, j, marker, render = true) {
if (render) {
if (!isEmpty(i, j)) clearCell(i, j)
cellEls[i][j].classList.add('filled', `cell-${marker.toLowerCase()}`)
cellEls[i][j].textContent = marker
}
grid[i][j] = marker
}
function checkWinningAtPos(i, j, stepI, stepJ) {
const iEnd = i + stepI * WIN_CNT
const jEnd = j + stepJ * WIN_CNT
if (iEnd > n || jEnd > m) return false
const set = new Set()
// same row, or same column already in End position
while (i !== iEnd || j !== jEnd) {
set.add(grid[i][j])
i += stepI
j += stepJ
}
return set.size === 1 && !isEmpty(i - stepI, j - stepJ)
}
function getState() {
console.assert(n !== null)
let isWinning = false
let numFilledCells = 0
let i, j
let stepI, stepJ
outer: for (i = 0; i < n; i++)
for (j = 0; j < m; j++) {
if (!isEmpty(i, j)) numFilledCells++
// row
if (checkWinningAtPos(i, j, 0, 1)) {
stepI = 0
stepJ = 1
isWinning = true
break outer
}
// column
if (checkWinningAtPos(i, j, 1, 0)) {
stepI = 1
stepJ = 0
isWinning = true
break outer
}
// main diagonal
if (checkWinningAtPos(i, j, 1, 1)) {
stepI = 1
stepJ = 1
isWinning = true
break outer
}
// secondary diagonal
if (checkWinningAtPos(i, j, 1, -1)) {
stepI = 1
stepJ = -1
isWinning = true
break outer
}
}
if (!isWinning)
return {
state: numFilledCells === n * m ? STATE_TIE : STATE_ON_PROGRESS,
}
winningPositions.splice(0, winningPositions.length)
for (let k = 0; k < WIN_CNT; k++) winningPositions.push([i + stepI * k, j + stepJ * k])
return {
state: STATE_WIN,
marker: grid[i][j],
}
}
function highlightWinningCells() {
winningPositions.forEach(pos => {
const [i, j] = pos
cellEls[i][j].classList.add('winning')
})
}
function isEmpty(i, j) {
return grid[i][j] === ' '
}
function at(i, j) {
return grid[i][j]
}
function toString() {
return grid.map(row => row.join('')).join('|')
}
init()
return {
build,
enable,
disable,
setCell,
clearCell,
toggleSpinner,
highlightWinningCells,
getState,
clear,
isEmpty,
get n() {
return n
},
get m() {
return m
},
get dimensions() {
return [n, m]
},
at,
toString,
STATE_ON_PROGRESS,
STATE_TIE,
STATE_WIN,
}
})()
function PlayerFactory(name, marker, el) {
const nameEl = el.querySelector('.player-name')
const markerEl = el.querySelector('.player-marker')
const scoreEl = el.querySelector('.player-score')
let score = 0
function init() {
render()
clear()
}
function render() {
nameEl.textContent = name
markerEl.textContent = marker
scoreEl.textContent = score
}
function toggleActive() {
el.classList.toggle('active')
}
function incrementScore() {
score++
scoreEl.textContent = score
}
function markWinner() {
el.classList.add('winner')
}
function markLoser() {
el.classList.add('loser')
}
function clear() {
el.classList.remove('active', 'winner', 'loser')
}
function getName() {
return name
}
function getMarker() {
return marker
}
function getScore() {
return score
}
init()
return {
toggleActive,
markWinner,
markLoser,
incrementScore,
clear,
get name() {
return getName()
},
get marker() {
return getMarker()
},
get score() {
return getScore()
},
}
}
function ComputerPlayerFactory(name, marker, el) {
function getBoardState(board) {
const { state, marker: winningMarker = null } = board.getState()
switch (state) {
case board.STATE_WIN:
return marker === winningMarker ? +1 : -1
case board.STATE_TIE:
return 0
default:
return null
}
}
function flipMarker(marker) {
return marker === 'X' ? 'O' : 'X'
}
function isBetter(a, b, isMyMove) {
if (isMyMove) return a > b
return a < b
}
const cache = new Map()
function minMax(board, marker, isMyMove) {
const cacheKey = `${isMyMove ? 1 : 0}${board.toString()}`
if (cache.has(cacheKey)) return cache.get(cacheKey)
const state = getBoardState(board)
if (state !== null) {
const ret = { cost: state, pos: null }
cache.set(cacheKey, ret)
return ret
}
let bestCost = 2 * (isMyMove ? -1 : +1)
let bestPos = null
outer: for (let i = 0; i < board.n; i++)
for (let j = 0; j < board.m; j++)
if (board.isEmpty(i, j)) {
board.setCell(i, j, marker, false)
const { cost } = minMax(board, flipMarker(marker), !isMyMove)
board.clearCell(i, j)
if (!isBetter(cost, bestCost, isMyMove)) continue
bestCost = cost
bestPos = [i, j]
if (bestCost === (isMyMove ? +1 : -1)) break outer
}
const ret = { cost: bestCost, pos: bestPos }
cache.set(cacheKey, ret)
return ret
}
function makeChoice(board) {
const { pos } = minMax(board, marker, true)
return pos
}
return Object.assign(
{
makeChoice,
isComputer: true,
},
PlayerFactory(name, marker, el)
)
}
const controller = (function () {
let round = 0
const players = [null, null]
let curPlayer = null
let otherPlayer = null
const playerParentEls = [
document.querySelector('.player-1'),
document.querySelector('.player-2'),
]
function init() {
bindEvents()
modal.show()
}
function switchPlayer() {
curPlayer = curPlayer === players[0] ? players[1] : players[0]
inferOtherPlayer()
}
function inferOtherPlayer() {
otherPlayer = curPlayer === players[0] ? players[1] : players[0]
}
function bindEvents() {
pubSub.register('GameOptionsCompleted', createGame)
pubSub.register('cellClick', handleCellClick)
pubSub.register('restartGame', restartGame)
pubSub.register('resetButtonClicked', resetBtnClicked)
pubSub.register('restartButtonClicked', restartGame)
}
function createGame(opts) {
displayController.hideMessage()
displayController.hideMessage()
displayController.hideAgainBtn()
modal.hide()
modal.enableClosing()
board.build(opts.dimensions, opts.dimensions)
players[0] = PlayerFactory(opts.player1.name, opts.player1.marker, playerParentEls[0])
if (opts.player2.isComputer)
players[1] = ComputerPlayerFactory(
opts.player2.name,
opts.player2.marker,
playerParentEls[1]
)
else players[1] = PlayerFactory(opts.player2.name, opts.player2.marker, playerParentEls[1])
curPlayer = players[0]
inferOtherPlayer()
round = 0
displayController.setRound(round + 1)
curPlayer.toggleActive()
board.enable()
}
function makeComputerMove() {
board.toggleSpinner()
setTimeout(() => {
const [i, j] = curPlayer.makeChoice(board)
board.toggleSpinner()
handleCellClick(i, j)
}, 50)
}
function handleCellClick(i, j) {
board.setCell(i, j, curPlayer.marker)
const { state: gameState } = board.getState()
if (gameState === board.STATE_ON_PROGRESS) {
curPlayer.toggleActive()
switchPlayer()
curPlayer.toggleActive()
if (curPlayer.isComputer) {
makeComputerMove()
}
} else {
board.disable()
displayController.showAgainBtn()
round++
if (gameState === board.STATE_WIN) {
board.highlightWinningCells()
curPlayer.incrementScore()
curPlayer.markWinner()
otherPlayer.markLoser()
displayController.showMessage(`${curPlayer.name} wins!`, displayController.MSG_WIN)
} else {
displayController.showMessage(`tie`, displayController.MSG_TIE)
}
}
}
function restartGame() {
board.clear()
players.forEach(p => p.clear())
curPlayer = players[round & 1]
inferOtherPlayer()
curPlayer.toggleActive()
displayController.hideMessage()
displayController.hideAgainBtn()
displayController.setRound(round + 1)
board.enable()
if (curPlayer.isComputer) makeComputerMove()
}
function resetBtnClicked() {
modal.show()
}
init()
})()
document.querySelector('.copyright-year').textContent = new Date().getFullYear()