-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
180 lines (168 loc) · 4.24 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Tic Tac Toe Game</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
background-color: aqua;
}
.game-board {
display: grid;
grid-template-columns: repeat(3, 100px);
grid-template-rows: repeat(3, 100px);
gap: 5px;
margin: auto;
}
.cell {
width: 100px;
height: 100px;
background-color:aqua;
display: flex;
align-items: center;
justify-content: center;
font-size: 40px;
cursor: pointer;
text-shadow: 0 0 3px #FF0000, 0 0 5px #0000FF;
color: white;
}
.cell.taken {
pointer-events: none;
}
.game-board{
justify-content: center;
width: 100wh;
}
.cell{
border: 2px solid;
}
#restartButton{
margin: 20px 0 0 0;
top:50%;
background-color: #333;
color:whitesmoke;
border: none;
padding: 10px 20px;
border-radius: 4px;
font-size: 16px;
cursor: pointer;
}
.Undo{
background-color: #333;
color: #fff;
border: none;
padding: 10px 20px;
border-radius: 4px;
font-size: 16px;
cursor: pointer;
margin: 5px;
}
h1{
text-shadow: 0 0 3px #FF0000, 0 0 5px #0000FF;
}
.completeBorder{
border: 3px black solid;
border-radius: 10px;
}
</style>
</head>
<body>
<h1>Tic Tac Toe</h1>
<div id="gameBoard" class="game-board">
<!-- 9 cells for Tic Tac Toe grid -->
<div class="cell" data-cell-index="0"></div>
<div class="cell" data-cell-index="1"></div>
<div class="cell" data-cell-index="2"></div>
<div class="cell" data-cell-index="3"></div>
<div class="cell" data-cell-index="4"></div>
<div class="cell" data-cell-index="5"></div>
<div class="cell" data-cell-index="6"></div>
<div class="cell" data-cell-index="7"></div>
<div class="cell" data-cell-index="8"></div>
</div>
<button id="restartButton">Restart Game</button>
<div id="results" class="results"></div>
<script >
// add code here
function createBoard(){
const grid = Array.from(document.querySelector(".game-board").children)
grid.forEach((it) =>{
it.style.border = "2px solid black";
})
const btn = document.querySelector("#restartButton")
btn.style.marginTop = "20px";
btn.style.padding = "5px";
const board = document.querySelector('.game-board');
board.style.justifyContent = "center";
board.style.maxWidth = "100wh";
}
function changeval(val){
return val == 'X' ? 'O' : 'X';
}
function checkWin(){
const grid = Array.from(document.querySelectorAll('.cell'));
const arr = [[0,1,2], [3,4,5], [6,7,8], [0,3,6], [1,4,7], [2,5,8], [0,4,8], [2,4,6]];
for (let it of arr) {
if (grid[it[0]].innerText == grid[it[1]].innerText && grid[it[2]].innerText == grid[it[1]].innerText && grid[it[0]].innerText != "") {
return true;
}
}
return false;
}
function draw(){
const grid = Array.from(document.querySelectorAll('.cell'));
for(let i = 0; i < grid.length; i++){
if(grid[i].innerText == "") return false;
}
return true;
}
let val = 'X';
let arr = [];
function control(e){
if(e.target.innerText == ""){
e.target.innerText = val;
console.log(checkWin());
if(checkWin()){
alert(`${val} wins!`);
grid.removeEventListener('click', control);
}
if(draw()){
alert("It's a draw!");
grid.removeEventListener('click', control);
}
val = changeval(val);
arr.push(e.target);
}
}
const grid = document.querySelector(".game-board");
grid.addEventListener("click", control);
createBoard();
document.querySelector("#restartButton").addEventListener('click', () =>{
const grid2 = Array.from(document.querySelectorAll('.cell'));
grid2.forEach((it) =>{
it.innerText = "";
});
grid.addEventListener("click", control);
val = 'X';
arr = [];
});
const div = document.createElement('button');
div.id = "undoButton"
div.innerText = "Undo";
div.style.marginTop = "20px";
div.className="Undo"
// div.style.marginBottom = "20px";
div.style.padding = "5px";
document.querySelector("body").insertBefore(div, document.querySelector("#restartButton"));
div.addEventListener('click', () => {
if (arr.length != 0) {
let cell = arr.pop();
cell.innerText = "";
val= changeval(val);
}
});
</script>
</body>
</html>