This repository has been archived by the owner on Jul 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathtask11.html
149 lines (133 loc) · 3.89 KB
/
task11.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
146
147
148
149
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tic-Tac-Toe</title>
<style>
h1{
font-family: Times New Roman;
margin-top: 70px;
text-align: center;
}
table {
margin-left: 80vh;
border-collapse: collapse;
border: 2px solid #333;
}
td {
width: 80px;
height: 80px;
border: 2px solid #333;
text-align: center;
vertical-align: middle;
font-size: 30px;
}
td:hover {
background-color: #cec8c8;
}
button {
display: block;
padding: 5px 15px 5px 15px;
margin: 20px auto;
}
button:hover{
background-color: skyblue;
}
</style>
</head>
<body>
<h1>Tic-Tac-Toe</h1>
<table>
<tr>
<td onclick="makeMove(0, 0)" id="cell-0-0"></td>
<td onclick="makeMove(0, 1)" id="cell-0-1"></td>
<td onclick="makeMove(0, 2)" id="cell-0-2"></td>
</tr>
<tr>
<td onclick="makeMove(1, 0)" id="cell-1-0"></td>
<td onclick="makeMove(1, 1)" id="cell-1-1"></td>
<td onclick="makeMove(1, 2)" id="cell-1-2"></td>
</tr>
<tr>
<td onclick="makeMove(2, 0)" id="cell-2-0"></td>
<td onclick="makeMove(2, 1)" id="cell-2-1"></td>
<td onclick="makeMove(2, 2)" id="cell-2-2"></td>
</tr>
</table>
<button onclick="resetGame()">Reset</button>
<script>
var name=prompt("enter your name:");
alert(`${name} you have assign X as a game sign`);
let currentPlayer = 'X';
let gameEnded = false;
function makeMove(row, col) {
const cell = document.getElementById(`cell-${row}-${col}`);
if (cell.innerHTML === '' && !gameEnded) {
cell.innerHTML = currentPlayer;
cell.style.cursor = 'default';
evaluateResults();
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
}
}
function evaluateResults() {
const winningCombinations = [
[[0, 0], [0, 1], [0, 2]],
[[1, 0], [1, 1], [1, 2]],
[[2, 0], [2, 1], [2, 2]],
[[0, 0], [1, 0], [2, 0]],
[[0, 1], [1, 1], [2, 1]],
[[0, 2], [1, 2], [2, 2]],
[[0, 0], [1, 1], [2, 2]],
[[0, 2], [1, 1], [2, 0]]
];
for (const combination of winningCombinations) {
const [a, b, c] = combination;
const cellA = document.getElementById(`cell-${a[0]}-${a[1]}`).innerHTML;
const cellB = document.getElementById(`cell-${b[0]}-${b[1]}`).innerHTML;
const cellC = document.getElementById(`cell-${c[0]}-${c[1]}`).innerHTML;
if (cellA !== '' && cellA === cellB && cellB === cellC) {
highlightWinningCells(a, b, c);
gameEnded = true;
setTimeout(() => {
alert(`Player ${cellA} wins!`);
}, 100);
return;
}
}
const cells = document.getElementsByTagName('td');
let isTie = true;
for (const cell of cells) {
if (cell.innerHTML === '') {
isTie = false;
break;
}
}
if (isTie) {
gameEnded = true;
setTimeout(() => {
alert("It's a tie!");
}, 100);
}
}
function highlightWinningCells(a, b, c) {
const cellA = document.getElementById(`cell-${a[0]}-${a[1]}`);
const cellB = document.getElementById(`cell-${b[0]}-${b[1]}`);
const cellC = document.getElementById(`cell-${c[0]}-${c[1]}`);
cellA.style.backgroundColor = 'green';
cellB.style.backgroundColor = 'lightgreen';
cellC.style.backgroundColor = 'seagreen';
}
function resetGame() {
const cells = document.getElementsByTagName('td');
for (const cell of cells) {
cell.innerHTML = '';
cell.style.backgroundColor = 'white';
cell.style.cursor = 'pointer';
}
currentPlayer = 'X';
gameEnded = false;
}
</script>
</body>
</html>