-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patht3.html
More file actions
222 lines (202 loc) · 7.17 KB
/
t3.html
File metadata and controls
222 lines (202 loc) · 7.17 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: #f2f2f2;
animation: colorChange 10s linear infinite alternate;
margin: 0;
padding: 0;
}
@keyframes colorChange {
0% {
background-color: #3498db;
}
25% {
background-color: #e74c3c;
}
50% {
background-color: #f1c40f;
}
75% {
background-color: #27ae60;
}
100% {
background-color: #9b59b6;
}
}
.game-container {
background-color: #fff;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
width: 300px;
margin: 100px auto;
padding: 20px;
}
h1 {
font-size: 28px;
color: #333;
margin-bottom: 10px;
}
.game-board {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-gap: 10px;
}
.cell {
width: 80px;
height: 80px;
background-color: #3498db;
display: flex;
align-items: center;
justify-content: center;
font-size: 24px;
color: #fff;
border: none;
cursor: pointer;
transition: background-color 0.2s;
}
.cell:hover {
background-color: #2980b9;
}
.cell.x::before {
content: 'X';
color: red;
}
.cell.o::before {
content: 'O';
color: green;
}
.player-inputs {
margin: 10px 0;
}
.player-inputs input {
width: 100%;
margin: 5px 0;
padding: 5px;
}
button {
background-color: #e74c3c;
color: #fff;
border: none;
padding: 10px 20px;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
margin-top: 10px;
}
button:hover {
background-color: #c0392b;
}
#message {
margin-top: 10px;
}
</style>
</head>
<body>
<div class="game-container">
<h1>Sanskar's Tic Tac Toe</h1>
<div class="player-inputs">
<input type="text" id="player1Name" placeholder="Player 1 Name">
<input type="text" id="player2Name" placeholder="Player 2 Name">
</div>
<div class="game-board">
<div class="cell" onclick="makeMove(this)"></div>
<div class="cell" onclick="makeMove(this)"></div>
<div class="cell" onclick="makeMove(this)"></div>
<div class="cell" onclick="makeMove(this)"></div>
<div class="cell" onclick="makeMove(this)"></div>
<div class="cell" onclick="makeMove(this)"></div>
<div class="cell" onclick="makeMove(this)"></div>
<div class="cell" onclick="makeMove(this)"></div>
<div class="cell" onclick="makeMove(this)"></div>
</div>
<div id="message">Enter players' names and start the game.</div>
<button onclick="startGame()">Start Game</button>
<button onclick="resetBoard()">Reset Game</button>
</div>
<script>
let currentPlayer = 'X';
let isGameOver = false;
let movesCount = 0;
const cells = document.querySelectorAll('.cell');
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 startGame() {
const player1Name = document.getElementById('player1Name').value;
const player2Name = document.getElementById('player2Name').value;
if (player1Name && player2Name) {
document.getElementById('message').textContent = `${player1Name}'s Turn`;
document.getElementById('player1Name').disabled = true;
document.getElementById('player2Name').disabled = true;
document.querySelector('.player-inputs button').disabled = true;
} else {
document.getElementById('message').textContent = 'Please enter both players\' names.';
}
}
function makeMove(cell) {
if (!cell.textContent && !isGameOver) {
cell.textContent = '';
cell.classList.add(currentPlayer.toLowerCase());
cell.style.backgroundColor = '#fff';
movesCount++;
if (checkWin()) {
isGameOver = true;
document.getElementById('message').textContent = `${currentPlayer} wins!`;
} else if (movesCount === 9) {
isGameOver = true;
document.getElementById('message').textContent = "It's a tie!";
} else {
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
const player1Name = document.getElementById('player1Name').value;
const player2Name = document.getElementById('player2Name').value;
document.getElementById('message').textContent =
currentPlayer === 'X' ? `${player1Name}'s Turn` : `${player2Name}'s Turn`;
}
}
}
function checkWin() {
for (const combo of winningCombinations) {
const [a, b, c] = combo;
if (
cells[a].classList.contains(currentPlayer.toLowerCase()) &&
cells[b].classList.contains(currentPlayer.toLowerCase()) &&
cells[c].classList.contains(currentPlayer.toLowerCase())
) {
highlightWinningCombo(a, b, c);
return true;
}
}
return false;
}
function highlightWinningCombo(a, b, c) {
cells[a].style.backgroundColor = '#27ae60';
cells[b].style.backgroundColor = '#27ae60';
cells[c].style.backgroundColor = '#27ae60';
}
function resetBoard() {
for (const cell of cells) {
cell.textContent = '';
cell.style.backgroundColor = '#3498db';
cell.classList.remove('x', 'o');
}
currentPlayer = 'X';
isGameOver = false;
movesCount = 0;
const player1Name = document.getElementById('player1Name').value;
const player2Name = document.getElementById('player2Name').value;
document.getElementById('message').textContent = `${player1Name}'s Turn`;
}
</script>
</body>
</html>