Gather data
This commit is contained in:
@@ -14,13 +14,9 @@ import static puzzle.SwedishGenerator.*;
|
|||||||
*/
|
*/
|
||||||
public final class ExportFormat {
|
public final class ExportFormat {
|
||||||
|
|
||||||
private ExportFormat() { }
|
private ExportFormat() { }
|
||||||
|
static final String HORIZONTAL = "h", VERTICAL = "v";
|
||||||
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 static boolean inBounds(int H, int W, int r, int c) {
|
|
||||||
return r >= 0 && r < H && c >= 0 && c < W;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------- Public API ----------
|
// ---------- Public API ----------
|
||||||
|
|
||||||
@@ -92,8 +88,7 @@ public final class ExportFormat {
|
|||||||
for (var r = minR; r <= maxR; r++) {
|
for (var r = minR; r <= maxR; r++) {
|
||||||
var row = new StringBuilder(Math.max(0, maxC - minC + 1));
|
var row = new StringBuilder(Math.max(0, maxC - minC + 1));
|
||||||
for (var c = minC; c <= maxC; c++) {
|
for (var c = minC; c <= maxC; c++) {
|
||||||
var ch = letterAt.get(pack(r, c));
|
row.append(letterAt.getOrDefault(pack(r, c), '#'));
|
||||||
row.append(ch != null ? ch : '#');
|
|
||||||
}
|
}
|
||||||
gridv2.add(row.toString());
|
gridv2.add(row.toString());
|
||||||
}
|
}
|
||||||
@@ -118,7 +113,7 @@ public final class ExportFormat {
|
|||||||
|
|
||||||
return new ExportedPuzzle(gridv2, wordsOut, difficulty, rewards);
|
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.
|
* 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) { }
|
public record Rewards(int coins, int stars, int hints) { }
|
||||||
|
|
||||||
/// @param direction "h" | "v"
|
/// @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) { }
|
public record ExportedPuzzle(List<String> gridv2, List<WordOut> words, int difficulty, Rewards rewards) { }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,8 @@ import java.util.function.Predicate;
|
|||||||
*/
|
*/
|
||||||
@SuppressWarnings("ALL")
|
@SuppressWarnings("ALL")
|
||||||
public record SwedishGenerator(int W, int H, int SIZE, int MAX_LEN) {
|
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) { }
|
record nbrs_8(int x, int y) { }
|
||||||
|
|
||||||
class Data {
|
class Data {
|
||||||
@@ -25,7 +26,7 @@ public record SwedishGenerator(int W, int H, int SIZE, int MAX_LEN) {
|
|||||||
}
|
}
|
||||||
public SwedishGenerator {
|
public SwedishGenerator {
|
||||||
Data.EXAMPLE = new byte[SIZE];
|
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(int W, int H) { this(W, H, W * H, Math.min(W, H)); }
|
||||||
public SwedishGenerator() { this(9, 8); }
|
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) {
|
record Grid(byte[] g, int H, int W) {
|
||||||
|
|
||||||
|
|
||||||
Grid deepCopyGrid() { return new Grid(g.clone(), H, 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); }
|
boolean isLettercell(int r, int c) { return !isDigitAt(r, c); }
|
||||||
char getCharAt(int r, int c) { return (char) (g[getOffset(r, c)] & 0xFF); }
|
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; }
|
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) { return ((g[getOffset(r, c)] & 64) != 0); }
|
||||||
boolean isLetterAt(int r, int c) {
|
boolean isDigitAt(int r, int c) { return (g[getOffset(r, c)] & 48) == 48; }
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Grid makeEmptyGrid() { return new Grid(Data.EXAMPLE.clone(), H, W); }
|
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>();
|
var slots = new ArrayList<Slot>();
|
||||||
for (var r = 0; r < H; r++) {
|
for (var r = 0; r < H; r++) {
|
||||||
for (var c = 0; c < W; c++) {
|
for (var c = 0; c < W; c++) {
|
||||||
var d = grid.getCharAt(r, c);
|
if (!grid.isDigitAt(r, c)) continue;
|
||||||
if (!isDigit(d)) continue;
|
var d = grid.getCharAt(r, c);
|
||||||
var dir = d - '0';
|
var dir = d - '0';
|
||||||
int or = OFFSETS[dir].x, oc = OFFSETS[dir].y;
|
int or = OFFSETS[dir].x, oc = OFFSETS[dir].y;
|
||||||
int dr = STEPS[dir].x, dc = STEPS[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));
|
var d = (char) ('0' + rng.randint(1, c == 0 ? CLUE_SIZE : 4));
|
||||||
g.setCharAt(r, c, d);
|
g.setCharAt(r, c, d);
|
||||||
if (!hasRoomForClue(g, r, c, d)) {
|
if (!hasRoomForClue(g, r, c, d)) {
|
||||||
g.setCharAt(r, c, '#');
|
g.setCharAt(r, c, C_DASH);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
placed++;
|
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);
|
var cc = clamp(cy + (rng.randint(-2, 2) + rng.randint(-2, 2)), 0, W - 1);
|
||||||
|
|
||||||
if (g.isDigitAt(rr, cc)) {
|
if (g.isDigitAt(rr, cc)) {
|
||||||
g.setCharAt(rr, cc, '#');
|
g.setCharAt(rr, cc, C_DASH);
|
||||||
} else {
|
} else {
|
||||||
var d = (char) ('0' + rng.randint(1, cc == 0 ? CLUE_SIZE : 4));
|
var d = (char) ('0' + rng.randint(1, cc == 0 ? CLUE_SIZE : 4));
|
||||||
g.setCharAt(rr, cc, d);
|
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;
|
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 r = 0; r < H; r++)
|
||||||
for (var c = 0; c < W; c++) {
|
for (var c = 0; c < W; c++) {
|
||||||
var ch = out.getCharAt(r, 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;
|
return out;
|
||||||
}
|
}
|
||||||
@@ -534,7 +529,7 @@ public record SwedishGenerator(int W, int H, int SIZE, int MAX_LEN) {
|
|||||||
|
|
||||||
double similarity(Grid a, Grid b) {
|
double similarity(Grid a, Grid b) {
|
||||||
var same = 0;
|
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);
|
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];
|
int r = s.rs[i], c = s.cs[i];
|
||||||
char cur = grid.getCharAt(r, c);
|
char cur = grid.getCharAt(r, c);
|
||||||
var ch = w.charAt(i);
|
var ch = w.charAt(i);
|
||||||
if (cur == '#') {
|
if (cur == C_DASH) {
|
||||||
urs[n] = r;
|
urs[n] = r;
|
||||||
ucs[n] = c;
|
ucs[n] = c;
|
||||||
up[n] = '#';
|
up[n] = C_DASH;
|
||||||
n++;
|
n++;
|
||||||
grid.setCharAt(r, c, ch);
|
grid.setCharAt(r, c, ch);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user