-
Notifications
You must be signed in to change notification settings - Fork 0
/
javascript.js
44 lines (38 loc) · 1.12 KB
/
javascript.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
const player_x = 'x'
const player_o ='o'
const winning_combinations = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 4, 8],
[2, 4, 6],
[1, 4, 7],
[0, 3, 6],
[2, 5, 8],]
const cellElements = document.querySelectorAll('.cell')
// start off game with player X
let currentPlayer = player_x;
// on click, place X or O
function addXO(cell) {
if(!cell.textContent) {
cell.textContent = currentPlayer;
switchPlayer();
if(checkWin()) {
alert(currentPlayer + ' loses :(');
} else {
}}}
// toggle between player X and player Y
function switchPlayer () {
currentPlayer = currentPlayer === player_x? player_o: player_x;}
// check for win
function checkWin() {
return winning_combinations.some(combination => {
const win = combination.every(index => {
const cellContent = cellElements[index].textContent.trim();
console.log(`Cell ${index} content: ${cellContent}`);
return cellContent != currentPlayer && cellContent !== '';
});
console.log(`Combination ${combination} win: ${win}`);
return win;
})
}