-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.js
115 lines (96 loc) · 2.83 KB
/
functions.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
// determine if X or O.
function determineWinners(iboard) {
var mark = '';
var winners = {};
if ((board[0] === board[1]) && (board[1] === board[2])){
mark = board[0];
} else if ((board[3] === board[4]) && (board[4] === board[5])){
mark = board[3];
} else if ((board[6] === board[7]) && (board[7] === board[8])) {
mark = board[6];
}else if ((board[0] === board[3]) && (board[3] === board[6])) {
mark = board[0];;
} else if ((board[1] === board[4]) && (board[4] === board[7])){
mark = board[1];
} else if ((board[2] === board[5]) && (board[5] === board[8])){
mark = board[2];
} else if ((board[0] === board[4]) && (board[4] === board[8])) {
mark = board[0];
} else if ((board[2] === board[4]) && (board[4] === board[6])) {
mark = board[2];
}
if ((board[0] === board[1]) && (board[1] === board[2]) && (board[2] == mark)){
winners[1] = 1;
winners[2] = 1;
winners[3] = 1;
}
if ((board[3] === board[4]) && (board[4] === board[5]) && (board[5] == mark)){
winners[4] = 1;
winners[5] = 1;
winners[6] = 1;
}
if ((board[6] === board[7]) && (board[7] === board[8]) && (board[8] == mark)) {
winners[7] = 1;
winners[8] = 1;
winners[9] = 1;
}
if ((board[0] === board[3]) && (board[3] === board[6]) && (board[6] == mark)) {
winners[1] = 1;
winners[4] = 1;
winners[7] = 1;
}
if ((board[1] === board[4]) && (board[4] === board[7]) && (board[7] == mark)){
winners[2] = 1;
winners[5] = 1;
winners[8] = 1;
}
if ((board[2] === board[5]) && (board[5] === board[8]) && (board[8] == mark)){
winners[3] = 1;
winners[6] = 1;
winners[9] = 1;
}
if ((board[0] === board[4]) && (board[4] === board[8]) && (board[8] == mark)) {
winners[1] = 1;
winners[5] = 1;
winners[9] = 1;
}
if ((board[2] === board[4]) && (board[4] === board[6]) && (board[6] == mark)) {
winners[3] = 1;
winners[5] = 1;
winners[7] = 1;
}
return winners;
}
function stringifyWinners(win_list) {
var wlist = 'Players ';
for (var k in win_list) {
if (win_list.hasOwnProperty(k)) {
wlist += k + ' ';
}
}
wlist += 'win!'
return wlist;
}
function drawTable(itable) {
var side = Math.sqrt(itable.length);
var table = '<table border = "1">';
for (var i = 0; i < side; i++) {
table += '<tr>';
// console.log("here")
// console.log(users);
for (var j = 0; j < side; j++) {
// table += '<td>' + itable[i*side + j] + '</td>';
// if (users[i*side + j] == 0) {
// console.log('red');
// table += '<td bgcolor="#FF0000">' + itable[i*side + j] + '</td>'
// } else {
// console.log('clear');
table += '<td>' + itable[i*side + j] + '</td>'
// };
}
table += '</tr>';
}
table += '</table>'
var div = document.getElementById('boardDiv');
div.innerHTML = table;
}