Estoy tratando de crear un solucionador de acertijos difícil, ¿alguien puede ayudarme sobre por qué esto solo muestra 4 números en la consola en lugar de 8?
s1 = [1,-4,-1,2]; s2 = [2,-4,-3,4]; s3 = [4,-1,-3,3]; s4 = [3,-3,-4,4]; s5 = [2,-4,-3,2]; s6 = [4,-2,-4,3]; s7 = [1,-2,-4,2]; s8 = [1,-1,-4,4]; s9 = [1,-3,-3,4]; pieces = [s1,s2,s3,s4,s5,s6,s7,s8,s9]; correct =[]; pog = 0; function setup() { createCanvas(400, 400); for(i=0;i<8;i++){ for (h=1;h<8;h++){ if(h==i){ i++; } for(j=0;j<4;j++){ if(pieces.at(i).at(1) == -pieces.at(h).at(3)){ console.log(h); break i=h; } else { shiftL(pieces.at(h)); } } } } } //BTW the piece above another piece is 3 pieces away!!!! function draw() { background(220); frameRate(1); } function shiftL(array) { let sV = array.at(0); array.splice(0,1); array.splice(array.length-1,0,sV); }Estoy tratando de generar más, pero solo muestra 1,3,7,2. Estoy agregando más texto para que esto pueda ser publicado.
Es bastante difícil decir lo que está tratando de hacer y su lógica de bucle for es muy complicada, aquí está con comentarios y declaraciones de registro:
for (i = 0; i < 8; i++) { console.log(`begin i = ${i}`); for (h = 1; h < 8; h++) { console.log(`begin h = ${h}`); // When i == 0 this will be false if (h == i) { // when i == 1 and h == 1, this increments i to 2 // on the next pass through the for loop that increments h, // i == 2 and h == 2, this increments i to 3 // so on, for each iteration of the inner loop, i gets incremented i++; console.log(`incremented i to ${i}`); } // this loop should happen just 14 times, 7 times for i = 0 and then 7 // times starting with i = 1 for (j = 0; j < 4; j++) { if (pieces.at(i).at(1) == -pieces.at(h).at(3)) { console.log(h); // exits the loop for j break; // unreachable, never happens console.log("never happens"); i = h; } else { console.log(`shiftL(piecese.at(${h}))`); shiftL(pieces.at(h)); } } } }Esperemos que esto lo ayude a descubrir por qué este código no funciona correctamente.