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
118 lines (110 loc) · 2.8 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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Task 11</title>
<style>
.box {
display: flex;
justify-content: center;
align-items: center;
}
h2 {
text-align: center;
box-shadow: 0px 0px 3px black;
}
#gameboard .cell:hover {
background-color: aquamarine;
box-shadow: 0px 0px 3px black;
}
#gameboard {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-gap: 10px;
width: 330px;
height: 330px;
}
.cell {
width: 100px;
height: 100px;
border: 2px solid black;
display: flex;
justify-content: center;
align-items: center;
font-size: 45px;
cursor: pointer;
}
</style>
</head>
<body>
<h2>Tic Tac Toe</h2>
<div class="box">
<div id="gameboard">
<div class="cell" id="cell-0"></div>
<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>
</div>
<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>
</body>
</html>