-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
127 lines (109 loc) · 3.46 KB
/
Copy pathtest.js
File metadata and controls
127 lines (109 loc) · 3.46 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
// 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 => {
cell.classList.remove('X');
cell.classList.remove('O');
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);
})
}
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);
}
function checkWin(currentClass) {
return winningCombo.some(combination => {
return combination.every(index => {
return cellElements[index].classList.contains(currentClass);
})
})
}
function endGame(draw) {
if (draw) {
winningMessageTextElement.innerText = "It's a draw!";
} else {
winningMessageTextElement.innerText = `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 isDraw() {
let playedCells = cellElements.filter(cell => cell.classList.contains('X') || cell.classList.contains('O'));
return playedCells.length === 9;
}
function restart() {
cellElements.forEach(cell => {
cell.classList.remove(playerX);
cell.classList.remove(playerO);
cell.removeEventListener('click', clickCell);
cell.addEventListener('click', clickCell);
cell.firstChild.style.display = "none";
cell.lastChild.style.display = "none";
});
winningMessageElement.classList.remove('show');
start();
}
restartButton.addEventListener('click', restart)