forked from Aniket965/Hello-world
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTIC-TAC-TOE.html
384 lines (330 loc) · 10.4 KB
/
TIC-TAC-TOE.html
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TIC TAC TOE</title>
<style>
table {
width: 100%;
}
.tictac
{
background:#cecece;
border:#999 10px solid;
width:100%;
height:33%;
font-size:7em;
}
.btn
{
background:#cecece;
border:#999 10px groove;
width:100%;
min-height:180px;
font-size:3em;
display: none;
}
</style>
</head>
<script>
//variável de controle gameOver inicializando com false
var gameOver = false;
//variável de controle positions para saber quantas posições foram ocupadas
var positions = 9;
//variável para identificar o jogador, sendo X ou O
var player = 'X';
//variável de controle hasWin indicando que no começo não existe um ganhador
var hasWin = 0;
var lastModify;
//criação do array
var squares = new Array(9);
//a inicialização neste caso não é necessária, mas se quiser pode fazer
//squares[0] = 0;
//squares[1] = 0;
//squares[2] = 0;
//squares[3] = 0;
//squares[4] = 0;
//squares[5] = 0;
//squares[6] = 0;
//squares[7] = 0;
//squares[8] = 0//;
//criação do array com a possibilidades para ********
var preWinCombinations = [
[0, 1, 2],
[0, 3, 6],
[0, 4, 8],
[1, 2, 0],
[1, 4, 7],
[2, 4, 6],
[2, 5, 8],
[3, 4, 5],
[3, 6, 0],
[4, 5, 3],
[4, 6, 2],
[4, 7, 1],
[4, 8, 0],
[5, 8, 2],
[6, 7, 8],
[7, 8, 6],
//Com uma casa de diferença
[0, 2, 1],
[0, 6, 3],
[0, 8, 4],
[2, 6, 4],
[2, 8, 5],
[6, 8, 7],
[1, 3, 0],
[1, 5, 2],
[3, 7, 6],
[7, 5, 8]
];
//criação do array com a possibilidades para ganhar o jogo
var winCombinations = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];
//criação do array com a possibilidades para ********
var tipCombinations = [
[0, 8],
[6, 2],
[2, 6],
[8, 0]
];
//criação do array com a possibilidades para ********
var forkCombinations = [
[0, 8],
[6, 2],
[5, 6],
[0, 5]
//[7, 5],
//[1, 3],
//[7, 8],
//[5, 1]
];
//criação do array com a possibilidades para ********
var middleSquares = [
[1],
[3],
[5],
[7]
];
//função utilizada para o momento do clique nos botões do jogador
function squareClick(square){
//variável para obter o valor da região ou ID clicado
var idElement = document.getElementById(square).value;
//uso da função nativa substring para obter a parte inteira de sqr7 (obter o 4 elemento da variável do tipo String)
var parseSquare = parseInt(square.substring(3, 4));
//fazer o controle de deixar preencher com o valor X ou 0 se estiver vazio
if(idElement == "") {
setSquare(parseSquare ,player);
//verificar se houve um ganhador sempre
checkForWinner(player);
//alterar o jogador
if(player == 'O') player = 'X';
else player = 'O';
macRound();
}
}
function macRound(){
if (victoriousMovement()) {
} else if (losingMovement()) {
} else if (centerNull()) {
} else if (forkTentative()){
} else if (tipNull()){
} else if (tipFilled()) {
} else {
fillsNext()
}
//verificar se houve um ganhador sempre
checkForWinner(player);
//alterar o jogador
if(player == 'O') player = 'X';
else player = 'O';
}
function victoriousMovement(){
console.log('victoriousMovement');
var victoriousMovement = false;
for(var i = 0; i < preWinCombinations.length; i++) {
if(squares[preWinCombinations[i][0]] == "O" && squares[preWinCombinations[i][1]] == "O") {
console.log('igual '+preWinCombinations[i][0]+' '+preWinCombinations[i][1]);
if (document.getElementById("sqr"+preWinCombinations[i][2]).value == "") {
console.log('res '+preWinCombinations[i][2]);
setSquare(preWinCombinations[i][2] ,'O');
i = preWinCombinations.length+1;
victoriousMovement = true;
}
}
}
return victoriousMovement;
}
function losingMovement(){
console.log('losingMovement');
var losingMovement = false;
for(var i = 0; i < preWinCombinations.length; i++) {
if(squares[preWinCombinations[i][0]] == "X" && squares[preWinCombinations[i][1]] == "X") {
console.log('igual '+preWinCombinations[i][0]+' '+preWinCombinations[i][1]);
if (document.getElementById("sqr"+preWinCombinations[i][2]).value == "") {
console.log('res '+preWinCombinations[i][2]);
setSquare(preWinCombinations[i][2] ,'O');
i = preWinCombinations.length+1;
losingMovement = true;
}
}
}
return losingMovement;
}
function centerNull(){
console.log('centerNull');
var centerNull = false;
if(squares[4] == null) {
console.log('centerNull '+squares[4]);
if (document.getElementById("sqr4").value == "") {
setSquare(4 ,'O');
centerNull = true;
}
}
return centerNull;
}
function tipFilled(){
console.log('tipFilled');
var tipFilled = false;
for(var i = 0; i < tipCombinations.length; i++) {
if(tipCombinations[i][0] == lastModify) {
if (document.getElementById("sqr"+tipCombinations[i][1]).value == "") {
setSquare(tipCombinations[i][1] ,'O');
i = tipCombinations.length+1;
tipFilled = true;
}
}
}
return tipFilled;
}
function tipNull(){
console.log('tipNull');
var tipNull = false;
for(var i = 0; i < tipCombinations.length; i++) {
if(squares[tipCombinations[i][0]] == null) {
if (document.getElementById("sqr"+tipCombinations[i][0]).value == "") {
setSquare(tipCombinations[i][0] ,'O');
i = tipCombinations.length+1;
tipNull = true;
}
}
}
return tipNull;
}
function middleNull(){
console.log('middleNull');
var middleNull = false;
for(var i = 0; i < middleSquares.length; i++) {
if(squares[middleSquares[i]] == null) {
if (document.getElementById("sqr"+middleSquares[i]).value == "") {
setSquare(middleSquares[i] ,'O');
i = middleSquares.length+1;
middleNull = true;
}
}
}
return middleNull;
}
function fillsNext(){
console.log('fillsNext');
for (var i = 0; i < squares.length; i++){
if (squares[i] == null) {
if (document.getElementById("sqr"+i).value == "") {
console.log('Nulo '+i);
setSquare(i ,'O');
i=squares.length+1;
}
}
}
}
function forkTentative(){
console.log('forkTentative');
var forkTentative = false;
for(var i = 0; i < forkCombinations.length; i++) {
if(squares[forkCombinations[i][0]] == "X" && squares[forkCombinations[i][1]] == "X") {
middleNull();
i = tipCombinations.length+1;
forkTentative = true;
}
}
return forkTentative;
}
function setSquare(square, player){
document.getElementById("sqr"+square).value = player;
lastModify = square;
squares[square] = player;
}
//função usada para zerar as variáveis e possibilitar o novo jogo
function playAgain() {
//varrer todas as posições
for (var i = 0; i < squares.length; i++)
{
var htmlButton = "sqr" + i;
//colocar em todos os botões o valor vazio
document.getElementById(htmlButton).value = '';
}
//resetar o array
squares = [];
//voltar com as 9 posições
positions = 9;
//voltar com o valor false para a variável gameOver
gameOver = false;
//ocultar o botão JOGAR DE NOVO
document.getElementById("btn").style.display = 'none';
}
//função que verifica se existe um ganhador
function checkForWinner(value) {
//verificar dentro de todas as combinações se tem os valores de X ou de O
for(var i = 0; i < winCombinations.length; i++) {
//se todas forem preenchidas com X ou todas forem preenchidas com O temos um ganhardor
if(squares[winCombinations[i][0]] == value && squares[winCombinations[i][1]] == value && squares[winCombinations[i][2]] == value) {
//uso de alert para mostrar o ganhador
alert(value + " GANHOU O JOGO!");
//colocar a variável de controle gameOver com o valor true
gameOver = true;
//mostrar o botão JOGAR DE NOVO
document.getElementById("btn").style.display = 'block';
//agora temos um ganhador, portanto hasWin=1
hasWin = 1;
}
}
//depois de verificar se houve ou não um ganhador diminuir a variável positions indicando uma posição ocupada
positions--;
//se o valor de positions for 0 e não houver um ganhador ainda, deu empate
if(positions==0 && !hasWin) {
alert("NINGUÉM GANHOU!");
gameOver = true;
document.getElementById("btn").style.display = 'block';
}
}
</script>
<body>
<table>
<tr>
<td><input type="button" id="sqr0" name="sqr0" class="tictac" onClick="squareClick('sqr0')" /></td>
<td><input type="button" id="sqr1" name="sqr1" class="tictac" onClick="squareClick('sqr1')" /></td>
<td><input type="button" id="sqr2" name="sqr2" class="tictac" onClick="squareClick('sqr2')" /></td>
</tr>
<tr>
<td><input type="button" id="sqr3" name="sqr3" class="tictac" onClick="squareClick('sqr3')" /></td>
<td><input type="button" id="sqr4" name="sqr4" class="tictac" onClick="squareClick('sqr4')" /></td>
<td><input type="button" id="sqr5" name="sqr5" class="tictac" onClick="squareClick('sqr5')" /></td>
</tr>
<tr>
<td><input type="button" id="sqr6" name="sqr6" class="tictac" onClick="squareClick('sqr6')" /></td>
<td><input type="button" id="sqr7" name="sqr7" class="tictac" onClick="squareClick('sqr7')" /></td>
<td><input type="button" id="sqr8" name="sqr8" class="tictac" onClick="squareClick('sqr8')" /></td>
</tr>
</table>
<input type="button" id="btn" value="JOGAR DE NOVO" name="again" class="btn" onClick="playAgain()" />
</body>
</html>