-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
157 lines (128 loc) · 3.62 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
//função que inicializa sozinha
(function() {
//array responsável por armazenar o jogo
var pecas = [];
var resposta = [];
var telaInicio = document.querySelector("#tela-inicio");
telaInicio.addEventListener("click", comecaJogo, false);
var telaFinal = document.querySelector("#tela-final");
//responsável por varrer o documento html, pegar as peças e colocar as referências no array (inicializar os elementos)
function init() {
for(var i = 1; i < 9; i++) {
var peca = document.querySelector("#peca" + i);
//peca.style.background = "url('img/peca" + i + ".png')" //mexe com o css do elemento
peca.addEventListener("click", movePeca, false);
pecas.push(peca);
}
//espaço em branco no tabuleiro
pecas.push(null);
resposta = pecas;
render();
}
//desenha o tabuleiro com as peças organizadas de acordo som suas posições do array
function render() {
//dentro do array
for(var i in pecas) {
var peca = pecas[i];
if(peca) {
peca.style.left = (i%3) * 230 + 5 + "px"; //% retorna a sobra da divisão por 3
if(i < 3) {
peca.style.top = "15px";
} else if(i < 6) {
peca.style.top = "245px";
} else {
peca.style.top = "475px"
}
}
}
}
function movePeca() {
var index = pecas.indexOf(this);
//se o elemento não estiver na coluna da esquerda
if(index % 3 !== 0) {
//verificar se tem espaço em branco à esquerda
if(!pecas[index - 1]) {
pecas[index - 1] = this;
pecas[index] = null;
}
}
if(index % 3 !== 2) {
//verificar se tem espaço em branco à esquerda
if(!pecas[index + 1]) {
pecas[index + 1] = this;
pecas[index] = null;
}
}
if(index > 2) {
//verificar se tem espaço em branco à esquerda
if(!pecas[index - 3]) {
pecas[index - 3] = this;
pecas[index] = null;
}
}
if(index < 6) {
//verificar se tem espaço em branco à esquerda
if(!pecas[index + 3]) {
pecas[index + 3] = this;
pecas[index] = null;
}
}
render();
if(verificaVitoria()) {
fimDeJogo();
}
}
function verificaVitoria() {
for(var i in pecas) {
var a = pecas[i];
var b = resposta[i];
if(a !== b) {
return false;
}
}
return true;
}
function fimDeJogo() {
telaFinal.style.opacity = "0.5";
telaFinal.style.zIndex = "1";
setTimeout(function() {
telaFinal.addEventListener("click", comecaJogo, false);
}, 500);
}
function randomSort(oldArray) {
var newArray;
var contar = 0;
do {
newArray = [];
while(newArray.length < oldArray.length) {
var i = Math.floor(Math.random() * oldArray.length); //gera um número aleatório arredondado entre 0 e 8
if(newArray.indexOf(oldArray[i]) < 0) {
newArray.push(oldArray[i]);
}
}
contar++;
console.log(contar);//verifica quantos jogos foram criados até ter uma ordem válida
} while(!validaJogo(newArray)); //! inverte os valores v ou f retornados pela função validaJogo
return newArray;
}
function validaJogo(array) {
var inversoes = 0;
var elementos = array.length;
for(var i = 0; i < elementos - 1; i++) {
for(var j = i + 1; j < elementos; j++) {
if(array[i] && array[j] && array[i].dataset.value < array[j].dataset.value) {
inversoes++;
}
}
}
return inversoes%2 === 0; //inversoes dividido por 2 gera raiz do zero(se é par)
}
function comecaJogo() {
pecas = randomSort(pecas);
this.style.opacity = "0";
this.style.zIndex = "-1";
this.removeEventListener("click", comecaJogo, false);
render();
}
init();
} ());