-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
204 lines (182 loc) · 6.18 KB
/
script.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
var counter = 0;
var events = 0;
var currentPlayer;
var canPlay = true;
// Caching grid table data
var r1c1, r1c2, r1c3, r2c1, r2c2, r2c3, r3c1, r3c2, r3c3;
r1c1 = r1c2 = r1c3 = r2c1 = r2c2 = r2c3 = r3c1 = r3c2 = r3c3 = -1;
const r1c1_td = document.getElementById("r1c1");
const r1c2_td = document.getElementById("r1c2");
const r1c3_td = document.getElementById("r1c3");
const r2c1_td = document.getElementById("r2c1");
const r2c2_td = document.getElementById("r2c2");
const r2c3_td = document.getElementById("r2c3");
const r3c1_td = document.getElementById("r3c1");
const r3c2_td = document.getElementById("r3c2");
const r3c3_td = document.getElementById("r3c3");
const result_h2 = document.getElementById("result");
const button = document.getElementById("button");
// Classes
function player(mark, name) {
this.mark = mark;
}
// Object declaration
// var player1 = new player(prompt("Player 1: Please choose a letter! (i.e. \'x\', \'o\'"));
// var player2 = new player(prompt("Player 2: Please choose a letter! (i.e. \'x\', \'o\'"));
var player1 = new player("X", "Player X");
var player2 = new player("O", "Player O");
function checkWin(cpl) {
var horizontal = (r1c1 == r1c2 && r1c2 == r1c3 && r1c3 == cpl.mark) || (r2c1 == r2c2 && r2c2 == r2c3 && r2c3 == cpl.mark) || (r3c1 == r3c2 && r3c2 == r3c3 && r3c3 == cpl.mark);
var vertical = (r1c1 == r2c1 && r2c1 == r3c1 && r3c1 == cpl.mark) || (r1c2 == r2c2 && r2c2 == r3c2 && r3c2 == cpl.mark) || (r1c3 == r2c3 && r2c3 == r3c3 && r3c3 == cpl.mark);
var diagonal = (r1c1 == r2c2 && r2c2 == r3c3 && r3c3 == cpl.mark) || (r1c3 == r2c2 && r2c2 == r3c1 && r3c1 == cpl.mark);
if (horizontal || vertical || diagonal) {
// alert("winner");
// result_h2.innerHTML = cpl.name + " wins!!";
// reset();
return true;
} else
return false;
}
function placeLetter(place) {
if (canPlay) {
if (counter % 2 === 0) {
currentPlayer = player1;
} else
currentPlayer = player2;
switch (place) {
case "r1c1":
if (r1c1 == -1) {
r1c1 = currentPlayer.mark;
r1c1_td.innerHTML = r1c1;
events++;
counter++;
}
break;
case "r1c2":
if (r1c2 == -1) {
r1c2 = currentPlayer.mark;
r1c2_td.innerHTML = r1c2;
events++;
counter++;
}
break;
case "r1c3":
if (r1c3 == -1) {
r1c3 = currentPlayer.mark;
r1c3_td.innerHTML = r1c3;
events++;
counter++;
}
break;
case "r2c1":
if (r2c1 == -1) {
r2c1 = currentPlayer.mark;
r2c1_td.innerHTML = r2c1;
events++;
counter++;
}
break;
case "r2c2":
if (r2c2 == -1) {
r2c2 = currentPlayer.mark;
r2c2_td.innerHTML = r2c2;
events++;
counter++;
}
break;
case "r2c3":
if (r2c3 == -1) {
r2c3 = currentPlayer.mark;
r2c3_td.innerHTML = r2c3;
events++;
counter++;
}
break;
case "r3c1":
if (r3c1 == -1) {
r3c1 = currentPlayer.mark;
r3c1_td.innerHTML = r3c1;
events++;
counter++;
}
break;
case "r3c2":
if (r3c2 == -1) {
r3c2 = currentPlayer.mark;
r3c2_td.innerHTML = r3c2;
events++;
counter++;
}
break;
case "r3c3":
if (r3c3 == -1) {
r3c3 = currentPlayer.mark;
r3c3_td.innerHTML = r3c3;
events++;
counter++;
}
break;
}
let win = checkWin(currentPlayer);
if (events === 9 && !win) {
// alert("Cats Game! :(");
result_h2.innerHTML = "Cats Game :(";
reset();
} else if (win) {
counter--;
if (counter % 2 === 0) {
result_h2.innerHTML = "Player X wins!!";
} else
result_h2.innerHTML = "Player O wins!!";
reset();
} else {
if (counter % 2 === 0) {
result_h2.innerHTML = "Player X\'s turn";
} else
result_h2.innerHTML = "Player O\'s turn";
}
}
}
function reset() {
canPlay = false;
button.innerHTML = "Play again?";
button.addEventListener('click', function() {
r1c1_td.innerHTML = r1c2_td.innerHTML = r1c3_td.innerHTML = r2c1_td.innerHTML =
r2c2_td.innerHTML = r2c3_td.innerHTML = r3c1_td.innerHTML = r3c2_td.innerHTML = r3c3_td.innerHTML = " ";
r1c1 = r1c2 = r1c3 = r2c1 = r2c2 = r2c3 = r3c1 = r3c2 = r3c3 = -1;
counter = events = 0;
button.innerHTML = "";
canPlay = true;
// main();
})
}
function main() {
r1c1_td.addEventListener('click', function() {
placeLetter("r1c1");
});
r1c2_td.addEventListener('click', function() {
placeLetter("r1c2");
});
r1c3_td.addEventListener('click', function() {
placeLetter("r1c3");
});
r2c1_td.addEventListener('click', function() {
placeLetter("r2c1");
});
r2c2_td.addEventListener('click', function() {
placeLetter("r2c2");
});
r2c3_td.addEventListener('click', function() {
placeLetter("r2c3");
});
r3c1_td.addEventListener('click', function() {
placeLetter("r3c1");
});
r3c2_td.addEventListener('click', function() {
placeLetter("r3c2");
});
r3c3_td.addEventListener('click', function() {
placeLetter("r3c3");
});
}
main();