1
+ var current = "X" ;
2
+
3
+ function has ( id , check ) {
4
+ if ( document . getElementById ( id ) . innerHTML == check ) {
5
+ return true ;
6
+ } else {
7
+ return false ;
8
+ }
9
+ }
10
+
11
+ function checkWin ( cur ) {
12
+
13
+ var check = cur ;
14
+
15
+ var one = "r1c1" ;
16
+ var two = "r1c2" ;
17
+ var three = "r1c3" ;
18
+
19
+ var four = "r2c1" ;
20
+ var five = "r2c2" ;
21
+ var six = "r2c3" ;
22
+
23
+ var seven = "r3c1" ;
24
+ var eight = "r3c2" ;
25
+ var nine = "r3c3" ;
26
+
27
+ var wins = false ;
28
+
29
+ if ( has ( one , check ) && has ( two , check ) && has ( three , check ) ) {
30
+ wins = true ;
31
+ }
32
+
33
+ if ( has ( four , check ) && has ( five , check ) && has ( six , check ) ) {
34
+ wins = true ;
35
+ }
36
+
37
+ if ( has ( seven , check ) && has ( eight , check ) && has ( nine , check ) ) {
38
+ wins = true ;
39
+ }
40
+
41
+ if ( has ( one , check ) && has ( four , check ) && has ( seven , check ) ) {
42
+ wins = true ;
43
+ }
44
+
45
+ if ( has ( two , check ) && has ( five , check ) && has ( eight , check ) ) {
46
+ wins = true ;
47
+ }
48
+
49
+ if ( has ( three , check ) && has ( six , check ) && has ( nine , check ) ) {
50
+ wins = true ;
51
+ }
52
+
53
+ if ( has ( one , check ) && has ( five , check ) && has ( nine , check ) ) {
54
+ wins = true ;
55
+ }
56
+
57
+ if ( has ( three , check ) && has ( five , check ) && has ( seven , check ) ) {
58
+ wins = true ;
59
+ }
60
+
61
+ return wins ;
62
+
63
+ }
64
+
65
+ function updateHTML ( id ) {
66
+ document . getElementById ( id ) . innerHTML = current ;
67
+ }
68
+
69
+ function buttonClicked ( id ) {
70
+ updateHTML ( id ) ;
71
+ document . getElementById ( id ) . disabled = 'true' ;
72
+
73
+ setTimeout ( function ( ) {
74
+ var won = checkWin ( current ) ;
75
+ if ( won ) {
76
+ alert ( current + " wins!" ) ;
77
+ }
78
+ if ( current == "X" ) {
79
+ current = "O" ;
80
+ } else {
81
+ current = "X" ;
82
+ }
83
+ } , 100 ) ;
84
+ }
0 commit comments