feat: allow flexible clue placement in all four directions for puzzles

Co-authored-by: aider (gpt-4o) <aider@aider.chat>
This commit is contained in:
mike
2025-12-26 19:54:24 +01:00
parent 541e101ae0
commit dd8a6bffa3

View File

@@ -325,30 +325,32 @@ public class SwedishGenerator {
for (var c = 0; c < W; c++) {
var d = grid[r][c];
if (!isDigit(d)) continue;
var di = d - '0';
int dr = DIRS[di][0], dc = DIRS[di][1];
int rr = r + dr, cc = c + dc;
if (rr < 0 || rr >= H || cc < 0 || cc >= W) continue;
if (!isLetterCell(grid[rr][cc])) continue;
var rs = new int[MAX_LEN + 1]; // allow MAX_LEN+1 like JS loop
var cs = new int[MAX_LEN + 1];
var n = 0;
while (rr >= 0 && rr < H && cc >= 0 && cc < W) {
var ch = grid[rr][cc];
if (!isLetterCell(ch)) break;
rs[n] = rr;
cs[n] = cc;
n++;
rr += dr;
cc += dc;
if (n > MAX_LEN) break; // allow n==MAX_LEN+1
// Check all four possible directions for clue placement
for (int dir = 1; dir <= 4; dir++) {
int dr = DIRS[dir][0], dc = DIRS[dir][1];
int rr = r + dr, cc = c + dc;
if (rr < 0 || rr >= H || cc < 0 || cc >= W) continue;
if (!isLetterCell(grid[rr][cc])) continue;
var rs = new int[MAX_LEN + 1]; // allow MAX_LEN+1 like JS loop
var cs = new int[MAX_LEN + 1];
var n = 0;
while (rr >= 0 && rr < H && cc >= 0 && cc < W) {
var ch = grid[rr][cc];
if (!isLetterCell(ch)) break;
rs[n] = rr;
cs[n] = cc;
n++;
rr += dr;
cc += dc;
if (n > MAX_LEN) break; // allow n==MAX_LEN+1
}
slots.add(new Slot(r, c, (char) ('0' + dir), Arrays.copyOf(rs, n), Arrays.copyOf(cs, n)));
}
slots.add(new Slot(r, c, d, Arrays.copyOf(rs, n), Arrays.copyOf(cs, n)));
}
}
return slots;