-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
148 lines (141 loc) · 5.19 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
var cube = document.getElementsByClassName("box");
var count = 0;
let turn = 1;
let green = "#5ef141";
function func(ths) {
let playerTurn = document.getElementsByClassName("player_turn");
if (turn === 1) playerTurn[0].innerHTML = "Player 2 Turn";
else if (turn === 2) playerTurn[0].innerHTML = "Player 1 Turn";
ths.classList.add("click-disable");
ths.classList.add("click-disable");
count++;
if (count % 2 == 0) {
ths.innerHTML = "X";
ths.accessKey = 1;
turn = 1;
} else {
ths.innerHTML = "O";
ths.accessKey = 0;
turn = 2;
}
const a = parseInt(cube[0].accessKey);
const b = parseInt(cube[1].accessKey);
const c = parseInt(cube[2].accessKey);
const d = parseInt(cube[3].accessKey);
const e = parseInt(cube[4].accessKey);
const f = parseInt(cube[5].accessKey);
const g = parseInt(cube[6].accessKey);
const h = parseInt(cube[7].accessKey);
const i = parseInt(cube[8].accessKey);
if (
a + b + c == 3 ||
d + e + f == 3 ||
g + h + i == 3 ||
a + d + g == 3 ||
b + e + h == 3 ||
c + f + i == 3 ||
a + e + i == 3 ||
c + e + g == 3
) {
if (a + b + c == 3) {
cube[0].style.backgroundColor = green;
cube[1].style.backgroundColor = green;
cube[2].style.backgroundColor = green;
}else if(d + e + f == 3){
cube[3].style.backgroundColor = green;
cube[4].style.backgroundColor = green;
cube[5].style.backgroundColor = green;
}else if(g + h + i == 3){
cube[6].style.backgroundColor = green;
cube[7].style.backgroundColor = green;
cube[8].style.backgroundColor = green;
}else if(a + d + g == 3){
cube[0].style.backgroundColor = green;
cube[3].style.backgroundColor = green;
cube[6].style.backgroundColor = green;
}else if(b + e + h == 3){
cube[1].style.backgroundColor = green;
cube[4].style.backgroundColor = green;
cube[7].style.backgroundColor = green;
}else if(c + f + i == 3){
cube[2].style.backgroundColor = green;
cube[5].style.backgroundColor = green;
cube[8].style.backgroundColor = green;
}else if(a + e + i == 3){
cube[0].style.backgroundColor = green;
cube[4].style.backgroundColor = green;
cube[8].style.backgroundColor = green;
}else if(c + e + g == 3){
cube[2].style.backgroundColor = green;
cube[4].style.backgroundColor = green;
cube[6].style.backgroundColor = green;
}
//alert("X wins!! ");
document.getElementById("result").innerText = "Player 2 (X) wins!!";
on();
} else if (
a + b + c == 0 ||
d + e + f == 0 ||
g + h + i == 0 ||
a + d + g == 0 ||
b + e + h == 0 ||
c + f + i == 0 ||
a + e + i == 0 ||
c + e + g == 0
) {
//alert("O wins!! ");
document.getElementById("result").innerText = "Player 1 (O) wins!!";
on();
if (a + b + c == 0) {
cube[0].style.backgroundColor = green;
cube[1].style.backgroundColor = green;
cube[2].style.backgroundColor = green;
}else if(d + e + f == 0){
cube[3].style.backgroundColor = green;
cube[4].style.backgroundColor = green;
cube[5].style.backgroundColor = green;
}else if(g + h + i == 0){
cube[6].style.backgroundColor = green;
cube[7].style.backgroundColor = green;
cube[8].style.backgroundColor = green;
}else if(a + d + g == 0){
cube[0].style.backgroundColor = green;
cube[3].style.backgroundColor = green;
cube[6].style.backgroundColor = green;
}else if(b + e + h == 0){
cube[1].style.backgroundColor = green;
cube[4].style.backgroundColor = green;
cube[7].style.backgroundColor = green;
}else if(c + f + i == 0){
cube[2].style.backgroundColor = green;
cube[5].style.backgroundColor = green;
cube[8].style.backgroundColor = green;
}else if(a + e + i == 0){
cube[0].style.backgroundColor = green;
cube[4].style.backgroundColor = green;
cube[8].style.backgroundColor = green;
}else if(c + e + g == 0){
cube[2].style.backgroundColor = green;
cube[4].style.backgroundColor = green;
cube[6].style.backgroundColor = green;
}
} else if (a + b + c + d + e + f + g + h + i) {
//alert("Its a Draw !!!");
document.getElementById("result").innerText = "It's a Draw!!";
on();
}
}
function on(){
document.getElementById("overlay").style.display = "block";
}
// Resets inmediatly the game if the user has finished. If not it will ask
// for a confirmation alert
function restart(isGameFinished) {
if (isGameFinished) { return reloadWindow() }
if (confirm('Are you sure yo want to restart the game?')) {
window.location.reload();
}
}
function reloadWindow() {
window.location.reload();
}