-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
73 lines (68 loc) · 3.06 KB
/
script.js
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
$(document).ready(function () {
var x = `<i class="fas fa-times"></i>`;
var o = `<i class="far fa-circle"></i>`;
var turn = 1,
cell_0 = $('.cell-0'),
cell_1 = $('.cell-1'),
cell_2 = $('.cell-2'),
cell_3 = $('.cell-3'),
cell_4 = $('.cell-4'),
cell_5 = $('.cell-5'),
cell_6 = $('.cell-6'),
cell_7 = $('.cell-7'),
cell_8 = $('.cell-8'),
btn = $('button');
var score = 0;
$('.cell').on("click", function (e) {
if ($(this).hasClass('o') || $(this).hasClass('x')) {
alert('already filled')
}
else if (turn === 9) {
alert('Its a TIE')
$('.cell').off('click')
}
else if (turn % 2 == 0) {
turn++;
$(this).text('o')
$(this).addClass('o')
if (cell_0.hasClass('o') && cell_1.hasClass('o') && cell_2.hasClass('o') ||
cell_3.hasClass('o') && cell_4.hasClass('o') && cell_5.hasClass('o') ||
cell_6.hasClass('o') && cell_7.hasClass('o') && cell_8.hasClass('o') ||
cell_0.hasClass('o') && cell_3.hasClass('o') && cell_6.hasClass('o') ||
cell_1.hasClass('o') && cell_4.hasClass('o') && cell_7.hasClass('o') ||
cell_2.hasClass('o') && cell_5.hasClass('o') && cell_8.hasClass('o') ||
cell_0.hasClass('o') && cell_4.hasClass('o') && cell_8.hasClass('o') ||
cell_2.hasClass('o') && cell_4.hasClass('o') && cell_6.hasClass('o')) {
alert('The Winner is : "O" ')
$('.cell').off('click')
score = score + 1;
winnerO.html(` <span>)O's</span> Win +=${score}`)
}
} else if (turn % 2 !== 0) {
turn++;
$(this).text('x')
$(this).addClass('x')
if (cell_0.hasClass('x') && cell_1.hasClass('x') && cell_2.hasClass('x') ||
cell_3.hasClass('x') && cell_4.hasClass('x') && cell_5.hasClass('x') ||
cell_6.hasClass('x') && cell_7.hasClass('x') && cell_8.hasClass('x') ||
cell_0.hasClass('x') && cell_3.hasClass('x') && cell_6.hasClass('x') ||
cell_1.hasClass('x') && cell_4.hasClass('x') && cell_7.hasClass('x') ||
cell_2.hasClass('x') && cell_5.hasClass('x') && cell_8.hasClass('x') ||
cell_0.hasClass('x') && cell_4.hasClass('x') && cell_8.hasClass('x') ||
cell_2.hasClass('x') && cell_4.hasClass('x') && cell_6.hasClass('x')) {
alert('The Winner is : "X" ')
$('.cell').off('click')
score = score + 1;
winnerX.html(` <span>X's</span> Win=${score}`)
}
}
});
btn.click(function (e) {
$('.cell').removeClass('o');
$('.cell').removeClass('x');
turn = 1;
$('.cell').text('');
});
const winnerX = $('.score-board>h2').first();
const winnerO = $('.score-board>h2').last();
});