-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
64 lines (52 loc) · 1.84 KB
/
app.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
const gameBoard = document.querySelector('#gameboard');
const infoDisplay = document.querySelector('#info');
const startCells = [
"", "", "", "", "", "", "", "", ""
]
let go = "circle"
infoDisplay.textContent = "Le cercle commence"
function createBoard() {
startCells.forEach((_cell, index) => {
const cellElement = document.createElement('div');
cellElement.classList.add('square');
cellElement.id = index;
cellElement.addEventListener('click', addGo)
gameBoard.append(cellElement);
})
}
createBoard()
function addGo(e) {
const goDisplay = document.createElement('div');
goDisplay.classList.add(go);
e.target.append(goDisplay);
go = go === "circle" ? "cross" : "circle";
infoDisplay.textContent = "Au tour de " + go;
e.target.removeEventListener('click', addGo);
checkScore()
}
function checkScore() {
const allSquares = document.querySelectorAll('.square');
const winningCombos = [
[0,1,2], [3,4,5], [6,7,8],
[0,3,6], [1,4,7], [2,5,8],
[0,4,8], [2,4,6]
]
winningCombos.forEach(array => {
const circleWins = array.every(cell =>
allSquares[cell].firstChild?.classList.contains('circle'));
if (circleWins) {
infoDisplay.textContent = "Les cercles gagnent !";
allSquares.forEach(square => square.replaceWith(square.cloneNode(true)));
return
}
})
winningCombos.forEach(array => {
const crossWins = array.every(cell =>
allSquares[cell].firstChild?.classList.contains('cross'));
if (crossWins) {
infoDisplay.textContent = "Les croix gagnent !";
allSquares.forEach(square => square.replaceWith(square.cloneNode(true)));
return
}
})
}