-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
117 lines (108 loc) · 4.27 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
const tileOne = document.getElementById('tile-one')
const tileTwo = document.getElementById('tile-two')
const tileThree = document.getElementById('tile-three')
const tileFour = document.getElementById('tile-four')
const tileFive = document.getElementById('tile-five')
const tileSix = document.getElementById('tile-six')
const tileSeven = document.getElementById('tile-seven')
const tileEight = document.getElementById('tile-eight')
const tileNine = document.getElementById('tile-nine')
const tiles = [tileOne, tileTwo, tileThree, tileFour, tileFive, tileSix, tileSeven, tileEight, tileNine]
const winComboOne = [tileOne, tileTwo, tileThree]
const winComboTwo = [tileFour, tileFive, tileSix]
const winComboThree = [tileSeven, tileEight, tileNine]
const winComboFour = [tileOne, tileFour, tileSeven]
const winComboFive = [tileTwo, tileFive, tileEight]
const winComboSix = [tileThree, tileSix, tileNine]
const winComboSeven = [tileOne, tileFive, tileNine]
const winComboEight = [tileThree, tileFive, tileSeven]
const playerScore = document.getElementById('player-score')
const computerScore = document.getElementById('computer-score')
const resultDiv = document.getElementById('result-div')
let gameOver = false
function computer(i) {
let computerChoice = tiles[Math.floor(Math.random()*8)]
if (computerChoice === tiles[i]) {
console.log('same')
computer(i)
} else if (computerChoice.innerText === 'X') {
console.log('occupied by player')
computer(i)
} else if (computerChoice.innerText === 'O') {
console.log('occupied by computer')
computer(i)
} else {
computerChoice.innerText = 'O'
}
}
function resetGame() {
tileOne.innerText = ''
tileTwo.innerText = ''
tileThree.innerText = ''
tileFour.innerText = ''
tileFive.innerText = ''
tileSix.innerText = ''
tileSeven.innerText = ''
tileEight.innerText = ''
tileNine.innerText = ''
}
function playerWins() {
result.innerText = 'You Win!'
resultDiv.style.backgroundColor = 'green'
playerScore.innerText++
console.log('player wins')
resetGame()
}
function computerWins() {
result.innerText = 'Computer Wins!'
resultDiv.style.backgroundColor = 'red'
computerScore.innerText++
console.log('computer wins')
resetGame()
}
function tie() {
result.innerText = 'Draw!'
resultDiv.style.backgroundColor = '#cc9900'
console.log('draw')
resetGame()
}
function winCheck(winComboTiles) {
if (winComboTiles[0].innerText === 'X' && winComboTiles[1].innerText === 'X' && winComboTiles[2].innerText === 'X') {
playerWins()
return 0
} else if (winComboTiles[0].innerText === 'O' && winComboTiles[1].innerText === 'O' && winComboTiles[2].innerText === 'O') {
computerWins()
return 1
} else {
return 2
}
}
function winChecker() {
winCheck(winComboOne)
winCheck(winComboTwo)
winCheck(winComboThree)
winCheck(winComboFour)
winCheck(winComboFive)
winCheck(winComboSix)
winCheck(winComboSeven)
winCheck(winComboEight)
if (winCheck(winComboOne) == 2 && winCheck(winComboTwo) == 2 && winCheck(winComboThree) == 2 && winCheck(winComboFour) == 2 && winCheck(winComboFive) == 2 && winCheck(winComboSix) == 2 && winCheck(winComboSeven) == 2 && winCheck(winComboEight) == 2 && tileOne.innerText != '' && tileTwo.innerText != '' && tileThree.innerText != '' && tileFour.innerText != '' && tileFive.innerText != '' && tileSix.innerText != '' && tileSeven.innerText != '' && tileEight.innerText != '' && tileNine.innerText != '') {
tie()
}
}
for (let i = 0; i < tiles.length; i++) {
tiles[i].addEventListener('click', ()=> {
if (tiles[i].innerText === 'O' || tiles[i].innerText === 'X') {
return
} else {
tiles[i].innerText = 'X'
computer(i)
}
})
}
window.setInterval(()=> {
if (tileOne.innerText != '' && tileTwo.innerText != '' && tileThree.innerText != '' && tileFour.innerText != '' && tileFive.innerText != '' && tileSix.innerText != '' && tileSeven.innerText != '' && tileEight.innerText != '' && tileNine.innerText != '') {
winChecker()
}
winChecker()
}, 250)