-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
110 lines (90 loc) · 2.51 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tic Toe Game</title>
<link rel="stylesheet" href="styles.css">
<style>
.board
{
display: grid;
grid-template-columns: repeat(3, 100px);
grid-template-rows: repeat(3,100px);
gap: 3px;
}
.cell {
width: 100px;
height: 100px;
background-color: #eee;
align-items: center;
justify-content: center;
display: flex;
font-size: 2em;
}
.h1
{
margin: 0px;
padding: 0px;
font-size: medium;
color: blueviolet;
}
</style>
</head>
<body>
<h1>Welcome to Tic Toe Game</h1>
<div class="board"></div>
<script >
const board = document.querySelector('.board');
let curretPlayer = 'X';
const cells = [];
// crete the game board
for(let i = 0; i<9; i++)
{
const cell = document.createElement('div');
cell.classlists.add('cell');
cell.dataset.index = i;
cell.addEventListener('click' , handlecellClick);
board.appendChild(cell);
cells.push(cell);
}
function handlecellClick()
{
if(this.textContent !== '' || checkwinner()) return;
this.textContent = curretPlayer;
if(checkwinner())
{
alert('Player' + curretPlayer + 'wins');
}
else if(isboardFull())
{
alert('It is draw!');
}
else{
currentPlayer == 'X' ? 'O' : 'X';
}
}
function checkwinner()
{
const winningCombos = [
[0 ,1 , 2] , [3 , 4 , 5] , [6 , 7 , 8 ] , // rows
[0 , 3 , 6] , [1 , 4 , 7] , [2 , 5 , 8], // columns
[0 , 4 , 8] , [2 , 4 , 6] // diagonals
];
for(const combo of winningCombos)
{
const [a , b , c] = combo;
if(cells[a].textContent === cells[b].textContent === cells[c].textContent)
{
return true;
}
}
return false;
}
function isboardFull()
{
return cells.every(cell => cell.textContent !== '');
}
</script>
</body>
</html>