-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerarTablero.js
227 lines (198 loc) · 6.63 KB
/
generarTablero.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
var range = function(start, end, step) {
var range = [];
var typeofStart = typeof start;
var typeofEnd = typeof end;
if (step === 0) {
throw TypeError("Step cannot be zero.");
}
if (typeofStart == "undefined" || typeofEnd == "undefined") {
throw TypeError("Must pass start and end arguments.");
} else if (typeofStart != typeofEnd) {
throw TypeError("Start and end arguments must be of same type.");
}
typeof step == "undefined" && (step = 1);
if (end < start) {
step = -step;
}
if (typeofStart == "number") {
while (step > 0 ? end >= start : end <= start) {
range.push(start);
start += step;
}
} else if (typeofStart == "string") {
if (start.length != 1 || end.length != 1) {
throw TypeError("Only strings with one character are supported.");
}
start = start.charCodeAt(0);
end = end.charCodeAt(0);
while (step > 0 ? end >= start : end <= start) {
range.push(String.fromCharCode(start));
start += step;
}
} else {
throw TypeError("Only string and number types are supported");
}
return range;
}
function crearTablero() {
var tablero = []
for (let i of range(0, 8)) {
tablero.push([0, 0, 0, 0, 0, 0, 0, 0, 0])
}
return tablero
}
function enCuadrilatero(numero, i, k, tablero) {
var cuadrilateros = {
"0": [tablero[0][0], tablero[0][1], tablero[0][2],
tablero[1][0], tablero[1][1], tablero[1][2],
tablero[2][0], tablero[2][1], tablero[2][2]],
"1": [tablero[0][3], tablero[0][4], tablero[0][5],
tablero[1][3], tablero[1][4], tablero[1][5],
tablero[2][3], tablero[2][4], tablero[2][5]],
"2": [tablero[0][6], tablero[0][7], tablero[0][8],
tablero[1][6], tablero[1][7], tablero[1][8],
tablero[2][6], tablero[2][7], tablero[2][8]],
"3": [tablero[3][0], tablero[3][1], tablero[3][2],
tablero[4][0], tablero[4][1], tablero[4][2],
tablero[5][0], tablero[5][1], tablero[5][2]],
"4": [tablero[3][3], tablero[3][4], tablero[3][5],
tablero[4][3], tablero[4][4], tablero[4][5],
tablero[5][3], tablero[5][4], tablero[5][5]],
"5": [tablero[3][6], tablero[3][7], tablero[3][8],
tablero[4][6], tablero[4][7], tablero[4][8],
tablero[5][6], tablero[5][7], tablero[5][8]],
"6": [tablero[6][0], tablero[6][1], tablero[6][2],
tablero[7][0], tablero[7][1], tablero[7][2],
tablero[8][0], tablero[8][1], tablero[8][2]],
"7": [tablero[6][3], tablero[6][4], tablero[6][5],
tablero[7][3], tablero[7][4], tablero[7][5],
tablero[8][3], tablero[8][4], tablero[8][5]],
"8": [tablero[6][6], tablero[6][7], tablero[6][8],
tablero[7][6], tablero[7][7], tablero[7][8],
tablero[8][6], tablero[8][7], tablero[8][8]],
}
if (0 <= i && i <= 2) {
if (0 <= k && k <= 2) {
if (cuadrilateros["0"].includes(numero)) {
return true
}
}
else if (3 <= k && k <= 5) {
if (cuadrilateros["1"].includes(numero)) {
return true
}
}
else {
if (cuadrilateros["2"].includes(numero)) {
return true
}
}
}
else if (3 <= i && i <= 5) {
if (0 <= k && k <= 2) {
if (cuadrilateros["3"].includes(numero)) {
return true
}
}
else if (3 <= k && k <= 5) {
if (cuadrilateros["4"].includes(numero)) {
return true
}
}
else {
if (cuadrilateros["5"].includes(numero)) {
return true
}
}
}
else {
if (0 <= k && k <= 2) {
if (cuadrilateros["6"].includes(numero)) {
return true
}
}
else if (3 <= k && k <= 5) {
if (cuadrilateros["7"].includes(numero)) {
return true
}
}
else {
if (cuadrilateros["8"].includes(numero)) {
return true
}
}
}
return false
}
function rellenarTablero(tablero) {
for (let i of range(0, 8)) {
for (let k of range(0, 8)) {
var numeros_a_utilizar = range(1, 9)
while (true) {
if (numeros_a_utilizar.length === 0) {
return false
}
var numero = numeros_a_utilizar[
Math.floor(Math.random()*numeros_a_utilizar.length)
]
if (tablero[i].includes(numero)) {
var index = numeros_a_utilizar.indexOf(numero)
numeros_a_utilizar.splice(index, 1)
continue
} else {
var flag = false
for (let j of range(0, 8)) {
if (numero === tablero[j][k]) {
var index = numeros_a_utilizar.indexOf(numero)
numeros_a_utilizar.splice(index, 1)
flag = true
break
}
}
if (flag) {
continue
}
if (enCuadrilatero(numero, i, k, tablero)) {
var index = numeros_a_utilizar.indexOf(numero)
numeros_a_utilizar.splice(index, 1)
continue
}
var index = numeros_a_utilizar.indexOf(numero)
numeros_a_utilizar.splice(index, 1)
tablero[i][k] = numero
break
}
}
}
}
return tablero
}
export function nuevoTablero() {
while (true) {
var tablero = rellenarTablero(crearTablero())
if (tablero) {
return tablero
}
}
}
/* function sumarValores(arr) {
var suma = 0
for (let valor of arr) {
suma += valor
}
return suma
}
var tiempos = []
start = +new Date();
for (let i of range(1, 1000)) {
start2 = +new Date();
nuevoTablero()
console.log(i)
end2 = +new Date();
tiempos.push(end2 - start2)
}
end = +new Date();
console.log("generado en:", (end - start)/1000, "s")
console.log("tiempo promedio por iteración:", (sumarValores(tiempos)/tiempos.length)/1000, "s")
console.log("Tiempo mínimo de rellenado:", Math.min(...tiempos)/1000, "s")
console.log("Tiempo máximo de rellenado:", Math.max(...tiempos)/1000, "s") */