You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

p5.js开发One Tough Puzzle求解器:控制台仅输出4个数字而非8个的问题求助

Troubleshooting Your One Tough Puzzle Solver

Hey there! Let’s break down why your solver is only spitting out 4 numbers and throwing that "not enough numbers" message. I’ve combed through your p5.js code and found a handful of key issues that are derailing the logic:

1. Invalid Break Syntax is Crashing Your Loops

First off, this line is causing a silent syntax error that cuts your loops short:

break i=h;

In JavaScript, break can only exit the current loop (or a labeled loop), and you can’t assign a variable in the same statement like this. When this line runs, it throws an error that might not pop up in your main console (check your browser’s dev tools!), which stops loop execution early—hence only 4 numbers getting logged.

Fix this by separating the assignment and break, and use loop labels if you need to jump out of an outer loop:

outerLoop: for(let i = 0; i < pieces.length; i++){
  for (let h = 0; h < pieces.length; h++){
    // ... your matching logic ...
    if(/* valid match found */){
      console.log(h);
      i = h; // Assign first
      break outerLoop; // Then exit the labeled outer loop
    }
  }
}

2. Buggy Rotation Logic (shiftL Function)

Your shiftL function isn’t correctly rotating puzzle pieces. Right now, it takes the first element and inserts it at the second-to-last position instead of the end. This means your pieces aren’t rotating as intended, so the edge-matching logic (pieces.at(i).at(1) == -pieces.at(h).at(3)) can’t find valid pairs consistently.

Fix the rotation with a simpler, correct loop left shift:

function shiftL(array) {
  // Take the first element and move it to the end of the array
  array.push(array.shift());
}

3. Loop Index Chaos is Skipping Pieces

Your logic for skipping the same piece (if(h==i){ i++; }) is messing with the outer loop’s index. Incrementing i inside the inner loop causes you to skip over pieces entirely, so you’re not checking all possible pairs. Instead, use continue to skip the current h iteration when it matches i:

for (let h = 0; h < pieces.length; h++){
  if(h === i) continue; // Skip the same piece without breaking the outer loop
  // ... rest of your matching code ...
}

Also, your loops only run up to i<8 and h<8, but you have 9 pieces (s1 to s9). This means you’re never checking the 9th piece, which contributes to the "not enough numbers" issue. Use pieces.length instead of hardcoding 8 to cover all pieces.

4. Unscoped Variables Are Causing Hidden Issues

You’re not declaring variables like i, h, and j with let or const, so they’re global variables. This can lead to unexpected behavior when loops overlap or run multiple times. Always declare loop variables locally:

for(let i=0;i<pieces.length;i++){
  for (let h=0;h<pieces.length;h++){
    for(let j=0;j<4;j++){
      // ... logic ...
    }
  }
}

Quick Fix Example

Here’s a trimmed-down version of your setup function with these fixes applied:

function setup() {
  createCanvas(400, 400);
  outerLoop: for(let i = 0; i < pieces.length; i++){
    for (let h = 0; h < pieces.length; h++){
      if(h === i) continue;
      for(let j = 0; j < 4; j++){
        if(pieces[i][1] === -pieces[h][3]){
          console.log(h);
          correct.push(pieces[h]);
          break outerLoop; // Adjust based on whether you need one match or all
        } else {
          shiftL(pieces[h]);
        }
      }
    }
  }
  // Add a check here to log match count
  console.log(`Found ${correct.length} matches`);
  if(correct.length < 8){
    console.log("not enough numbers");
  }
}

Give these changes a try—they should fix the early loop termination and incorrect rotation, allowing your solver to find more valid piece matches.

内容的提问来源于stack exchange,提问作者Zackary Reavis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 11:52:30