-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPROJECT_TIC TAC TOE.txt
128 lines (117 loc) · 3.23 KB
/
PROJECT_TIC TAC TOE.txt
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tic-Tac-Toe</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h1>Tic-Tac-Toe</h1>
<div class="board">
<div class="cell" data-index="0"></div>
<div class="cell" data-index="1"></div>
<div class="cell" data-index="2"></div>
<div class="cell" data-index="3"></div>
<div class="cell" data-index="4"></div>
<div class="cell" data-index="5"></div>
<div class="cell" data-index="6"></div>
<div class="cell" data-index="7"></div>
<div class="cell" data-index="8"></div>
</div>
<div class="status"></div>
<button id="resetBtn">Reset</button>
</div>
<script src="script.js"></script>
</body>
</html>
const cells = document.querySelectorAll('.cell');
const statusDiv = document.querySelector('.status');
const resetBtn = document.getElementById('resetBtn');
let currentPlayer = 'X';
let board = Array(9).fill(null);
let isGameOver = false;
const winningCombinations = [
[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 handleClick(event) {
const index = event.target.dataset.index;
if (!board[index] && !isGameOver) {
board[index] = currentPlayer;
event.target.textContent = currentPlayer;
if (checkWinner()) {
statusDiv.textContent = Player ${currentPlayer} wins!;
isGameOver = true;
} else if (board.every(cell => cell)) {
statusDiv.textContent = 'It\'s a draw!';
isGameOver = true;
} else {
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
}
}
}
function checkWinner() {
return winningCombinations.some(combination => {
return combination.every(index => board[index] === currentPlayer);
});
}
function resetGame() {
board = Array(9).fill(null);
cells.forEach(cell => (cell.textContent = ''));
currentPlayer = 'X';
isGameOver = false;
statusDiv.textContent = '';
}
cells.forEach(cell => cell.addEventListener('click', handleClick));
resetBtn.addEventListener('click', resetGame);
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: purple;
font-family: Arial, sans-serif;
margin: 0;
}
.container {
text-align: center;
background-color:white;
}
.board {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-gap: 5px;
margin-bottom: 20px;
}
.cell {
width: 100px;
height: 100px;
background-color: fff;
display: flex;
justify-content: center;
align-items: center;
font-size: 2em;
cursor: pointer;
border: 2px solid #333;
transition: background-color 0.3s;
}
.cell:hover {
background-color: #ddd;
}
.status {
margin-bottom: 20px;
font-size: 1.5em;
}
#resetBtn {
padding: 10px 20px;
font-size: 1em;
cursor: pointer;
}