-
Notifications
You must be signed in to change notification settings - Fork 0
/
tictactoe.html
145 lines (134 loc) · 4.24 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
134
135
136
137
138
139
140
141
142
143
144
145
<!DOCTYPE html>
<html>
<head>
<title>Tic Tac Toe</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin: 0;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background-color: #f0f0f0;
}
#container {
display: flex;
flex-direction: column;
align-items: center;
}
#board {
display: grid;
grid-template-columns: repeat(3, 60px);
margin-bottom: 20px;
}
.cell {
width: 60px;
height: 60px;
font-size: 24px;
border: 1px solid #000;
line-height: 60px;
text-align: center;
cursor: pointer;
}
#status {
margin-bottom: 20px;
font-size: 18px;
}
#resetButton {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
</style>
</head>
<body>
<div id="container">
<h1>Tic Tac Toe</h1>
<div id="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>
<div id="status">Player X's turn</div>
<button id="resetButton">Reset Game</button>
</div>
<script>
const cells = document.querySelectorAll('.cell');
// console.log(cells);
const resetButton = document.getElementById('resetButton');
let currentPlayer = 'X';
let gameActive = true;
function clickHandle(event) {
const cell = event.target;
if (cell.textContent !== '' || !gameActive) {
return;
}
if (currentPlayer === 'X') {
cell.style.color = '#ff0000'; // Red color for 'X'
} else {
cell.style.color = '#0000ff'; // Blue color for 'O'
}
cell.textContent = currentPlayer;
if (gamewin()) {
gameActive = false;
document.getElementById('status').textContent = `Player ${currentPlayer} won the Game`;
} else if (checkdraw()) {
gameActive = false;
document.getElementById('status').textContent = 'The Game is a tie';
} else {
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
document.getElementById('status').textContent = `Player ${currentPlayer}'s turn`;
}
}
function checkdraw() {
for (let i = 0; i < cells.length; i++) {
if (cells[i].textContent === '') {
return false;
}
}
return true;
}
function gamewin() {
const winPatterns = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];
for (let i = 0; i < winPatterns.length; i++) {
const [a, b, c] = winPatterns[i];
if (cells[a].textContent === currentPlayer &&
cells[b].textContent === currentPlayer &&
cells[c].textContent === currentPlayer) {
return true;
}
console.log(cells[a], cells[b], cells[c]);
}
return false;
}
function resetGame() {
cells.forEach(cell => {
cell.textContent = '';
cell.style.color = '';
});
currentPlayer = 'X';
gameActive = true;
status.textContent = `Player ${currentPlayer}'s turn`;
}
cells.forEach(cell => cell.addEventListener('click', clickHandle));
resetButton.addEventListener('click', resetGame);
</script>
</body>
</html>