-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathticTac.js
123 lines (107 loc) · 3.66 KB
/
ticTac.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
var turn = 'X';
var score = {
'X': 0,
'O': 0
};
var gridValue = 0;
function fnLoad() {
var select = document.getElementById("grid");
for (i = 3; i <= 100; i += 1) {
var option = document.createElement('option');
select.options[select.options.length] = new Option(i + ' X ' + i, i);
}
addEvent(document.getElementById("game"), "click", fnChoose);
fnNewGame();
}
function addEvent(element, eventName, callback) {
if (element.addEventListener) {
element.addEventListener(eventName, callback, false);
} else if (element.attachEvent) {
element.attachEvent("on" + eventName, callback);
}
}
function fnChoose(e) {
if (e.target && e.target.nodeName == "TD") {
var targetElement = document.getElementById(e.target.id);
var prevTurn;
if ((targetElement.className).indexOf("disabled") == -1) {
targetElement.innerHTML = turn;
targetElement.classList.add('disabled');
targetElement.classList.add(turn);
score[turn] += 1;
prevTurn = turn;
turn = turn === "X" ? "O" : "X";
if (fndecide(targetElement, prevTurn)) {
alert(prevTurn + ' has won the game');
fnNewGame();
} else if ((score['X'] + score['O']) == (gridValue * gridValue)) {
alert('Draw!');
fnNewGame();
}
}
}
}
function fndecide(targetElement, prevTurn) {
var UL = document.getElementById('game');
var elements, i, j, cnt;
if (score[prevTurn] >= gridValue) {
var classes = targetElement.className.split(/\s+/);
for (i = 0; i < classes.length; i += 1) {
cnt = 0;
if (classes[i].indexOf('row') !== -1 || classes[i].indexOf('col') !== -1 || classes[i].indexOf('dia') !== -1) {
elements = UL.getElementsByClassName(classes[i]);
for (j = 0; j < elements.length; j += 1) {
if (elements[j].innerHTML == prevTurn) {
cnt += 1; }
}
if (cnt == gridValue) {
return true;
}
}
}
}
return false;
}
function fnNewGame() {
var gameUL = document.getElementById("game");
if (gameUL.innerHTML !== '') {
gameUL.innerHTML = null;
score = {
'X': 0,
'O': 0
};
turn = 'X';
gridValue = 0;
}
var select = document.getElementById("grid");
gridValue = select.options[select.selectedIndex].value;
var i, j, li, k = 0,
classLists;
var gridAdd = +gridValue + 1;
for (i = 1; i <= gridValue; i += 1) {
tr = document.createElement('tr');
for (j = 1; j <= gridValue; j += 1) {
k += 1;
li = document.createElement('td');
li.setAttribute("id", 'li' + k);
classLists = 'td row' + i + ' col' + j;
if (i === j) {
classLists = 'td row' + i + ' col' + j + ' dia0';
}
if ((i + j) === gridAdd) {
classLists = 'td row' + i + ' col' + j + ' dia1';
}
if (!isEven(gridValue) && (Math.round(gridValue / 2) === i && Math.round(gridValue / 2) === j))
classLists = 'td row' + i + ' col' + j + ' dia0 dia1';
li.className = classLists;
tr.appendChild(li);
}
gameUL.appendChild(tr);
}
}
function isEven(value) {
if (value % 2 == 0)
return true;
else
return false;
}