diff --git a/src/main/java/puzzle/SwedishGenerator.java b/src/main/java/puzzle/SwedishGenerator.java index 9c07ddb..1198405 100644 --- a/src/main/java/puzzle/SwedishGenerator.java +++ b/src/main/java/puzzle/SwedishGenerator.java @@ -144,13 +144,11 @@ public record SwedishGenerator(Rng rng) { public Grid(byte[] g) { this(g, new long[2]); } static Grid createEmpty() { return new Grid(new byte[SIZE], new long[2]); } - int digitAt(int r, int c) { return g[offset(r, c)] - 48; } int digitAt(int index) { return g[index] - 48; } public static int r(int offset) { return offset & 7; } public static int c(int offset) { return offset >>> 3; } static int offset(int r, int c) { return r | (c << 3); } Grid deepCopyGrid() { return new Grid(g.clone(), bo.clone()); } - public byte byteAt(int r, int c) { return g[offset(r, c)]; } public byte byteAt(int pos) { return g[pos]; } void setByteAt(int idx, byte ch) { g[idx] = ch; } void setAt(int idx, byte ch) { @@ -168,9 +166,8 @@ public record SwedishGenerator(Rng rng) { if (idx < 64) bo[0] &= ~(1L << idx); else bo[1] &= ~(1L << (idx & 63)); } - static boolean isDigit(byte b) { return (b & 48) == 48; } - boolean isDigitAt(int r, int c) { return isDigit(g[offset(r, c)]); } - boolean isDigitAt(int index) { return isDigit(g[index]); } + static boolean isDigit(byte b) { return (b & 48) == 48; } + boolean isDigitAt(int index) { return isDigit(g[index]); } boolean isClue(int index) { if (index < 64) { return (bo[0] >> index & 1L) != 0; @@ -196,12 +193,10 @@ public record SwedishGenerator(Rng rng) { return false; } } - static boolean isLetter(byte b) { return (b & 64) != 0; } - public boolean isLetterSet(int r, int c) { return isLetter(g[offset(r, c)]); } - public boolean isLetterSet(int idx) { return isLetter(g[idx]); } - static boolean notDigit(byte b) { return (b & 48) != 48; } - public boolean isLetterAt(int r, int c) { return notDigit(g[offset(r, c)]); } - public boolean isLetterAt(int index) { return notDigit(g[index]); } + static boolean isLetter(byte b) { return (b & 64) != 0; } + public boolean isLetterSet(int idx) { return isLetter(g[idx]); } + static boolean notDigit(byte b) { return (b & 48) != 48; } + public boolean isLetterAt(int index) { return notDigit(g[index]); } public double similarity(Grid b) { var same = 0; @@ -328,10 +323,10 @@ public record SwedishGenerator(Rng rng) { } static int intersectSorted(int[] a, int aLen, int[] b, int bLen, int[] out) { - int i = 0, j = 0, k = 0; + int i = 0, j = 0, k = 0, x, y; while (i < aLen && j < bLen) { - int x = a[i]; - int y = b[j]; + x = a[i]; + y = b[j]; if (x == y) { out[k++] = x; i++; @@ -455,7 +450,7 @@ public record SwedishGenerator(Rng rng) { long packed = Neighbors9x8.NBR8_PACKED[Grid.offset(rr, cc)]; int n = (int) (packed >>> 56); for (int k = 0; k < n; k++) { - int nidx = (int)((packed >>> (k * 7)) & 0x7F); + int nidx = (int) ((packed >>> (k * 7)) & 0x7F); if (seen.get(nidx) || grid.isLetterAt(nidx)) continue; seen.set(nidx); diff --git a/src/test/java/puzzle/MainTest.java b/src/test/java/puzzle/MainTest.java index 0c8aadd..65c63c3 100644 --- a/src/test/java/puzzle/MainTest.java +++ b/src/test/java/puzzle/MainTest.java @@ -87,10 +87,10 @@ public class MainTest { Assertions.assertEquals(DASH, grid.byteAt(Grid.offset(1, 1))); // Test isLetterAt - Assertions.assertTrue(grid.isLetterSet(0, 0)); - Assertions.assertFalse(grid.isLetterSet(1, 2)); - Assertions.assertTrue(grid.isLetterSet(2, 3)); - Assertions.assertFalse(grid.isLetterSet(1, 1)); + Assertions.assertTrue(grid.isLetterSet(Grid.offset(0, 0))); + Assertions.assertFalse(grid.isLetterSet(Grid.offset(1, 2))); + Assertions.assertTrue(grid.isLetterSet(Grid.offset(2, 3))); + Assertions.assertFalse(grid.isLetterSet(Grid.offset(1, 1))); // Test isDigitAt Assertions.assertFalse(grid.isDigitAt(0)); @@ -100,9 +100,9 @@ public class MainTest { Assertions.assertFalse(grid.isDigitAt(Grid.offset(1, 1))); // Test isLettercell - Assertions.assertTrue(grid.isLetterAt(0, 0)); // 'A' is letter - Assertions.assertFalse(grid.isLetterAt(1, 2)); // '5' is digit - Assertions.assertTrue(grid.isLetterAt(1, 1)); // '#' is lettercell + Assertions.assertTrue(grid.isLetterAt(Grid.offset(0, 0))); // 'A' is letter + Assertions.assertFalse(grid.isLetterAt(Grid.offset(1, 2))); // '5' is digit + Assertions.assertTrue(grid.isLetterAt(Grid.offset(1, 1))); // '#' is lettercell } @Test diff --git a/src/test/java/puzzle/SwedishGeneratorTest.java b/src/test/java/puzzle/SwedishGeneratorTest.java index e166489..99ea5c1 100644 --- a/src/test/java/puzzle/SwedishGeneratorTest.java +++ b/src/test/java/puzzle/SwedishGeneratorTest.java @@ -73,20 +73,20 @@ public class SwedishGeneratorTest { grid.setByteAt(0, (byte) 'A'); grid.setClue(Grid.offset(0, 1), (byte) '1'); - assertEquals('A', grid.byteAt(0, 0)); - assertEquals(1, grid.digitAt(0, 1)); + assertEquals('A', grid.byteAt(0)); + assertEquals(1, grid.digitAt(Grid.offset(0, 1))); assertTrue(grid.isLetterAt(0)); assertFalse(grid.isDigitAt(0)); assertTrue(grid.isDigitAt(Grid.offset(0, 1))); assertFalse(grid.isLetterAt(Grid.offset(0, 1))); - assertTrue(grid.isLetterAt(0, 0)); - assertFalse(grid.isLetterAt(0, 1)); + assertTrue(grid.isLetterAt( 0)); + assertFalse(grid.isLetterAt(Grid.offset(0, 1))); var copy = grid.deepCopyGrid(); - assertEquals('A', copy.byteAt(0, 0)); + assertEquals('A', copy.byteAt(0)); copy.setByteAt(0, (byte) 'B'); - assertEquals('B', copy.byteAt(0, 0)); - assertEquals('A', grid.byteAt(0, 0)); + assertEquals('B', copy.byteAt(0)); + assertEquals('A', grid.byteAt(0)); } @Test @@ -279,9 +279,9 @@ public class SwedishGeneratorTest { // 1. Successful placement in empty grid assertTrue(placeWord(grid, s, w1, undoBuffer, 0)); - assertEquals('A', grid.byteAt(0, 0)); - assertEquals('B', grid.byteAt(0, 1)); - assertEquals('C', grid.byteAt(0, 2)); + assertEquals('A', grid.byteAt( 0)); + assertEquals('B', grid.byteAt(Grid.offset(0, 1))); + assertEquals('C', grid.byteAt(Grid.offset(0, 2))); assertEquals(0b111L, undoBuffer[0]); // 2. Successful placement with partial overlap (same characters) @@ -292,18 +292,18 @@ public class SwedishGeneratorTest { var w2 = new Lemma("ABD", 1, "conflict"); assertFalse(placeWord(grid, s, w2, undoBuffer, 2)); // Verify grid is unchanged (still "ABC") - assertEquals('A', grid.byteAt(0, 0)); - assertEquals('B', grid.byteAt(0, 1)); - assertEquals('C', grid.byteAt(0, 2)); + assertEquals('A', grid.byteAt(Grid.offset(0, 0))); + assertEquals('B', grid.byteAt(Grid.offset(0, 1))); + assertEquals('C', grid.byteAt(Grid.offset(0, 2))); // 4. Partial placement then conflict (rollback) grid = Grid.createEmpty(); grid.setByteAt(Grid.offset(0, 2), (byte) 'X'); // Conflict at the end assertFalse(placeWord(grid, s, w1, undoBuffer, 3)); // Verify grid is still empty (except for 'X') - assertEquals(DASH, grid.byteAt(0, 0)); - assertEquals(DASH, grid.byteAt(0, 1)); - assertEquals('X', grid.byteAt(0, 2)); + assertEquals(DASH, grid.byteAt(Grid.offset(0, 0))); + assertEquals(DASH, grid.byteAt(Grid.offset(0, 1))); + assertEquals('X', grid.byteAt(Grid.offset(0, 2))); } @Test @@ -317,12 +317,12 @@ public class SwedishGeneratorTest { var placed = placeWord(grid, s, w, undoBuffer, 0); assertTrue(placed); - assertEquals('A', grid.byteAt(0, 1)); - assertEquals('Z', grid.byteAt(0, 2)); + assertEquals('A', grid.byteAt(Grid.offset(0, 1))); + assertEquals('Z', grid.byteAt(Grid.offset(0, 2))); assertEquals(0b11L, undoBuffer[0]); s.undoPlace(grid, undoBuffer[0]); - assertEquals(DASH, grid.byteAt(0, 1)); - assertEquals(DASH, grid.byteAt(0, 2)); + assertEquals(DASH, grid.byteAt(Grid.offset(0, 1))); + assertEquals(DASH, grid.byteAt(Grid.offset(0, 2))); } } \ No newline at end of file