-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
142 lines (102 loc) · 5.29 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
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
// change player name.
var P1="PLAYER1";
var P2="PLAYER2";
function changeName(){
P1=prompt("Name of the player who choose: 'X' !!" );
P2=prompt("Name of the player who choose: 'O' !!" );
var str1=P1+" : X";
var str2=P2+" : O";
document.getElementById("result").innerHTML=str1+"<br>"+str2;
document.getElementById("result").style.color="ANTIQUEWHITE";
}
var flag=true;
var t=0;
function game(){
if(flag==true){
document.activeElement.innerHTML="X";
document.activeElement.removeAttribute("onclick");
document.activeElement.style.backgroundColor="#2a74f0";
t++;
flag=false;
var final=checkresult();
if(final=="stop"){
document.getElementById("result").innerHTML=P1+" WINS !!";
document.getElementById("result").style.fontSize="45px";
document.getElementById("result").style.color="antiquewhite";
document.getElementById("result").style.paddingTop="30px";
removeATT();
}
else if(t==9){
document.getElementById("result").innerHTML="MATCH DRAW !!";
}
}
else{
document.activeElement.innerHTML="O";
document.activeElement.style.backgroundColor="hotpink";
document.activeElement.removeAttribute("onclick");
t++;
flag=true;
var final=checkresult();
if(final=="stop"){
document.getElementById("result").innerHTML=P2+" WINS !!";
document.getElementById("result").style.fontSize="45px";
document.getElementById("result").style.color="antiquewhite";
document.getElementById("result").style.paddingTop="30px";
removeATT();
}
else if(t==9){
document.getElementById("result").innerHTML="MATCH DRAW !!";
}
}
}
function checkresult(){
var str;
if((document.getElementById("b1").innerHTML==document.getElementById("b2").innerHTML && document.getElementById("b1").innerHTML==document.getElementById("b3").innerHTML) && ((document.getElementById("b1").innerHTML=="X") || (document.getElementById("b1").innerHTML=="O"))){
str="stop";
return str;
}
else if((document.getElementById("b1").innerHTML==document.getElementById("b4").innerHTML && document.getElementById("b1").innerHTML==document.getElementById("b7").innerHTML) && ((document.getElementById("b1").innerHTML=="X") || (document.getElementById("b1").innerHTML=="O"))){
str="stop";
return str;
}
else if((document.getElementById("b1").innerHTML==document.getElementById("b5").innerHTML && document.getElementById("b1").innerHTML==document.getElementById("b9").innerHTML) && ((document.getElementById("b1").innerHTML=="X") || (document.getElementById("b1").innerHTML=="O"))){
str="stop";
return str;
}
else if((document.getElementById("b3").innerHTML==document.getElementById("b6").innerHTML && document.getElementById("b9").innerHTML==document.getElementById("b3").innerHTML) && ((document.getElementById("b3").innerHTML=="X") || (document.getElementById("b3").innerHTML=="O"))){
str="stop";
return str;
}
else if((document.getElementById("b3").innerHTML==document.getElementById("b5").innerHTML && document.getElementById("b3").innerHTML==document.getElementById("b7").innerHTML) && ((document.getElementById("b3").innerHTML=="X") || (document.getElementById("b3").innerHTML=="O"))){
str="stop";
return str;
}
else if((document.getElementById("b7").innerHTML==document.getElementById("b8").innerHTML && document.getElementById("b7").innerHTML==document.getElementById("b9").innerHTML) && ((document.getElementById("b7").innerHTML=="X") || (document.getElementById("b7").innerHTML=="O"))){
str="stop";
return str;
}
else if((document.getElementById("b4").innerHTML==document.getElementById("b6").innerHTML && document.getElementById("b4").innerHTML==document.getElementById("b5").innerHTML) && ((document.getElementById("b4").innerHTML=="X") || (document.getElementById("b4").innerHTML=="O"))){
str="stop";
return str;
}
else if((document.getElementById("b2").innerHTML==document.getElementById("b5").innerHTML && document.getElementById("b2").innerHTML==document.getElementById("b8").innerHTML) && ((document.getElementById("b2").innerHTML=="X") || (document.getElementById("b2").innerHTML=="O"))){
str="stop";
return str;
}
}
//Remove attribute
function removeATT(){
document.getElementById("b1").removeAttribute("onclick");
document.getElementById("b2").removeAttribute("onclick");
document.getElementById("b3").removeAttribute("onclick");
document.getElementById("b4").removeAttribute("onclick");
document.getElementById("b5").removeAttribute("onclick");
document.getElementById("b6").removeAttribute("onclick");
document.getElementById("b7").removeAttribute("onclick");
document.getElementById("b8").removeAttribute("onclick");
document.getElementById("b9").removeAttribute("onclick");
}
//replay game
function replay(){
window.location.reload();
}