Gather data

This commit is contained in:
mike
2026-01-06 19:32:32 +01:00
parent 73f06a2b13
commit 07b629c7a0
2 changed files with 24 additions and 33 deletions

View File

@@ -14,13 +14,9 @@ import static puzzle.SwedishGenerator.*;
*/
public final class ExportFormat {
private ExportFormat() { }
private static boolean isLetter(char ch) { return ch >= 'A' && ch <= 'Z'; }
private static boolean inBounds(int H, int W, int r, int c) {
return r >= 0 && r < H && c >= 0 && c < W;
}
private ExportFormat() { }
static final String HORIZONTAL = "h", VERTICAL = "v";
private static boolean inBounds(int H, int W, int r, int c) { return r >= 0 && r < H && c >= 0 && c < W; }
// ---------- Public API ----------
@@ -92,8 +88,7 @@ public final class ExportFormat {
for (var r = minR; r <= maxR; r++) {
var row = new StringBuilder(Math.max(0, maxC - minC + 1));
for (var c = minC; c <= maxC; c++) {
var ch = letterAt.get(pack(r, c));
row.append(ch != null ? ch : '#');
row.append(letterAt.getOrDefault(pack(r, c), '#'));
}
gridv2.add(row.toString());
}
@@ -118,7 +113,7 @@ public final class ExportFormat {
return new ExportedPuzzle(gridv2, wordsOut, difficulty, rewards);
}
static final String HORIZONTAL = "h", VERTICAL = "v";
/**
* Convert a generator Slot + assigned word into a Placed object for export.
*/
@@ -196,7 +191,8 @@ public final class ExportFormat {
public record Rewards(int coins, int stars, int hints) { }
/// @param direction "h" | "v"
public record WordOut(Lemma lemma,String word, String[] clue, int startRow, int startCol, String direction, String answer, int arrowRow, int arrowCol, boolean isReversed, int complex) { }
public record WordOut(Lemma lemma, String word, String[] clue, int startRow, int startCol, String direction, String answer, int arrowRow, int arrowCol, boolean isReversed,
int complex) { }
public record ExportedPuzzle(List<String> gridv2, List<WordOut> words, int difficulty, Rewards rewards) { }
}

View File

@@ -16,7 +16,8 @@ import java.util.function.Predicate;
*/
@SuppressWarnings("ALL")
public record SwedishGenerator(int W, int H, int SIZE, int MAX_LEN) {
public static final char C_DASH = '#';
static final byte _1 = 49, _9 = 57, A = 65, Z = 90, DASH = (byte) C_DASH;
record nbrs_8(int x, int y) { }
class Data {
@@ -25,7 +26,7 @@ public record SwedishGenerator(int W, int H, int SIZE, int MAX_LEN) {
}
public SwedishGenerator {
Data.EXAMPLE = new byte[SIZE];
Arrays.fill(Data.EXAMPLE, (byte) '#');
Arrays.fill(Data.EXAMPLE, DASH);
}
public SwedishGenerator(int W, int H) { this(W, H, W * H, Math.min(W, H)); }
public SwedishGenerator() { this(9, 8); }
@@ -108,21 +109,15 @@ public record SwedishGenerator(int W, int H, int SIZE, int MAX_LEN) {
record Grid(byte[] g, int H, int W) {
Grid deepCopyGrid() { return new Grid(g.clone(), H, W); }
int getOffset(int r, int c) { return r * W + c; }
private int getOffset(int r, int c) { return r * W + c; }
boolean isLettercell(int r, int c) { return !isDigitAt(r, c); }
char getCharAt(int r, int c) { return (char) (g[getOffset(r, c)] & 0xFF); }
byte byteAt(int r, int c) { return g[getOffset(r, c)]; }
void setCharAt(int r, int c, char ch) { g[getOffset(r, c)] = (byte) ch; }
static final byte _1 = 49, _9 = 57, A = 65, Z = 90;
boolean isLetterAt(int r, int c) {
byte ch = g[getOffset(r, c)];
return ch >= A && ch <= Z;
}
boolean isDigitAt(int r, int c) {
byte ch = g[getOffset(r, c)];
return ch >= _1 && ch <= _9;
}
boolean isLetterAt(int r, int c) { return ((g[getOffset(r, c)] & 64) != 0); }
boolean isDigitAt(int r, int c) { return (g[getOffset(r, c)] & 48) == 48; }
}
Grid makeEmptyGrid() { return new Grid(Data.EXAMPLE.clone(), H, W); }
@@ -312,8 +307,8 @@ public record SwedishGenerator(int W, int H, int SIZE, int MAX_LEN) {
var slots = new ArrayList<Slot>();
for (var r = 0; r < H; r++) {
for (var c = 0; c < W; c++) {
var d = grid.getCharAt(r, c);
if (!isDigit(d)) continue;
if (!grid.isDigitAt(r, c)) continue;
var d = grid.getCharAt(r, c);
var dir = d - '0';
int or = OFFSETS[dir].x, oc = OFFSETS[dir].y;
int dr = STEPS[dir].x, dc = STEPS[dir].y;
@@ -461,7 +456,7 @@ public record SwedishGenerator(int W, int H, int SIZE, int MAX_LEN) {
var d = (char) ('0' + rng.randint(1, c == 0 ? CLUE_SIZE : 4));
g.setCharAt(r, c, d);
if (!hasRoomForClue(g, r, c, d)) {
g.setCharAt(r, c, '#');
g.setCharAt(r, c, C_DASH);
continue;
}
placed++;
@@ -480,11 +475,11 @@ public record SwedishGenerator(int W, int H, int SIZE, int MAX_LEN) {
var cc = clamp(cy + (rng.randint(-2, 2) + rng.randint(-2, 2)), 0, W - 1);
if (g.isDigitAt(rr, cc)) {
g.setCharAt(rr, cc, '#');
g.setCharAt(rr, cc, C_DASH);
} else {
var d = (char) ('0' + rng.randint(1, cc == 0 ? CLUE_SIZE : 4));
g.setCharAt(rr, cc, d);
if (!hasRoomForClue(g, rr, cc, d)) g.setCharAt(rr, cc, '#');
if (!hasRoomForClue(g, rr, cc, d)) g.setCharAt(rr, cc, C_DASH);
}
}
return g;
@@ -508,7 +503,7 @@ public record SwedishGenerator(int W, int H, int SIZE, int MAX_LEN) {
for (var r = 0; r < H; r++)
for (var c = 0; c < W; c++) {
var ch = out.getCharAt(r, c);
if (isDigit(ch) && !hasRoomForClue(out, r, c, ch)) out.setCharAt(r, c, '#');
if (isDigit(ch) && !hasRoomForClue(out, r, c, ch)) out.setCharAt(r, c, C_DASH);
}
return out;
}
@@ -534,7 +529,7 @@ public record SwedishGenerator(int W, int H, int SIZE, int MAX_LEN) {
double similarity(Grid a, Grid b) {
var same = 0;
for (var r = 0; r < H; r++) for (var c = 0; c < W; c++) if (a.getCharAt(r, c) == b.getCharAt(r, c)) same++;
for (var r = 0; r < H; r++) for (var c = 0; c < W; c++) if (a.byteAt(r, c) == b.byteAt(r, c)) same++;
return same / (double) (W * H);
}
@@ -642,10 +637,10 @@ public record SwedishGenerator(int W, int H, int SIZE, int MAX_LEN) {
int r = s.rs[i], c = s.cs[i];
char cur = grid.getCharAt(r, c);
var ch = w.charAt(i);
if (cur == '#') {
if (cur == C_DASH) {
urs[n] = r;
ucs[n] = c;
up[n] = '#';
up[n] = C_DASH;
n++;
grid.setCharAt(r, c, ch);
} else {