Files
puzzle-generator/src/main/java/puzzle/ExportFormat.java
2026-01-09 03:41:51 +01:00

221 lines
8.0 KiB
Java

package puzzle;
import lombok.experimental.Delegate;
import puzzle.Main.PuzzleResult;
import puzzle.SwedishGenerator.Grid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import static puzzle.SwedishGenerator.R;
import static puzzle.SwedishGenerator.Lemma;
import static puzzle.SwedishGenerator.SIZE;
import static puzzle.SwedishGenerator.Slot;
import static puzzle.SwedishGenerator.C;
/**
* ExportFormat.java
*
* Direct port of export_format.js:
* - scans filled grid for clue digits '1'..'4'
* - extracts placed words in canonical direction (horizontal=right, vertical=down)
* - crops to bounding box (words + arrow cells) with 1-cell margin
* - outputs gridv2 + words[] (+ difficulty, rewards)
*/
public record ExportFormat() {
record Gridded(@Delegate Grid grid) {
String gridToString() {
var sb = new StringBuilder();
for (var r = 0; r < R; r++) {
if (r > 0) sb.append('\n');
for (var c = 0; c < C; c++) sb.append((char) grid.byteAt(r, c));
}
return sb.toString();
}
public String renderHuman() {
var sb = new StringBuilder();
for (var r = 0; r < R; r++) {
if (r > 0) sb.append('\n');
for (var c = 0; c < C; c++) {
sb.append(grid.isDigitAt(r, c) ? ' ' : (char) grid.byteAt(r, c));
}
}
return sb.toString();
}
}
record Bit(long[] bits) {
public Bit() { this(new long[(SIZE >> 6) + 1]); }
static int wordIndex(int bitIndex) { return bitIndex >> 6; }
public boolean get(int bitIndex) { return (this.bits[wordIndex(bitIndex)] & 1L << bitIndex) != 0L; }
public void set(int bitIndex) { bits[wordIndex(bitIndex)] |= 1L << bitIndex; }
public void clear(int bitIndex) { this.bits[wordIndex(bitIndex)] &= ~(1L << bitIndex); }
public void clear() { Arrays.fill(bits, 0L); }
}
record Bit1029(long[] bits) {
public Bit1029() { this(new long[1029]); }
static int wordIndex(int bitIndex) { return bitIndex >> 6; }
public boolean get(int bitIndex) { return (this.bits[wordIndex(bitIndex)] & 1L << bitIndex) != 0L; }
public void set(int bitIndex) { bits[wordIndex(bitIndex)] |= 1L << bitIndex; }
public void clear(int bitIndex) { this.bits[wordIndex(bitIndex)] &= ~(1L << bitIndex); }
public void clear() { Arrays.fill(bits, 0L); }
}
static final String HORIZONTAL = "h", VERTICAL = "v";
private static boolean inBounds(int r, int c) { return r >= 0 && r < SwedishGenerator.R && c >= 0 && c < SwedishGenerator.C; }
public static ExportedPuzzle exportFormatFromFilled(PuzzleResult puz, int difficulty, Rewards rewards) {
Objects.requireNonNull(puz, "puz");
var g = puz.filled().grid();
var placed = new ArrayList<Placed>();
var clueMap = puz.filled().clueMap();
puz.swe().forEachSlot(g.grid(), (int key, long packedPos, int len) -> {
var word = clueMap.get(key);
if (word != null) {
var p = extractPlacedFromSlot(Slot.from(key, packedPos, len), word);
if (p != null) placed.add(p);
}
});
// If nothing placed: return full grid mapped to letters/# only
if (placed.isEmpty()) {
var gridv2 = new ArrayList<String>(R);
for (var r = 0; r < R; r++) {
var sb = new StringBuilder(C);
for (var c = 0; c < C; c++) {
sb.append(g.isLetterAt(r, c) ? (char) g.byteAt(r, c) : '#');
}
gridv2.add(sb.toString());
}
return new ExportedPuzzle(gridv2, new WordOut[0], difficulty, rewards);
}
// 2) bounding box around all word cells + arrow cells, with 1-cell margin
int minR = Integer.MAX_VALUE, minC = Integer.MAX_VALUE;
int maxR = Integer.MIN_VALUE, maxC = Integer.MIN_VALUE;
for (var rc : placed) {
for (var c : rc.cells) {
minR = Math.min(minR, Grid.r(c));
minC = Math.min(minC, Grid.c(c));
maxR = Math.max(maxR, Grid.r(c));
maxC = Math.max(maxC, Grid.c(c));
}
minR = Math.min(minR, rc.arrowRow);
minC = Math.min(minC, rc.arrowCol);
maxR = Math.max(maxR, rc.arrowRow);
maxC = Math.max(maxC, rc.arrowCol);
}
// 3) map of only used letter cells (everything else becomes '#')
var letterAt = new HashMap<Long, Character>();
for (var p : placed) {
for (var c : p.cells) {
int rr = Grid.r(c), cc = Grid.c(c);
if (inBounds(rr, cc) && g.isLetterAt(rr, cc)) {
letterAt.put(pack(rr, cc), (char) g.byteAt(rr, cc));
}
}
}
// 4) render gridv2 over cropped bounds (out-of-bounds become '#')
var gridv2 = new ArrayList<String>(Math.max(0, maxR - minR + 1));
for (var r = minR; r <= maxR; r++) {
var row = new StringBuilder(Math.max(0, maxC - minC + 1));
for (var c = minC; c <= maxC; c++) {
row.append(letterAt.getOrDefault(pack(r, c), '#'));
}
gridv2.add(row.toString());
}
// 5) words output with cropped coordinates
int MIN_R = minR, MIN_C = minC;
var wordsOut = placed.stream().map(p -> new WordOut(
p.lemma,
p.startRow - MIN_R,
p.startCol - MIN_C,
p.direction,
p.arrowRow - MIN_R,
p.arrowCol - MIN_C,
p.isReversed,
p.lemma.simpel()
)).toArray(WordOut[]::new);
return new ExportedPuzzle(gridv2, wordsOut, difficulty, rewards);
}
/**
* Convert a generator Slot + assigned word into a Placed object for export.
*/
private static Placed extractPlacedFromSlot(Slot s, Lemma lemma) {
int r = s.clueR();
int c = s.clueC();
int d = s.dir();
int[] cells = new int[s.len()];
for (int i = 0; i < s.len(); i++) {
cells[i] = s.pos(i);
}
// Canonicalize: always output right/down
int startRow, startCol, arrowRow, arrowCol;
String direction;
boolean isReversed = false;
startRow = Grid.r(cells[0]);
startCol = Grid.c(cells[0]);
if (d == 2) { // right -> horizontal
direction = HORIZONTAL;
arrowRow = r;
arrowCol = c;
} else if (d == 3 || d == 5) { // down or down-bent -> vertical
direction = VERTICAL;
arrowRow = r;
arrowCol = c;
} else if (d == 4) { // left -> horizontal (REVERSED)
direction = HORIZONTAL;
isReversed = true;
arrowRow = r;
arrowCol = c;
} else if (d == 1) { // up -> vertical (REVERSED)
direction = VERTICAL;
isReversed = true;
arrowRow = r;
arrowCol = c;
} else {
return null;
}
return new Placed(
lemma,
startRow,
startCol,
direction,
arrowRow,
arrowCol,
cells,
isReversed
);
}
private static long pack(int r, int c) { return (((long) r) << 32) ^ (c & 0xFFFFFFFFL); }
private record Placed(Lemma lemma, int startRow, int startCol, String direction, int arrowRow, int arrowCol, int[] cells, boolean isReversed) { }
public record Rewards(int coins, int stars, int hints) { }
public record WordOut(Lemma lemma, int startRow, int startCol, String direction, int arrowRow, int arrowCol, boolean isReversed, int complex) {
public String word() { return new String(lemma().word(), java.nio.charset.StandardCharsets.US_ASCII); }
public ArrayList<String> clue() { return lemma.clue(); }
}
public record ExportedPuzzle(List<String> gridv2, WordOut[] words, int difficulty, Rewards rewards) { }
}