-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
111 lines (101 loc) · 2.81 KB
/
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
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
//document.querySelector('body').style.background = 'red'
const wins = [
[0, 1, 2],
[0, 3, 6],
[0, 4, 8],
[1, 4, 7],
[2, 4, 6],
[2, 5, 8],
[3, 4, 5],
[6, 7, 8],
]
let currentTurn = 'X'
let countturn = 0
let gameOver = false
let xwon = 0
let owon = 0
let tie = 0
const changeTurn = () => {
currentTurn = currentTurn === 'X' ? 'O' : 'X'
let turnDisplay = document.querySelector('.turn-display')
turnDisplay.innerText = currentTurn
}
const checkWin = () => {
let boxSymbol = document.getElementsByClassName('box-symbol')
wins.forEach((combination) => {
if (
boxSymbol[combination[0]].innerText ===
boxSymbol[combination[1]].innerText &&
boxSymbol[combination[1]].innerText ===
boxSymbol[combination[2]].innerText &&
boxSymbol[combination[0]].innerText !== ''
) {
gameOver = true
if (boxSymbol[combination[0]].innerText === 'X') {
countturn = 0
xwon++
} else if (boxSymbol[combination[0]].innerText === 'O') {
countturn = 0
owon++
}
let xScore = document.querySelector('.x-won')
xScore.innerText = xwon
let oScore = document.querySelector('.o-won')
oScore.innerText = owon
}
if (countturn === 9 && gameOver == false) {
gameOver = true
countturn = 0
tie++
let tiescore = document.querySelector('.tie')
tiescore.innerText = tie
}
})
}
var boxes = document.getElementsByClassName('box')
Array.from(boxes).forEach((element) => {
let boxSymbol = element.querySelector('.box-symbol')
element.addEventListener('click', () => {
if (boxSymbol.innerText === '' && gameOver === false) {
boxSymbol.innerText = currentTurn
countturn++
changeTurn()
checkWin()
}
})
})
let reset = document.querySelector('.Reset')
reset.addEventListener('click', () => {
boxes = document.getElementsByClassName('box')
Array.from(boxes).forEach((element) => {
boxSymbol = element.querySelector('.box-symbol')
boxSymbol.innerText = ''
})
gameOver = false
countturn = 0
currentTurn = 'X'
turnDisplay = document.querySelector('.turn-display')
turnDisplay.innerText = currentTurn
})
// Array.from(boxes).forEach((ele) => {
// ele.addEventListener('click', (e) => {
// alert(e.target.getAttribute('id'))
// })
// })
//const arrayOfStrings = ['Hello', 'World', 'JavaScript', 'Array', 'Example']
// // Adding a new element to the end of the array
// arrayOfStrings.push('New Element')
// console.log(arrayOfStrings) // Output: ["Hello", "World", "JavaScript", "Array", "Example", "New Element"]
// const arrayOfStrings = [
// 'Hello',
// 'World',
// 'JavaScript',
// 'Array',
// 'Example',
// 'New Element',
// ]
arrayOfStrings.forEach((str) => {
console.log(str)
})
// console.log(arrayOfStrings[0])
// console.log(arrayOfStrings[1])