-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathTic-Tac-Toe Game
140 lines (140 loc) · 3.53 KB
/
Tic-Tac-Toe Game
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
134
135
136
137
138
139
140
HTML:
<!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 Game</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="game-container">
<h1>Tic-Tac-Toe</h1>
<div id="gameBoard">
<div class="cell" data-cell></div>
<div class="cell" data-cell></div>
<div class="cell" data-cell></div>
<div class="cell" data-cell></div>
<div class="cell" data-cell></div>
<div class="cell" data-cell></div>
<div class="cell" data-cell></div>
<div class="cell" data-cell></div>
<div class="cell" data-cell></div>
</div>
<p id="gameStatus">Player X's turn</p>
<button id="restartButton">Restart Game</button>
</div>
<script src="script.js"></script>
</body>
</html>
CSS:
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
.game-container {
text-align: center;
background-color: #f0f0f0;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
#gameBoard {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-template-rows: repeat(3, 100px);
gap: 5px;
}
.cell {
width: 100px;
height: 100px;
background-color: white;
display: flex;
justify-content: center;
align-items: center;
font-size: 36px;
cursor: pointer;
border: 1px solid #ccc;
}
button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
margin-top: 20px;
}
p {
font-size: 18px;
margin-top: 20px;
}
JavaScript:
const cells = document.querySelectorAll('[data-cell]');
const gameStatus = document.getElementById('gameStatus');
const restartButton = document.getElementById('restartButton');
let isXTurn = true;
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]
];
startGame();
restartButton.addEventListener('click', startGame);
function startGame() {
isXTurn = true;
gameStatus.textContent = "Player X's turn";
cells.forEach(cell => {
cell.classList.remove('x');
cell.classList.remove('o');
cell.removeEventListener('click', handleClick);
cell.addEventListener('click', handleClick, { once: true });
});
}
function handleClick(e) {
const cell = e.target;
const currentClass = isXTurn ? 'x' : 'o';
placeMark(cell, currentClass);
if (checkWin(currentClass)) {
endGame(false);
} else if (isDraw()) {
endGame(true);
} else {
swapTurns();
updateStatus();
}
}
function placeMark(cell, currentClass) {
cell.classList.add(currentClass);
}
function swapTurns() {
isXTurn = !isXTurn;
}
function updateStatus() {
gameStatus.textContent = isXTurn ? "Player X's turn" : "Player O's turn";
}
function checkWin(currentClass) {
return winningCombinations.some(combination => {
return combination.every(index => {
return cells[index].classList.contains(currentClass);
});
});
}
function isDraw() {
return [...cells].every(cell => {
return cell.classList.contains('x') || cell.classList.contains('o');
});
}
function endGame(draw) {
if (draw) {
gameStatus.textContent = "It's a draw!";
} else {
gameStatus.textContent = isXTurn ? "Player X wins!" : "Player O wins!";
}
}