-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe.html
133 lines (120 loc) · 3.69 KB
/
tictactoe.html
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
<!DOCTYPE html>
<html>
<head>
<title>Tic Tac Toe</title>
<style>
body {
text-align: center;
font-family: Arial, sans-serif;
background-color: #000000;
}
h1 {
margin-top: 20px;
}
.board {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-gap: 10px;
width: 320px;
margin: 0 auto;
margin-top: 50px;
}
.cell {
background-color: #eee;
border: 2px solid #ccc;
display: flex;
align-items: center;
justify-content: center;
font-size: 37px;
cursor: pointer;
transition: background-color 0.3s ease;
padding: 50px;
border-radius: 20px;
}
.cell:hover {
background-color: #ddd;
}
.cell.X {
color: #ff5e5e;
}
.cell.O {
color: #56baf1;
}
</style>
</head>
<body>
<div style="color: #56baf1;">
<h1>Tic Tac Toe</h1>
</div>
<div class="board">
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
</div>
<script>
const cells = document.querySelectorAll('.cell');
let currentPlayer = 'X';
cells.forEach(cell => {
cell.addEventListener('click', handleCellClick);
});
function handleCellClick(e) {
const cell = e.target;
if (cell.textContent !== '') return; // Check if the cell is already occupied
cell.textContent = currentPlayer;
cell.classList.add(currentPlayer);
if (checkWin(currentPlayer)) {
setTimeout(() => {
alert(`Player ${currentPlayer} wins!`);
resetGame();
}, 100);
} else if (checkDraw()) {
setTimeout(() => {
alert("It's a draw!");
resetGame();
}, 100);
} else {
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
if (currentPlayer === 'O') {
takeComputerTurn();
}
}
}
function takeComputerTurn() {
const availableCells = Array.from(cells).filter(cell => cell.textContent === '');
const randomIndex = Math.floor(Math.random() * availableCells.length);
const cell = availableCells[randomIndex];
if (cell) {
setTimeout(() => {
cell.click();
}, 500);
}
}
function checkWin(player) {
const winningCombinations = [
[0, 1, 2], [3, 4, 5], [6, 7, 8], // Rows
[0, 3, 6], [1, 4, 7], [2, 5, 8], // Columns
[0, 4, 8], [2, 4, 6] // Diagonals
];
return winningCombinations.some(combination => {
return combination.every(index => cells[index].classList.contains(player));
});
}
function checkDraw() {
return Array.from(cells).every(cell => cell.textContent !== '');
}
function resetGame() {
cells.forEach(cell => {
cell.textContent = '';
cell.classList.remove('X', 'O');
});
currentPlayer = 'X';
}
</script>
</body>
</html>