-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
146 lines (126 loc) · 3.74 KB
/
Copy pathindex.js
File metadata and controls
146 lines (126 loc) · 3.74 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
// variables for players 1 & 2
const playerX = ('X');
const playerO = ('O');
// array with all winning combinations
const winningCombo = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];
// variables for all needed DOM elements
const cellElements = Array.from(document.querySelectorAll(`[class*="cell"]`, 'id'));
const boardElement = document.getElementById('board');
const winningMessageElement = document.getElementById('winningMessage');
const restartButton = document.getElementById('restartButton');
const winningMessageTextElement = document.getElementById('winningMessageText');
const xImage = document.getElementById('xImage');
const oImage = document.getElementById('oImage');
let isPlayerOTurn = Boolean
let currentClass = '';
let cellsWithXO = [];
const cellZero = document.getElementById('0')
const cellOne = document.getElementById('1')
const cellTwo = document.getElementById('2')
const cellThree = document.getElementById('3')
const cellFour = document.getElementById('4')
const cellFive = document.getElementById('5')
const cellSix = document.getElementById('6')
const cellSeven = document.getElementById('7')
const cellEight = document.getElementById('8')
start();
function start() {
isPlayerOTurn = false
cellElements.forEach(cell => {
document.querySelectorAll(`[class*="X"]`).remove;
document.querySelectorAll(`[class*="O"]`).remove;
cellZero.addEventListener('click', clickCell);
cellOne.addEventListener('click', clickCell);
cellTwo.addEventListener('click', clickCell);
cellThree.addEventListener('click', clickCell);
cellFour.addEventListener('click', clickCell);
cellFive.addEventListener('click', clickCell);
cellSix.addEventListener('click', clickCell);
cellSeven.addEventListener('click', clickCell);
cellEight.addEventListener('click', clickCell);
// document.getElementById('winningMessage').hidden = True;
})
winningMessageElement.classList.remove('show');
}
function clickCell(e) {
const cell = e.target
let currentClass = isPlayerOTurn ? playerO : playerX
placeMark(cell, currentClass)
if (checkWin(currentClass)) {
endGame(false)
} else if (isDraw()) {
endGame(true)
} else {
swapTurns()
}
}
function placeMark(cell, currentClass) {
cell.classList.add(currentClass);
addImage(cell);
swapTurns();
}
function setBoardHoverClass() {
boardElement.classList.remove(playerO)
boardElement.classList.remove(playerO)
if (isPlayerOTurn) {
boardElement.classList.add(playerO)
} else {
boardElement.classList.add(playerX)
}
}
function checkWin(currentClass) {
return winningCombo.some(combination => {
return combination.every(index => {
return (cellElements[index].classList.contains(currentClass))
})
})
}
function endGame(draw) {
if (draw) {
winningMessageTextElement.innerText.add("It's a draw!")
} else {
winningMessageTextElement.innerText.add(`The ${isPlayerOTurn ? "O's" : "X's"} win!`)
}
winningMessageElement.classList.add('show')
}
function swapTurns() {
isPlayerOTurn = !isPlayerOTurn
}
function addImage(cell) {
if (cell.classList.contains('X')) {
cell.firstChild.style.display = "block";
} else if (cell.classList.contains('O')) {
cell.lastChild.style.display = "block";
}
}
function setBoardHoverClass(isPlayerOTurn) {
boardElement.classList.remove(playerX)
boardElement.classList.remove(playerO)
if (isPlayerOTurn) {
boardElement.classList.add(playerO)
} else {
boardElement.classList.add(playerX)
}
}
function isDraw() {
let playedCells = cellElements.includes(('O') || ('X'))
if (playedCells.length > 8){
return true;
} else {
return false
}
}
function restart() {
cell.forEach.classList.remove('X')
cell.forEach.forclassList.remove('O')
}
restartButton.addEventListener('click', restart)