forked from HJain13/Tic-Tac-Toe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttt.js
116 lines (109 loc) · 3.83 KB
/
ttt.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
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
var x = 'Player 2';
var placed = ['', '', '', '', '', '', '', '', '', ''];
var gOver = 0,
score1 = 0,
score2 = 0;
function place(i) {
if (gOver != 1) {
if ((x == 'Player 1') && (placed[i] == '')) {
document.getElementById("ci" + i).style.display = 'block';
document.getElementById("p2").style.opacity='0.2';
document.getElementById("p1").style.opacity='1';
placed[i] = 'ci';
x = 'Player 2';
} else if ((x == 'Player 2') && (placed[i] == '')) {
document.getElementById("cr" + i).style.display = 'block';
document.getElementById("p1").style.opacity='0.2';
document.getElementById("p2").style.opacity='1';
placed[i] = 'cr';
x = 'Player 1';
}
if (placed[1] == placed[2] && placed[2] == placed[3] && placed[1] != '') {
declare(x);
enhance(1, 2, 3, x);
} else if (placed[1] == placed[4] && placed[4] == placed[7] && placed[1] != '') {
declare(x);
enhance(1, 4, 7, x);
} else if (placed[1] == placed[5] && placed[5] == placed[9] && placed[1] != '') {
declare(x);
enhance(1, 5, 9, x);
} else if (placed[2] == placed[5] && placed[5] == placed[8] && placed[2] != '') {
declare(x);
enhance(2, 5, 8, x);
} else if (placed[3] == placed[5] && placed[5] == placed[7] && placed[3] != '') {
declare(x);
enhance(3, 5, 7, x);
} else if (placed[3] == placed[6] && placed[6] == placed[9] && placed[3] != '') {
declare(x);
enhance(3, 6, 9, x);
} else if (placed[4] == placed[6] && placed[6] == placed[5] && placed[4] != '') {
declare(x);
enhance(4, 5, 6, x);
} else if (placed[7] == placed[9] && placed[9] == placed[8] && placed[9] != '') {
declare(x);
enhance(7, 8, 9, x)
}
if (gOver!=1) checkTie();
}
}
function declare(x) {
document.getElementById("status").innerHTML = "<h1>" + x + " Wins!</h1><div class='playAgainbtn' id='rst' onClick='resetBoard()'><h2>Play Again</h2></div>";
if (x == 'Player 1') {
score1++;
document.getElementById("sp1").innerHTML = score1;
document.getElementById("mob-sp1").innerHTML = score1;
}
else {
score2++;
document.getElementById("sp2").innerHTML = score2;
document.getElementById("mob-sp2").innerHTML = score2;
}
document.getElementById("p1").style.opacity='0.2';
document.getElementById("p2").style.opacity='0.2';
gOver = 1;
}
function checkTie() {
var i, flag = 0;
for (i = 1; i <= 9; i++) {
if (placed[i] == '') {
flag = 1;
break;
}
}
if (flag == 0) {
document.getElementById("status").innerHTML = "<h1>It's A Draw</h1><div class='playAgainbtn' id='rst' onClick='resetBoard()'><h2>Play Again</h2></div>";
gOver = 1;
document.getElementById("p1").style.opacity = '0.2';
document.getElementById("p2").style.opacity = '0.2';
}
}
function enhance(a, b, c, x) {
if (x == 'Player 1') {
document.getElementById('cr' + a).setAttribute("class", "crossE");
document.getElementById('cr' + b).setAttribute("class", "crossE");
document.getElementById('cr' + c).setAttribute("class", "crossE");
window.getComputedStyle
} else {
document.getElementById('ci' + a).style.borderColor = "#0dd";
document.getElementById('ci' + b).style.borderColor = "#0dd";
document.getElementById('ci' + c).style.borderColor = "#0dd";
}
}
function resetBoard() {
placed[0] = '';
for (var i = 1; i <= 9; i++) {
document.getElementById("cr" + i).style.display = 'none';
document.getElementById('ci' + i).style.display = 'none';
document.getElementById('cr' + i).setAttribute("class", "cross");
document.getElementById('ci' + i).style.borderColor = "#333";
placed[i] = '';
}
gOver = 0;
document.getElementById("status").innerHTML = "<h1>Tic`Tac`Toe</h1>";
}
function resetScore() {
score1 = 0;
document.getElementById("sp1").innerHTML = score1;
score2 = 0;
document.getElementById("sp2").innerHTML = score2;
}