Skip to content

Commit 111705b

Browse files
tic tac toe
0 parents  commit 111705b

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

index.html

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
<!DOCTYPE html>
3+
<html lang="en">
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Tic Tac Toe</title>
8+
<link rel="stylesheet" href="style.css">
9+
</head>
10+
<body>
11+
<h1>Tic Tac Toe</h1>
12+
<div class="game-board">
13+
<div class="square" id="top-left">&nbsp;</div>
14+
<div class="square" id="top-middle">&nbsp;</div>
15+
<div class="square" id="top-right">&nbsp;</div>
16+
<div class="square" id="middle-left">&nbsp;</div>
17+
<div class="square" id="middle-middle">&nbsp;</div>
18+
<div class="square" id="middle-right">&nbsp;</div>
19+
<div class="square" id="bottom-left">&nbsp;</div>
20+
<div class="square" id="bottom-middle">&nbsp;</div>
21+
<div class="square" id="bottom-right">&nbsp;</div>
22+
</div>
23+
<div class="game-info">
24+
<p id="turn">Turn: X</p>
25+
<p id="result">&nbsp;</p>
26+
<button id="reset">Reset</button>
27+
</div>
28+
<script src="java.js"></script>
29+
</body>
30+
</html>

java.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
let gameBoard = document.querySelector('.game-board');
2+
let squares = document.querySelectorAll('.square');
3+
let currentPlayer = 'X';
4+
let gameOver = false;
5+
6+
gameBoard.addEventListener('click', (e) => {
7+
if (e.target.classList.contains('square') && !gameOver) {
8+
let square = e.target;
9+
if (square.textContent === '') {
10+
square.textContent = currentPlayer;
11+
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
12+
checkWinner();
13+
}
14+
}
15+
});
16+
17+
document.getElementById('reset').addEventListener('click', () => {
18+
squares.forEach((square) => {
19+
square.textContent = '';
20+
});
21+
currentPlayer = 'X';
22+
gameOver = false;
23+
document.getElementById('turn').textContent = 'Turn: X';
24+
document.getElementById('result').textContent = '';
25+
});
26+
27+
function checkWinner() {
28+
const winningCombinations = [
29+
['top-left', 'top-middle', 'top-right'],
30+
['middle-left', 'middle-middle', 'middle-right'],
31+
['bottom-left', 'bottom-middle', 'bottom-right'],
32+
['top-left', 'middle-left', 'bottom-left'],
33+
['top-middle', 'middle-middle', 'bottom-middle'],
34+
['top-right', 'middle-right', 'bottom-right'],
35+
['top-left', 'middle-middle', 'bottom-right'],
36+
['top-right', 'middle-middle', 'bottom-left']
37+
];
38+
39+
for (let combination of winningCombinations) {
40+
if (
41+
document.getElementById(combination[0]).textContent === currentPlayer &&
42+
document.getElementById(combination[1]).textContent === currentPlayer &&
43+
document.getElementById(combination[2]).textContent === currentPlayer
44+
) {
45+
gameOver = true;
46+
document.getElementById('result').textContent = `Player ${currentPlayer} wins!`;
47+
break;
48+
}
49+
}
50+
51+
if (!gameOver && Array.from(squares).every((square) => square.textContent !== '')) {
52+
gameOver = true;
53+
document.getElementById('result').textContent = 'It\'s a draw!';
54+
}
55+
56+
document.getElementById('turn').textContent = `Turn: ${currentPlayer}`;
57+
}

style.css

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
.game-board {
2+
display: grid;
3+
grid-template-columns: repeat(3, 1fr);
4+
grid-gap: 10px;
5+
}
6+
.square {
7+
background-color: #ccc;
8+
padding: 20px;
9+
text-align: center;
10+
cursor: pointer;
11+
border: 1px solid black ;
12+
}
13+
.square:hover {
14+
background-color: #aaa;
15+
}
16+
.game-info {
17+
margin-top: 20px;
18+
}
19+
#result {
20+
font-weight: bold;
21+
color: #666;
22+
}
23+
#reset{
24+
height: 40px;
25+
width: 200px;
26+
font-size: 30px;
27+
font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
28+
font-weight: 500;
29+
background-color: #666;
30+
}
31+
#turn{
32+
font-size: 20px;
33+
font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
34+
font-style: italic;
35+
}
36+
h1{
37+
font-size: 40px;
38+
font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
39+
font-weight: 400;
40+
}
41+

0 commit comments

Comments
 (0)