-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTTT.js
More file actions
117 lines (99 loc) · 3.71 KB
/
TTT.js
File metadata and controls
117 lines (99 loc) · 3.71 KB
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 boxes = document.querySelectorAll(".box");
var reset = document.querySelector("button");
var span = document.querySelector("#message");
var ist=true;
var count=0;
reset.addEventListener("click", function(){
count=0;
for(var i=0; i<9; i++)
{
boxes[i].textContent="";
boxes[i].style.backgroundColor=null;
}
span.textContent="SELECT A BOX!";
ist=true;
});
for(var i=0; i<9; i++)
{
boxes[i].addEventListener("click", function(){
if(count%2==0 && this.textContent=="" && ist)
{
this.textContent="O";
count++;
check();
}
else if(count%2==1 && this.textContent=="" && ist)
{
this.textContent="X";
count++;
check();
}
});
};
function check()
{
if(boxes[0].textContent===boxes[1].textContent && boxes[0].textContent===boxes[2].textContent && boxes[0].textContent!="")
{
span.textContent="'"+boxes[0].textContent+"' WON";
boxes[0].style.backgroundColor="green";
boxes[1].style.backgroundColor="green";
boxes[2].style.backgroundColor="green";
ist=false;
}
else if(boxes[3].textContent===boxes[4].textContent && boxes[3].textContent===boxes[5].textContent && boxes[5].textContent!="")
{
span.textContent="'"+boxes[3].textContent+"' WON";
boxes[3].style.backgroundColor="green";
boxes[4].style.backgroundColor="green";
boxes[5].style.backgroundColor="green";
ist=false;
}
else if(boxes[6].textContent===boxes[7].textContent && boxes[6].textContent===boxes[8].textContent && boxes[8].textContent!="")
{
span.textContent="'"+boxes[6].textContent+"' WON";
boxes[6].style.backgroundColor="green";
boxes[7].style.backgroundColor="green";
boxes[8].style.backgroundColor="green";
ist=false;
}
else if(boxes[0].textContent===boxes[3].textContent && boxes[3].textContent===boxes[6].textContent && boxes[6].textContent!="")
{
span.textContent="'"+boxes[0].textContent+"' WON";
boxes[0].style.backgroundColor="green";
boxes[3].style.backgroundColor="green";
boxes[6].style.backgroundColor="green";
ist=false;
}
else if(boxes[1].textContent===boxes[4].textContent && boxes[4].textContent===boxes[7].textContent && boxes[7].textContent!="")
{
span.textContent="'"+boxes[1].textContent+"' WON";
boxes[4].style.backgroundColor="green";
boxes[1].style.backgroundColor="green";
boxes[7].style.backgroundColor="green";
ist=false;
}
else if(boxes[2].textContent===boxes[5].textContent && boxes[5].textContent===boxes[8].textContent && boxes[8].textContent!="")
{
span.textContent="'"+boxes[2].textContent+"' WON";
boxes[5].style.backgroundColor="green";
boxes[8].style.backgroundColor="green";
boxes[2].style.backgroundColor="green";
ist=false;
}
else if(boxes[0].textContent===boxes[4].textContent && boxes[4].textContent===boxes[8].textContent && boxes[8].textContent!="")
{
span.textContent="'"+boxes[0].textContent+"' WON";
boxes[0].style.backgroundColor="green";
boxes[4].style.backgroundColor="green";
boxes[8].style.backgroundColor="green";
ist=false;
}
else if(boxes[2].textContent===boxes[4].textContent && boxes[4].textContent===boxes[6].textContent && boxes[6].textContent!="")
{
span.textContent="'"+boxes[2].textContent+"' WON";
boxes[4].style.backgroundColor="green";
boxes[6].style.backgroundColor="green";
boxes[2].style.backgroundColor="green";
ist=false;
}
};