-
Notifications
You must be signed in to change notification settings - Fork 1
/
ticTacToe.html
181 lines (150 loc) · 2.98 KB
/
ticTacToe.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
181
<!DOCTYPE html>
<html>
<head>
<style>
* {
box-sizing: border-box;
}
.ticTacToe {
width: 200px;
text-align: center;
}
#board{
display: inline-block;
}
button {
width: 50px;
height: 50px;
background-color: #14bdac;
border-top: none;
border-left: none;
outline: none;
font-size: 20px;
cursor: pointer;
}
button:last-child {
border-right: none;
}
div:last-child button {
border-bottom: none;
}
#restart {
width: 70%;
border-bottom: none;
margin-top: 20px;
border-right: none;
}
</style>
</head>
<body>
<div class="ticTacToe">
<h1>Tic-Tac-Toe</h1>
<div id="board">
<div>
<button> </button><button> </button><button> </button>
</div>
<div>
<button> </button><button> </button><button> </button>
</div>
<div>
<button> </button><button> </button><button> </button>
</div>
</div>
<button id="restart">Restart Game</button>
</div>
<script>
var game = {};
// Init the game
function init(){
game = {
isCircle: true,
diagnol: 0,
antidiagnol: 0,
count: 0,
row: [0, 0, 0],
col: [0, 0, 0]
};
}
// Check if any player wins
function checkWin(i, j){
let score;
if(game.isCircle){
score = 1;
}
else{
score = -1;
}
game.row[i] += score;
game.col[j] += score;
if(i === j){
game.diagnol += score;
}
if(i + j === 2){
game.antidiagnol += score;
}
if(Math.abs(game.row[i]) === 3 || Math.abs(game.col[j]) === 3 || Math.abs(game.diagnol) === 3 || Math.abs(game.antidiagnol) === 3 ){
return score; // If the score is 1 , circle wins
}
}
// Clear the board and reset the game
function clearGame() {
// Reset the game
init();
var buttons = document.querySelectorAll('#board button');
for(let i = 0; i < buttons.length; i++){
buttons[i].innerHTML = ' ';
buttons[i].disabled = false;
}
}
function showMsg(msg) {
setTimeout(function(){
alert(msg + " Restarting the game ...");
}, 1);
}
function ticTacToe(){
document.getElementById('restart').addEventListener('click', clearGame);
// Set the click handler for each cell
var buttons = document.querySelectorAll('#board button');
for(let i = 0; i < buttons.length; i++){
buttons[i].addEventListener('click', function(e){
let target = e.target;
if(target.nodeName === 'BUTTON'){
// Set the value of the cell
if(game.isCircle){
target.innerHTML = 'O';
}
else{
target.innerHTML = 'X';
}
target.disabled = true;
game.count++;
// Check if the player wins or not
let res = checkWin(Math.floor(i / 3), i % 3);
if(res === 1){
showMsg("Circle Wins!");
setTimeout(clearGame, 1000);
return;
}
else if(res === -1){
showMsg("Cross Wins!");
setTimeout(clearGame, 1000);
return;
}
// Check if no player wins
if(game.count === 9){
showMsg("Draw!");
setTimeout(clearGame, 1000);
}
// Change to next player's turn
game.isCircle = !game.isCircle;
}
})
}
}
window.onload = function(){
init();
ticTacToe();
}
</script>
</body>
</html>