-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
185 lines (158 loc) · 5.33 KB
/
Copy pathscript.js
File metadata and controls
185 lines (158 loc) · 5.33 KB
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
var numX = 0
var numO = 0
// the board
var board = document.getElementById("board")
const xOwns = []
const oOwns = []
const x = 'x' // redundant, but i thought i was gonna use these as vaiables later.
const o = 'o' // maybe i did and forgot? who knows
var turnNum = 2 // still uncertain if i'm actually callin stalemate correctly. all i know is i'm calling this project
for (let i = 0; i < 9; i++) {
const spot = document.createElement('div')
spot.classList.add('spot')
spot.setAttribute('id', i)
board.appendChild(spot)
spot.addEventListener('click', function(e) {
decideTurn(spot)
decideWinner(spot)
})
}
function decideTurn(spot) {
if (turnNum % 2 === 0 && spot.innerHTML !== o) {
player.one(spot)
} else if (turnNum % 2 === 1 && spot.innerHTML !== x) {
player.two(spot)
}
}
const player = {
one: (spot) => {
spot.innerHTML = x
spot.classList.add('x')
turnNum++
},
two: (spot) => {
spot.innerHTML = o
spot.classList.add('o')
turnNum++
}
}
var spotZero = document.getElementById('0')
var spotOne = document.getElementById('1')
var spotTwo = document.getElementById('2')
var spotThree = document.getElementById('3')
var spotFour = document.getElementById('4')
var spotFive = document.getElementById('5')
var spotSix = document.getElementById('6')
var spotSeven = document.getElementById('7')
var spotEight = document.getElementById('8')
function decideWinner(spot) {
// HORIZONALS ///
if (spotZero.classList.contains('x') && spotOne.classList.contains('x') && spotTwo.classList.contains('x')){
xWins()
}
if (spotZero.classList.contains('o') && spotOne.classList.contains('o') && spotTwo.classList.contains('o')){
oWins()
}
if (spotThree.classList.contains('x') && spotFour.classList.contains('x') && spotFive.classList.contains('x')){
xWins()
}
if (spotThree.classList.contains('o') && spotFour.classList.contains('o') && spotFive.classList.contains('o')){
oWins()
}
if (spotSix.classList.contains('x') && spotSeven.classList.contains('x') && spotEight.classList.contains('x')){
xWins()
}
if (spotSix.classList.contains('o') && spotSeven.classList.contains('o') && spotEight.classList.contains('o')){
oWins()
///// VERTICALS /////
}
if (spotZero.classList.contains('x') && spotThree.classList.contains('x') && spotSix.classList.contains('x')){
xWins()
}
if (spotZero.classList.contains('o') && spotThree.classList.contains('o') && spotSix.classList.contains('o')){
oWins()
}
if (spotOne.classList.contains('x') && spotFour.classList.contains('x') && spotSeven.classList.contains('x')){
xWins()
}
if (spotOne.classList.contains('o') && spotFour.classList.contains('o') && spotSeven.classList.contains('o')){
oWins()
}
if (spotTwo.classList.contains('x') && spotFive.classList.contains('x') && spotEight.classList.contains('x')){
xWins()
}
if (spotTwo.classList.contains('o') && spotFive.classList.contains('o') && spotEight.classList.contains('o')){
oWins()
}
///// DIAGONALS /////
if (spotZero.classList.contains('x') && spotFour.classList.contains('x') && spotEight.classList.contains('x')){
xWins()
}
if (spotZero.classList.contains('o') && spotFour.classList.contains('o') && spotEight.classList.contains('o')){
oWins()
}
if (spotTwo.classList.contains('x') && spotFour.classList.contains('x') && spotSix.classList.contains('x')){
xWins()
}
if (spotTwo.classList.contains('o') && spotFour.classList.contains('o') && spotSix.classList.contains('o')){
oWins()
}
///// truthfully, this is still messed up. it calls the game sometimes when it shouldn't. but my hands hurt and im tired. /////
else if (turnNum >= 11) {
stalemate()
}
}
function stalemate() {
alert('both of you suck, equally.')
reset()
}
function xWins() {
alert('X wins. you suck, O.')
reset()
tallyX()
}
function oWins() {
alert('O wins. you suck, X.')
reset()
tallyO()
}
function tallyX() {
let displayX = document.getElementById("displayX")
numX = numX + 1
displayX.innerHTML = numX
}
function tallyO() {
let displayO = document.getElementById("displayO")
numO = numO + 1
displayO.innerHTML = numO
}
function reset() {
turnNum = 2
spotZero.classList.remove('x')
spotZero.classList.remove('o')
spotZero.innerHTML = ''
spotOne.classList.remove('x')
spotOne.classList.remove('o')
spotOne.innerHTML = ''
spotTwo.classList.remove('x')
spotTwo.classList.remove('o')
spotTwo.innerHTML = ''
spotThree.classList.remove('x')
spotThree.classList.remove('o')
spotThree.innerHTML = ''
spotFour.classList.remove('x');
spotFour.classList.remove('o')
spotFour.innerHTML = ''
spotFive.classList.remove('x')
spotFive.classList.remove('o')
spotFive.innerHTML = ''
spotSix.classList.remove('x')
spotSix.classList.remove('o')
spotSix.innerHTML = ''
spotSeven.classList.remove('x')
spotSeven.classList.remove('o')
spotSeven.innerHTML = ''
spotEight.classList.remove('x')
spotEight.classList.remove('o')
spotEight.innerHTML = ''
}