-
Notifications
You must be signed in to change notification settings - Fork 0
/
tic.js
91 lines (74 loc) · 1.47 KB
/
tic.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
function game(){
document.turn="x";
document.a=null;
document.count=0;
}
function text(square){
if (document.a!=null){
player(document.turn+ " won the game,if you want play again, restart the game :)")
}
else if (square.innerText==''){
square.innerText=document.turn;
change();
}
else{
alert("select other box please")
}
}
function change(){
document.count++;
if(winner(document.turn))
{
player("congrats " +document.turn+ " won the game")
}
else if(document.count==9){
player(" The game is draw")
}
else if (document.turn=="x"){
document.turn="o";
player(document.turn+" need to play")
}
else{
document.turn="x";
player(document.turn+" need to play")
}
}
function player(message)
{
var b=document.getElementById("turn");
b.innerText=message;
}
function winner(a){
var result=false;
if (row(1,2,3,a)||
row(4,5,6,a)||
row(7,8,9,a)||
row(1,4,7,a)||
row(2,5,8,a)||
row(3,6,9,a)||
row(1,5,9,a)||
row(3,5,7,a)
){
result=true;
document.a=1;
}
return result;
}
function row(a,b,c,move){
var rowCheck=false;
if (data(a)==move && data(b)==move && data(c)==move)
rowCheck=true
return rowCheck
}
function data(a){
return document.getElementById("s"+a).innerText;
}
function restart(){
var i;
for(i=1;i<10;i++){
document.getElementById("s"+i).innerText='';
}
document.a=null;
document.count=0;
player("The game starts with X")
}