-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (34 loc) · 969 Bytes
/
index.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
import Game from './game.js'
let p1, p2
while (!p1) {
p1 = window.prompt('Enter player 1 name:')
}
while (!p2 && p1 !== p2) {
p2 = window.prompt(p1 === p2
? `Please enter a different name than ${p1}.`
: 'Enter player 2 name:')
}
window.onload = () => {
const game = new Game(p1, p2)
const turn = document.getElementById('turn')
const player = document.getElementById('player')
player.innerText = game.player
document.querySelectorAll('td').forEach((el) => {
el.onclick = (evt) => {
el.onclick = undefined
evt.target.innerText = game.sym
evt.target.onclick = undefined
const [row, col] = evt.target.classList
game.turn(row, col)
if (game.hasWinner()) {
turn.innerText = `${game.player} wins!`
document.querySelectorAll('td').forEach(el => {
el.onclick = undefined
})
} else {
game.nextPlayer()
player.innerText = game.player
}
}
})
}