-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
84 lines (64 loc) · 1.43 KB
/
index.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
var current = "X";
function has(id, check) {
if (document.getElementById(id).innerHTML == check) {
return true;
}else{
return false;
}
}
function checkWin(cur) {
var check = cur;
var one = "r1c1";
var two = "r1c2";
var three = "r1c3";
var four = "r2c1";
var five = "r2c2";
var six = "r2c3";
var seven = "r3c1";
var eight = "r3c2";
var nine = "r3c3";
var wins = false;
if (has(one, check) && has(two, check) && has(three, check)) {
wins = true;
}
if (has(four, check) && has(five, check) && has(six, check)) {
wins = true;
}
if (has(seven, check) && has(eight, check) && has(nine, check)) {
wins = true;
}
if (has(one, check) && has(four, check) && has(seven, check)) {
wins = true;
}
if (has(two, check) && has(five, check) && has(eight, check)) {
wins = true;
}
if (has(three, check) && has(six, check) && has(nine, check)) {
wins = true;
}
if (has(one, check) && has(five, check) && has(nine, check)) {
wins = true;
}
if (has(three, check) && has(five, check) && has(seven, check)) {
wins = true;
}
return wins;
}
function updateHTML(id) {
document.getElementById(id).innerHTML = current;
}
function buttonClicked(id) {
updateHTML(id);
document.getElementById(id).disabled = 'true';
setTimeout(function() {
var won = checkWin(current);
if (won) {
alert(current + " wins!");
}
if (current == "X") {
current = "O";
}else{
current = "X";
}
}, 100);
}