-
Notifications
You must be signed in to change notification settings - Fork 0
/
game logic.js
90 lines (64 loc) · 2 KB
/
game logic.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
let playerText = document.getElementById('PlayerText')
let restartBtn = document.getElementById('RestartBtn')
let boxes = Array.from (document.getElementsByClassName('box'))
let winnerIndicator = getComputedStyle(document.body).getPropertyValue('--winning-blocks')
console.log(boxes)
const O_TEXT = "O"
const X_TEXT = "X"
let currentPlayer = X_TEXT
let spaces = Array(9).fill(null)
console.log(spaces)
//function for starting the game
const startGame = () => {
boxes.forEach(box => box.addEventListener('click', boxClicked))
}
//conditions for alternating players
function boxClicked(e) {
const id = e.target.id
if(!spaces[id]){
spaces[id] = currentPlayer
e.target.innerText = currentPlayer
if(playerHasWon() !==false){
playerText = '${currentPlayer} has won!'
let winning_blocks = playerHasWon()
winning_blocks.map( box => boxes[box].style.backgroundColor= winnerIndicator)
boxes.forEach(box => box.removeEventListener('click', boxClicked))
return
}
currentPlayer = currentPlayer == X_TEXT ? O_TEXT : X_TEXT
}
}
// winning combinations criteria
const winningCombos = [
[0,1,2],
[3,4,5],
[6,7,8],
[8,3,6],
[1,4,7],
[2,5,8],
[0,4,8],
[2,4,6]
]
//function for when the player wins
function playerHasWon() {
for (const condition of winningCombos ) {
let [a,b,c] = condition
if(spaces[a] && (spaces[a] == spaces[b]) && spaces[a] == spaces[c]) {
return[a,b,c]
}
}
return false
}
// restart button functionality
restartBtn.addEventListener('click', restart)
function restart() {
spaces.fill(null)
boxes.forEach( box => {
box.innerText = ''
box.style.backgroundColor=''
boxes.forEach(box => box.addEventListener('click', boxClicked))
})
currentPlayer = X_TEXT
}
//start the game
startGame()