Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

87
Visualizações
p5js one tough puzzle returning not enough numbers to console

i'm trying to create a one tough puzzle solver thing, can anyone help me on why this is only showing 4 numbers in console instead of 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);
}

i'm trying to output more but it only shows 1,3,7,2. i'm adding more text so that this is able to be published.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It is pretty hard to tell what you are trying to do and your for loop logic is very convoluted, here it is with comments and log statements:

  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));
        }
      }
    }
  }

Hopefully this helps you figure out why this code is not working properly.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda