This repository was 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
141 lines (127 loc) · 3.45 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TIC TAC TOE</title>
</head>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Pacifico&display=swap" rel="stylesheet">
<style>
body{
background-color: rgb(25, 36, 102);
}
.head{
font-size: larger;
font-family: 'Pacifico', cursive;
text-align: center;
background-color: #DDA0DD;
color: black;
border: solid rgb(0, 0, 0) 5px;
border-radius: 15px;
}
.box{
display: flex;
justify-content: center;
align-items: center;
}
.board{
margin-top: 40px ;
display: grid;
grid-template-columns: repeat(3,1fr);
grid-gap: 10px;
width: 330px;
height: 330px;
}
.cell{
font-family: 'Pacifico', cursive;
font-size: 50px;
text-align: center;
width: 100px;
height: 100px;
border-radius: 10px;
background-color: #DDA0DD;
background-color: rgb(127, 201, 67);
box-shadow: 2px 3px 5px red;
}
.cell:hover {
background-color: #DDA0DD;
}
</style>
<body>
<fieldset class="head">
<h2>TIC TAC TOE</h2>
</fieldset>
<div class="box">
<div class="board">
<div class="cell" id="cell-1"></div>
<div class="cell" id="cell-2"></div>
<div class="cell"id="cell-3"></div>
<div class="cell"id="cell-4"></div>
<div class="cell"id="cell-5"></div>
<div class="cell"id="cell-6"></div>
<div class="cell"id="cell-7"></div>
<div class="cell"id="cell-8"></div>
<div class="cell"id="cell-9"></div>
</div>
</div>
</body>
<script>
var currentPlayer = 'X';
var moves = 0;
var cells = Array.from(document.getElementsByClassName('cell'));
var board = ['', '', '', '', '', '', '', '', ''];
cells.forEach(function(cell, index) {
cell.addEventListener('click', function() {
makemove(index);
});
});
function makemove(cellIndex) {
if (board[cellIndex] === '') {
board[cellIndex] = currentPlayer;
cells[cellIndex].innerText = currentPlayer;
moves++;
if (checkWin(currentPlayer))
{
alert(currentPlayer + ' wins!');
resetGame();
return;
}
else if (moves === 9)
{
alert("It's a draw!");
resetGame();
return;
}
currentPlayer = currentPlayer === 'X' ? 'O' : 'X';
}
}
function checkWin(player) {
var winCombos= [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];
return winCombos.some(combination =>
{
return combination.every(index => board[index] === player);
});
}
function resetGame()
{
board = ['', '', '', '', '', '', '', '', ''];
moves = 0;
currentPlayer = 'X';
cells.forEach(cell => {
cell.innerText = '';
});
}
</script>
</html>