-
Notifications
You must be signed in to change notification settings - Fork 0
/
cttt.js
92 lines (76 loc) · 2.16 KB
/
cttt.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
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
const cells = document.querySelectorAll('.cell');
const statusText = document.getElementById('status');
const restartBtn = document.getElementById('restart');
let currentPlayer = 'X';
let board = ['', '', '', '', '', '', '', '', ''];
let justcheck = [];
let isGameActive = true;
const winningConditions = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];
function handleCellClick(event) {
const cell = event.target;
const index = cell.getAttribute('data-index');
if (board[index] !== '' || !isGameActive) return;
justcheck.push(index);
board[index] = currentPlayer;
cell.textContent = currentPlayer;
checkonly();
checkWinner();
}
function checkonly() {
if (justcheck.length > 6) {
let removeindex = justcheck.shift();
console.log(justcheck)
board[removeindex] = '';
console.log(board)
cells.forEach(cell => {
if (cell.dataset.index == removeindex) {
cell.textContent = '';
}
});
}
}
function checkWinner() {
let roundWon = false;
for (let i = 0; i < winningConditions.length; i++) {
const winCondition = winningConditions[i];
const a = board[winCondition[0]];
const b = board[winCondition[1]];
const c = board[winCondition[2]];
if (a === '' || b === '' || c === '') continue;
if (a === b && b === c) {
roundWon = true;
break;
}
}
if (roundWon) {
console.log("won");
statusText.textContent = `${currentPlayer} Wins!`;
isGameActive = false;
return;
}
if (!board.includes('')) {
statusText.textContent = 'Draw!';
isGameActive = false;
return;
}
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
}
function restartGame() {
board.fill('');
justcheck.length=0;
cells.forEach(cell => (cell.textContent = ''));
currentPlayer = 'X';
isGameActive = true;
statusText.textContent = '';
}
cells.forEach(cell => cell.addEventListener('click', handleCellClick));
restartBtn.addEventListener('click', restartGame);