diff --git a/diuf/sudoku/Cell.java b/diuf/sudoku/Cell.java index 95640d9..70f72c1 100644 --- a/diuf/sudoku/Cell.java +++ b/diuf/sudoku/Cell.java @@ -83,7 +83,7 @@ public void setValueAndCancel(int value, Grid targetGrid) { assert value != 0; targetGrid.setCellValue(this.index, value); targetGrid.clearCellPotentialValues(this.index); - for(int i = 0; i < 20; i++) { + for(int i = 0; i < (Settings.getInstance().isBlocks() ? 20 : 16); i++) { targetGrid.removeCellPotentialValue(Grid.visibleCellIndex[this.index][i], value); } } diff --git a/diuf/sudoku/Grid.java b/diuf/sudoku/Grid.java index 39cddc1..322d1aa 100644 --- a/diuf/sudoku/Grid.java +++ b/diuf/sudoku/Grid.java @@ -76,15 +76,14 @@ public class Grid { private static final Cell cells[]; public static final int[][] regionCellIndex; public static final int[][] cellRegions; - public static final int[][] visibleCellIndex; - public static final int[][] forwardVisibleCellIndex; + public static int[][] visibleCellIndex; + public static int[][] forwardVisibleCellIndex; private static final Block[] blocks; private static final Row[] rows; private static final Column[] columns; public static final Region[][] regions; - public static final CellSet[] visibleCellsSet; - public static final CellSet[] forwardVisibleCellsSet; - + public static CellSet[] visibleCellsSet; + public static CellSet[] forwardVisibleCellsSet; static { cells = new Cell[] { new Cell(0), new Cell(1), new Cell(2), new Cell(3), new Cell(4), new Cell(5), new Cell(6), new Cell(7), new Cell(8), @@ -182,7 +181,7 @@ public class Grid { { 7,16,25,34,43,52,60,61,62,69,70,71,72,73,74,75,76,77,78,80}, { 8,17,26,35,44,53,60,61,62,69,70,71,72,73,74,75,76,77,78,79} }; - forwardVisibleCellIndex = new int [][] { + forwardVisibleCellIndex = new int [][] { {1,2,3,4,5,6,7,8,9,10,11,18,19,20,27,36,45,54,63,72}, {2,3,4,5,6,7,8,9,10,11,18,19,20,28,37,46,55,64,73}, {3,4,5,6,7,8,9,10,11,18,19,20,29,38,47,56,65,74}, @@ -265,7 +264,7 @@ public class Grid { {80}, {} }; - visibleCellsSet = new CellSet[] { + visibleCellsSet = new CellSet[] { new CellSet(visibleCellIndex[0]),new CellSet(visibleCellIndex[1]),new CellSet(visibleCellIndex[2]),new CellSet(visibleCellIndex[3]),new CellSet(visibleCellIndex[4]),new CellSet(visibleCellIndex[5]),new CellSet(visibleCellIndex[6]),new CellSet(visibleCellIndex[7]),new CellSet(visibleCellIndex[8]), new CellSet(visibleCellIndex[9]),new CellSet(visibleCellIndex[10]),new CellSet(visibleCellIndex[11]),new CellSet(visibleCellIndex[12]),new CellSet(visibleCellIndex[13]),new CellSet(visibleCellIndex[14]),new CellSet(visibleCellIndex[15]),new CellSet(visibleCellIndex[16]),new CellSet(visibleCellIndex[17]), new CellSet(visibleCellIndex[18]),new CellSet(visibleCellIndex[19]),new CellSet(visibleCellIndex[20]),new CellSet(visibleCellIndex[21]),new CellSet(visibleCellIndex[22]),new CellSet(visibleCellIndex[23]),new CellSet(visibleCellIndex[24]),new CellSet(visibleCellIndex[25]),new CellSet(visibleCellIndex[26]), @@ -370,14 +369,13 @@ public static Block getBlockAt(int x, int y) { public static Grid.Region getRegionAt(int regionTypeIndex, int cellIndex) { return Grid.regions[regionTypeIndex][Grid.cellRegions[cellIndex][regionTypeIndex]]; } - //=== Non-static methods ============== /** * Is the cell a given or not * @param index the cell index [0..80] */ - + public void setGiven(int index) { this.isGiven[index] = true; } @@ -390,6 +388,370 @@ public boolean isGiven(int index) { return this.isGiven[index]; } +//Change visible cells according to variant + public static void changeVisibleCells() { + if (Settings.getInstance().isBlocks()) + visibleCellIndex = new int[][] { + { 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,18,19,20,27,36,45,54,63,72}, + { 0, 2, 3, 4, 5, 6, 7, 8, 9,10,11,18,19,20,28,37,46,55,64,73}, + { 0, 1, 3, 4, 5, 6, 7, 8, 9,10,11,18,19,20,29,38,47,56,65,74}, + { 0, 1, 2, 4, 5, 6, 7, 8,12,13,14,21,22,23,30,39,48,57,66,75}, + { 0, 1, 2, 3, 5, 6, 7, 8,12,13,14,21,22,23,31,40,49,58,67,76}, + { 0, 1, 2, 3, 4, 6, 7, 8,12,13,14,21,22,23,32,41,50,59,68,77}, + { 0, 1, 2, 3, 4, 5, 7, 8,15,16,17,24,25,26,33,42,51,60,69,78}, + { 0, 1, 2, 3, 4, 5, 6, 8,15,16,17,24,25,26,34,43,52,61,70,79}, + { 0, 1, 2, 3, 4, 5, 6, 7,15,16,17,24,25,26,35,44,53,62,71,80}, + { 0, 1, 2,10,11,12,13,14,15,16,17,18,19,20,27,36,45,54,63,72}, + { 0, 1, 2, 9,11,12,13,14,15,16,17,18,19,20,28,37,46,55,64,73}, + { 0, 1, 2, 9,10,12,13,14,15,16,17,18,19,20,29,38,47,56,65,74}, + { 3, 4, 5, 9,10,11,13,14,15,16,17,21,22,23,30,39,48,57,66,75}, + { 3, 4, 5, 9,10,11,12,14,15,16,17,21,22,23,31,40,49,58,67,76}, + { 3, 4, 5, 9,10,11,12,13,15,16,17,21,22,23,32,41,50,59,68,77}, + { 6, 7, 8, 9,10,11,12,13,14,16,17,24,25,26,33,42,51,60,69,78}, + { 6, 7, 8, 9,10,11,12,13,14,15,17,24,25,26,34,43,52,61,70,79}, + { 6, 7, 8, 9,10,11,12,13,14,15,16,24,25,26,35,44,53,62,71,80}, + { 0, 1, 2, 9,10,11,19,20,21,22,23,24,25,26,27,36,45,54,63,72}, + { 0, 1, 2, 9,10,11,18,20,21,22,23,24,25,26,28,37,46,55,64,73}, + { 0, 1, 2, 9,10,11,18,19,21,22,23,24,25,26,29,38,47,56,65,74}, + { 3, 4, 5,12,13,14,18,19,20,22,23,24,25,26,30,39,48,57,66,75}, + { 3, 4, 5,12,13,14,18,19,20,21,23,24,25,26,31,40,49,58,67,76}, + { 3, 4, 5,12,13,14,18,19,20,21,22,24,25,26,32,41,50,59,68,77}, + { 6, 7, 8,15,16,17,18,19,20,21,22,23,25,26,33,42,51,60,69,78}, + { 6, 7, 8,15,16,17,18,19,20,21,22,23,24,26,34,43,52,61,70,79}, + { 6, 7, 8,15,16,17,18,19,20,21,22,23,24,25,35,44,53,62,71,80}, + { 0, 9,18,28,29,30,31,32,33,34,35,36,37,38,45,46,47,54,63,72}, + { 1,10,19,27,29,30,31,32,33,34,35,36,37,38,45,46,47,55,64,73}, + { 2,11,20,27,28,30,31,32,33,34,35,36,37,38,45,46,47,56,65,74}, + { 3,12,21,27,28,29,31,32,33,34,35,39,40,41,48,49,50,57,66,75}, + { 4,13,22,27,28,29,30,32,33,34,35,39,40,41,48,49,50,58,67,76}, + { 5,14,23,27,28,29,30,31,33,34,35,39,40,41,48,49,50,59,68,77}, + { 6,15,24,27,28,29,30,31,32,34,35,42,43,44,51,52,53,60,69,78}, + { 7,16,25,27,28,29,30,31,32,33,35,42,43,44,51,52,53,61,70,79}, + { 8,17,26,27,28,29,30,31,32,33,34,42,43,44,51,52,53,62,71,80}, + { 0, 9,18,27,28,29,37,38,39,40,41,42,43,44,45,46,47,54,63,72}, + { 1,10,19,27,28,29,36,38,39,40,41,42,43,44,45,46,47,55,64,73}, + { 2,11,20,27,28,29,36,37,39,40,41,42,43,44,45,46,47,56,65,74}, + { 3,12,21,30,31,32,36,37,38,40,41,42,43,44,48,49,50,57,66,75}, + { 4,13,22,30,31,32,36,37,38,39,41,42,43,44,48,49,50,58,67,76}, + { 5,14,23,30,31,32,36,37,38,39,40,42,43,44,48,49,50,59,68,77}, + { 6,15,24,33,34,35,36,37,38,39,40,41,43,44,51,52,53,60,69,78}, + { 7,16,25,33,34,35,36,37,38,39,40,41,42,44,51,52,53,61,70,79}, + { 8,17,26,33,34,35,36,37,38,39,40,41,42,43,51,52,53,62,71,80}, + { 0, 9,18,27,28,29,36,37,38,46,47,48,49,50,51,52,53,54,63,72}, + { 1,10,19,27,28,29,36,37,38,45,47,48,49,50,51,52,53,55,64,73}, + { 2,11,20,27,28,29,36,37,38,45,46,48,49,50,51,52,53,56,65,74}, + { 3,12,21,30,31,32,39,40,41,45,46,47,49,50,51,52,53,57,66,75}, + { 4,13,22,30,31,32,39,40,41,45,46,47,48,50,51,52,53,58,67,76}, + { 5,14,23,30,31,32,39,40,41,45,46,47,48,49,51,52,53,59,68,77}, + { 6,15,24,33,34,35,42,43,44,45,46,47,48,49,50,52,53,60,69,78}, + { 7,16,25,33,34,35,42,43,44,45,46,47,48,49,50,51,53,61,70,79}, + { 8,17,26,33,34,35,42,43,44,45,46,47,48,49,50,51,52,62,71,80}, + { 0, 9,18,27,36,45,55,56,57,58,59,60,61,62,63,64,65,72,73,74}, + { 1,10,19,28,37,46,54,56,57,58,59,60,61,62,63,64,65,72,73,74}, + { 2,11,20,29,38,47,54,55,57,58,59,60,61,62,63,64,65,72,73,74}, + { 3,12,21,30,39,48,54,55,56,58,59,60,61,62,66,67,68,75,76,77}, + { 4,13,22,31,40,49,54,55,56,57,59,60,61,62,66,67,68,75,76,77}, + { 5,14,23,32,41,50,54,55,56,57,58,60,61,62,66,67,68,75,76,77}, + { 6,15,24,33,42,51,54,55,56,57,58,59,61,62,69,70,71,78,79,80}, + { 7,16,25,34,43,52,54,55,56,57,58,59,60,62,69,70,71,78,79,80}, + { 8,17,26,35,44,53,54,55,56,57,58,59,60,61,69,70,71,78,79,80}, + { 0, 9,18,27,36,45,54,55,56,64,65,66,67,68,69,70,71,72,73,74}, + { 1,10,19,28,37,46,54,55,56,63,65,66,67,68,69,70,71,72,73,74}, + { 2,11,20,29,38,47,54,55,56,63,64,66,67,68,69,70,71,72,73,74}, + { 3,12,21,30,39,48,57,58,59,63,64,65,67,68,69,70,71,75,76,77}, + { 4,13,22,31,40,49,57,58,59,63,64,65,66,68,69,70,71,75,76,77}, + { 5,14,23,32,41,50,57,58,59,63,64,65,66,67,69,70,71,75,76,77}, + { 6,15,24,33,42,51,60,61,62,63,64,65,66,67,68,70,71,78,79,80}, + { 7,16,25,34,43,52,60,61,62,63,64,65,66,67,68,69,71,78,79,80}, + { 8,17,26,35,44,53,60,61,62,63,64,65,66,67,68,69,70,78,79,80}, + { 0, 9,18,27,36,45,54,55,56,63,64,65,73,74,75,76,77,78,79,80}, + { 1,10,19,28,37,46,54,55,56,63,64,65,72,74,75,76,77,78,79,80}, + { 2,11,20,29,38,47,54,55,56,63,64,65,72,73,75,76,77,78,79,80}, + { 3,12,21,30,39,48,57,58,59,66,67,68,72,73,74,76,77,78,79,80}, + { 4,13,22,31,40,49,57,58,59,66,67,68,72,73,74,75,77,78,79,80}, + { 5,14,23,32,41,50,57,58,59,66,67,68,72,73,74,75,76,78,79,80}, + { 6,15,24,33,42,51,60,61,62,69,70,71,72,73,74,75,76,77,79,80}, + { 7,16,25,34,43,52,60,61,62,69,70,71,72,73,74,75,76,77,78,80}, + { 8,17,26,35,44,53,60,61,62,69,70,71,72,73,74,75,76,77,78,79} + }; + else + visibleCellIndex = new int[][] { + { 1, 2, 3, 4, 5, 6, 7, 8, 9,18,27,36,45,54,63,72}, + { 0, 2, 3, 4, 5, 6, 7, 8,10,19,28,37,46,55,64,73}, + { 0, 1, 3, 4, 5, 6, 7, 8,11,20,29,38,47,56,65,74}, + { 0, 1, 2, 4, 5, 6, 7, 8,12,21,30,39,48,57,66,75}, + { 0, 1, 2, 3, 5, 6, 7, 8,13,22,31,40,49,58,67,76}, + { 0, 1, 2, 3, 4, 6, 7, 8,14,23,32,41,50,59,68,77}, + { 0, 1, 2, 3, 4, 5, 7, 8,15,24,33,42,51,60,69,78}, + { 0, 1, 2, 3, 4, 5, 6, 8,16,25,34,43,52,61,70,79}, + { 0, 1, 2, 3, 4, 5, 6, 7,17,26,35,44,53,62,71,80}, + { 0,10,11,12,13,14,15,16,17,18,27,36,45,54,63,72}, + { 1, 9,11,12,13,14,15,16,17,19,28,37,46,55,64,73}, + { 2, 9,10,12,13,14,15,16,17,20,29,38,47,56,65,74}, + { 3, 9,10,11,13,14,15,16,17,21,30,39,48,57,66,75}, + { 4, 9,10,11,12,14,15,16,17,22,31,40,49,58,67,76}, + { 5, 9,10,11,12,13,15,16,17,23,32,41,50,59,68,77}, + { 6, 9,10,11,12,13,14,16,17,24,33,42,51,60,69,78}, + { 7, 9,10,11,12,13,14,15,17,25,34,43,52,61,70,79}, + { 8, 9,10,11,12,13,14,15,16,26,35,44,53,62,71,80}, + { 0, 9,19,20,21,22,23,24,25,26,27,36,45,54,63,72}, + { 1,10,18,20,21,22,23,24,25,26,28,37,46,55,64,73}, + { 2,11,18,19,21,22,23,24,25,26,29,38,47,56,65,74}, + { 3,12,18,19,20,22,23,24,25,26,30,39,48,57,66,75}, + { 4,13,18,19,20,21,23,24,25,26,31,40,49,58,67,76}, + { 5,14,18,19,20,21,22,24,25,26,32,41,50,59,68,77}, + { 6,15,18,19,20,21,22,23,25,26,33,42,51,60,69,78}, + { 7,16,18,19,20,21,22,23,24,26,34,43,52,61,70,79}, + { 8,17,18,19,20,21,22,23,24,25,35,44,53,62,71,80}, + { 0, 9,18,28,29,30,31,32,33,34,35,36,45,54,63,72}, + { 1,10,19,27,29,30,31,32,33,34,35,37,46,55,64,73}, + { 2,11,20,27,28,30,31,32,33,34,35,38,47,56,65,74}, + { 3,12,21,27,28,29,31,32,33,34,35,39,48,57,66,75}, + { 4,13,22,27,28,29,30,32,33,34,35,40,49,58,67,76}, + { 5,14,23,27,28,29,30,31,33,34,35,41,50,59,68,77}, + { 6,15,24,27,28,29,30,31,32,34,35,42,51,60,69,78}, + { 7,16,25,27,28,29,30,31,32,33,35,43,52,61,70,79}, + { 8,17,26,27,28,29,30,31,32,33,34,44,53,62,71,80}, + { 0, 9,18,27,37,38,39,40,41,42,43,44,45,54,63,72}, + { 1,10,19,28,36,38,39,40,41,42,43,44,46,55,64,73}, + { 2,11,20,29,36,37,39,40,41,42,43,44,47,56,65,74}, + { 3,12,21,30,36,37,38,40,41,42,43,44,48,57,66,75}, + { 4,13,22,31,36,37,38,39,41,42,43,44,49,58,67,76}, + { 5,14,23,32,36,37,38,39,40,42,43,44,50,59,68,77}, + { 6,15,24,33,36,37,38,39,40,41,43,44,51,60,69,78}, + { 7,16,25,34,36,37,38,39,40,41,42,44,52,61,70,79}, + { 8,17,26,35,36,37,38,39,40,41,42,43,53,62,71,80}, + { 0, 9,18,27,36,46,47,48,49,50,51,52,53,54,63,72}, + { 1,10,19,28,37,45,47,48,49,50,51,52,53,55,64,73}, + { 2,11,20,29,38,45,46,48,49,50,51,52,53,56,65,74}, + { 3,12,21,30,39,45,46,47,49,50,51,52,53,57,66,75}, + { 4,13,22,31,40,45,46,47,48,50,51,52,53,58,67,76}, + { 5,14,23,32,41,45,46,47,48,49,51,52,53,59,68,77}, + { 6,15,24,33,42,45,46,47,48,49,50,52,53,60,69,78}, + { 7,16,25,34,43,45,46,47,48,49,50,51,53,61,70,79}, + { 8,17,26,35,44,45,46,47,48,49,50,51,52,62,71,80}, + { 0, 9,18,27,36,45,55,56,57,58,59,60,61,62,63,72}, + { 1,10,19,28,37,46,54,56,57,58,59,60,61,62,64,73}, + { 2,11,20,29,38,47,54,55,57,58,59,60,61,62,65,74}, + { 3,12,21,30,39,48,54,55,56,58,59,60,61,62,66,75}, + { 4,13,22,31,40,49,54,55,56,57,59,60,61,62,67,76}, + { 5,14,23,32,41,50,54,55,56,57,58,60,61,62,68,77}, + { 6,15,24,33,42,51,54,55,56,57,58,59,61,62,69,78}, + { 7,16,25,34,43,52,54,55,56,57,58,59,60,62,70,79}, + { 8,17,26,35,44,53,54,55,56,57,58,59,60,61,71,80}, + { 0, 9,18,27,36,45,54,64,65,66,67,68,69,70,71,72}, + { 1,10,19,28,37,46,55,63,65,66,67,68,69,70,71,73}, + { 2,11,20,29,38,47,56,63,64,66,67,68,69,70,71,74}, + { 3,12,21,30,39,48,57,63,64,65,67,68,69,70,71,75}, + { 4,13,22,31,40,49,58,63,64,65,66,68,69,70,71,76}, + { 5,14,23,32,41,50,59,63,64,65,66,67,69,70,71,77}, + { 6,15,24,33,42,51,60,63,64,65,66,67,68,70,71,78}, + { 7,16,25,34,43,52,61,63,64,65,66,67,68,69,71,79}, + { 8,17,26,35,44,53,62,63,64,65,66,67,68,69,70,80}, + { 0, 9,18,27,36,45,54,63,73,74,75,76,77,78,79,80}, + { 1,10,19,28,37,46,55,64,72,74,75,76,77,78,79,80}, + { 2,11,20,29,38,47,56,65,72,73,75,76,77,78,79,80}, + { 3,12,21,30,39,48,57,66,72,73,74,76,77,78,79,80}, + { 4,13,22,31,40,49,58,67,72,73,74,75,77,78,79,80}, + { 5,14,23,32,41,50,59,68,72,73,74,75,76,78,79,80}, + { 6,15,24,33,42,51,60,69,72,73,74,75,76,77,79,80}, + { 7,16,25,34,43,52,61,70,72,73,74,75,76,77,78,80}, + { 8,17,26,35,44,53,62,71,72,73,74,75,76,77,78,79} + }; + if (Settings.getInstance().isBlocks()) + forwardVisibleCellIndex = new int [][] { + {1,2,3,4,5,6,7,8,9,10,11,18,19,20,27,36,45,54,63,72}, + {2,3,4,5,6,7,8,9,10,11,18,19,20,28,37,46,55,64,73}, + {3,4,5,6,7,8,9,10,11,18,19,20,29,38,47,56,65,74}, + {4,5,6,7,8,12,13,14,21,22,23,30,39,48,57,66,75}, + {5,6,7,8,12,13,14,21,22,23,31,40,49,58,67,76}, + {6,7,8,12,13,14,21,22,23,32,41,50,59,68,77}, + {7,8,15,16,17,24,25,26,33,42,51,60,69,78}, + {8,15,16,17,24,25,26,34,43,52,61,70,79}, + {15,16,17,24,25,26,35,44,53,62,71,80}, + {10,11,12,13,14,15,16,17,18,19,20,27,36,45,54,63,72}, + {11,12,13,14,15,16,17,18,19,20,28,37,46,55,64,73}, + {12,13,14,15,16,17,18,19,20,29,38,47,56,65,74}, + {13,14,15,16,17,21,22,23,30,39,48,57,66,75}, + {14,15,16,17,21,22,23,31,40,49,58,67,76}, + {15,16,17,21,22,23,32,41,50,59,68,77}, + {16,17,24,25,26,33,42,51,60,69,78}, + {17,24,25,26,34,43,52,61,70,79}, + {24,25,26,35,44,53,62,71,80}, + {19,20,21,22,23,24,25,26,27,36,45,54,63,72}, + {20,21,22,23,24,25,26,28,37,46,55,64,73}, + {21,22,23,24,25,26,29,38,47,56,65,74}, + {22,23,24,25,26,30,39,48,57,66,75}, + {23,24,25,26,31,40,49,58,67,76}, + {24,25,26,32,41,50,59,68,77}, + {25,26,33,42,51,60,69,78}, + {26,34,43,52,61,70,79}, + {35,44,53,62,71,80}, + {28,29,30,31,32,33,34,35,36,37,38,45,46,47,54,63,72}, + {29,30,31,32,33,34,35,36,37,38,45,46,47,55,64,73}, + {30,31,32,33,34,35,36,37,38,45,46,47,56,65,74}, + {31,32,33,34,35,39,40,41,48,49,50,57,66,75}, + {32,33,34,35,39,40,41,48,49,50,58,67,76}, + {33,34,35,39,40,41,48,49,50,59,68,77}, + {34,35,42,43,44,51,52,53,60,69,78}, + {35,42,43,44,51,52,53,61,70,79}, + {42,43,44,51,52,53,62,71,80}, + {37,38,39,40,41,42,43,44,45,46,47,54,63,72}, + {38,39,40,41,42,43,44,45,46,47,55,64,73}, + {39,40,41,42,43,44,45,46,47,56,65,74}, + {40,41,42,43,44,48,49,50,57,66,75}, + {41,42,43,44,48,49,50,58,67,76}, + {42,43,44,48,49,50,59,68,77}, + {43,44,51,52,53,60,69,78}, + {44,51,52,53,61,70,79}, + {51,52,53,62,71,80}, + {46,47,48,49,50,51,52,53,54,63,72}, + {47,48,49,50,51,52,53,55,64,73}, + {48,49,50,51,52,53,56,65,74}, + {49,50,51,52,53,57,66,75}, + {50,51,52,53,58,67,76}, + {51,52,53,59,68,77}, + {52,53,60,69,78}, + {53,61,70,79}, + {62,71,80}, + {55,56,57,58,59,60,61,62,63,64,65,72,73,74}, + {56,57,58,59,60,61,62,63,64,65,72,73,74}, + {57,58,59,60,61,62,63,64,65,72,73,74}, + {58,59,60,61,62,66,67,68,75,76,77}, + {59,60,61,62,66,67,68,75,76,77}, + {60,61,62,66,67,68,75,76,77}, + {61,62,69,70,71,78,79,80}, + {62,69,70,71,78,79,80}, + {69,70,71,78,79,80}, + {64,65,66,67,68,69,70,71,72,73,74}, + {65,66,67,68,69,70,71,72,73,74}, + {66,67,68,69,70,71,72,73,74}, + {67,68,69,70,71,75,76,77}, + {68,69,70,71,75,76,77}, + {69,70,71,75,76,77}, + {70,71,78,79,80}, + {71,78,79,80}, + {78,79,80}, + {73,74,75,76,77,78,79,80}, + {74,75,76,77,78,79,80}, + {75,76,77,78,79,80}, + {76,77,78,79,80}, + {77,78,79,80}, + {78,79,80}, + {79,80}, + {80}, + {} + }; + else + forwardVisibleCellIndex = new int [][] { + {1,2,3,4,5,6,7,8,9,18,27,36,45,54,63,72}, + {2,3,4,5,6,7,8,10,19,28,37,46,55,64,73}, + {3,4,5,6,7,8,11,20,29,38,47,56,65,74}, + {4,5,6,7,8,12,21,30,39,48,57,66,75}, + {5,6,7,8,13,22,31,40,49,58,67,76}, + {6,7,8,14,23,32,41,50,59,68,77}, + {7,8,15,24,33,42,51,60,69,78}, + {8,16,25,34,43,52,61,70,79}, + {17,26,35,44,53,62,71,80}, + {10,11,12,13,14,15,16,17,18,27,36,45,54,63,72}, + {11,12,13,14,15,16,17,19,28,37,46,55,64,73}, + {12,13,14,15,16,17,20,29,38,47,56,65,74}, + {13,14,15,16,17,21,30,39,48,57,66,75}, + {14,15,16,17,22,31,40,49,58,67,76}, + {15,16,17,23,32,41,50,59,68,77}, + {16,17,24,33,42,51,60,69,78}, + {17,25,34,43,52,61,70,79}, + {26,35,44,53,62,71,80}, + {19,20,21,22,23,24,25,26,27,36,45,54,63,72}, + {20,21,22,23,24,25,26,28,37,46,55,64,73}, + {21,22,23,24,25,26,29,38,47,56,65,74}, + {22,23,24,25,26,30,39,48,57,66,75}, + {23,24,25,26,31,40,49,58,67,76}, + {24,25,26,32,41,50,59,68,77}, + {25,26,33,42,51,60,69,78}, + {26,34,43,52,61,70,79}, + {35,44,53,62,71,80}, + {28,29,30,31,32,33,34,35,36,45,54,63,72}, + {29,30,31,32,33,34,35,37,46,55,64,73}, + {30,31,32,33,34,35,38,47,56,65,74}, + {31,32,33,34,35,39,48,57,66,75}, + {32,33,34,35,40,49,58,67,76}, + {33,34,35,41,50,59,68,77}, + {34,35,42,51,60,69,78}, + {35,43,52,61,70,79}, + {44,53,62,71,80}, + {37,38,39,40,41,42,43,44,45,54,63,72}, + {38,39,40,41,42,43,44,46,55,64,73}, + {39,40,41,42,43,44,47,56,65,74}, + {40,41,42,43,44,48,57,66,75}, + {41,42,43,44,49,58,67,76}, + {42,43,44,50,59,68,77}, + {43,44,51,60,69,78}, + {44,52,61,70,79}, + {53,62,71,80}, + {46,47,48,49,50,51,52,53,54,63,72}, + {47,48,49,50,51,52,53,55,64,73}, + {48,49,50,51,52,53,56,65,74}, + {49,50,51,52,53,57,66,75}, + {50,51,52,53,58,67,76}, + {51,52,53,59,68,77}, + {52,53,60,69,78}, + {53,61,70,79}, + {62,71,80}, + {55,56,57,58,59,60,61,62,63,72}, + {56,57,58,59,60,61,62,64,73}, + {57,58,59,60,61,62,65,74}, + {58,59,60,61,62,66,75}, + {59,60,61,62,67,76}, + {60,61,62,68,77}, + {61,62,69,78}, + {62,70,79}, + {71,80}, + {64,65,66,67,68,69,70,71,72}, + {65,66,67,68,69,70,71,73}, + {66,67,68,69,70,71,74}, + {67,68,69,70,71,75}, + {68,69,70,71,76}, + {69,70,71,77}, + {70,71,78}, + {71,79}, + {80}, + {73,74,75,76,77,78,79,80}, + {74,75,76,77,78,79,80}, + {75,76,77,78,79,80}, + {76,77,78,79,80}, + {77,78,79,80}, + {78,79,80}, + {79,80}, + {80}, + {} + }; + visibleCellsSet = new CellSet[] { + new CellSet(visibleCellIndex[0]),new CellSet(visibleCellIndex[1]),new CellSet(visibleCellIndex[2]),new CellSet(visibleCellIndex[3]),new CellSet(visibleCellIndex[4]),new CellSet(visibleCellIndex[5]),new CellSet(visibleCellIndex[6]),new CellSet(visibleCellIndex[7]),new CellSet(visibleCellIndex[8]), + new CellSet(visibleCellIndex[9]),new CellSet(visibleCellIndex[10]),new CellSet(visibleCellIndex[11]),new CellSet(visibleCellIndex[12]),new CellSet(visibleCellIndex[13]),new CellSet(visibleCellIndex[14]),new CellSet(visibleCellIndex[15]),new CellSet(visibleCellIndex[16]),new CellSet(visibleCellIndex[17]), + new CellSet(visibleCellIndex[18]),new CellSet(visibleCellIndex[19]),new CellSet(visibleCellIndex[20]),new CellSet(visibleCellIndex[21]),new CellSet(visibleCellIndex[22]),new CellSet(visibleCellIndex[23]),new CellSet(visibleCellIndex[24]),new CellSet(visibleCellIndex[25]),new CellSet(visibleCellIndex[26]), + new CellSet(visibleCellIndex[27]),new CellSet(visibleCellIndex[28]),new CellSet(visibleCellIndex[29]),new CellSet(visibleCellIndex[30]),new CellSet(visibleCellIndex[31]),new CellSet(visibleCellIndex[32]),new CellSet(visibleCellIndex[33]),new CellSet(visibleCellIndex[34]),new CellSet(visibleCellIndex[35]), + new CellSet(visibleCellIndex[36]),new CellSet(visibleCellIndex[37]),new CellSet(visibleCellIndex[38]),new CellSet(visibleCellIndex[39]),new CellSet(visibleCellIndex[40]),new CellSet(visibleCellIndex[41]),new CellSet(visibleCellIndex[42]),new CellSet(visibleCellIndex[43]),new CellSet(visibleCellIndex[44]), + new CellSet(visibleCellIndex[45]),new CellSet(visibleCellIndex[46]),new CellSet(visibleCellIndex[47]),new CellSet(visibleCellIndex[48]),new CellSet(visibleCellIndex[49]),new CellSet(visibleCellIndex[50]),new CellSet(visibleCellIndex[51]),new CellSet(visibleCellIndex[52]),new CellSet(visibleCellIndex[53]), + new CellSet(visibleCellIndex[54]),new CellSet(visibleCellIndex[55]),new CellSet(visibleCellIndex[56]),new CellSet(visibleCellIndex[57]),new CellSet(visibleCellIndex[58]),new CellSet(visibleCellIndex[59]),new CellSet(visibleCellIndex[60]),new CellSet(visibleCellIndex[61]),new CellSet(visibleCellIndex[62]), + new CellSet(visibleCellIndex[63]),new CellSet(visibleCellIndex[64]),new CellSet(visibleCellIndex[65]),new CellSet(visibleCellIndex[66]),new CellSet(visibleCellIndex[67]),new CellSet(visibleCellIndex[68]),new CellSet(visibleCellIndex[69]),new CellSet(visibleCellIndex[70]),new CellSet(visibleCellIndex[71]), + new CellSet(visibleCellIndex[72]),new CellSet(visibleCellIndex[73]),new CellSet(visibleCellIndex[74]),new CellSet(visibleCellIndex[75]),new CellSet(visibleCellIndex[76]),new CellSet(visibleCellIndex[77]),new CellSet(visibleCellIndex[78]),new CellSet(visibleCellIndex[79]),new CellSet(visibleCellIndex[80]) + }; + forwardVisibleCellsSet = new CellSet[] { + new CellSet(forwardVisibleCellIndex[0]),new CellSet(forwardVisibleCellIndex[1]),new CellSet(forwardVisibleCellIndex[2]),new CellSet(forwardVisibleCellIndex[3]),new CellSet(forwardVisibleCellIndex[4]),new CellSet(forwardVisibleCellIndex[5]),new CellSet(forwardVisibleCellIndex[6]),new CellSet(forwardVisibleCellIndex[7]),new CellSet(forwardVisibleCellIndex[8]), + new CellSet(forwardVisibleCellIndex[9]),new CellSet(forwardVisibleCellIndex[10]),new CellSet(forwardVisibleCellIndex[11]),new CellSet(forwardVisibleCellIndex[12]),new CellSet(forwardVisibleCellIndex[13]),new CellSet(forwardVisibleCellIndex[14]),new CellSet(forwardVisibleCellIndex[15]),new CellSet(forwardVisibleCellIndex[16]),new CellSet(forwardVisibleCellIndex[17]), + new CellSet(forwardVisibleCellIndex[18]),new CellSet(forwardVisibleCellIndex[19]),new CellSet(forwardVisibleCellIndex[20]),new CellSet(forwardVisibleCellIndex[21]),new CellSet(forwardVisibleCellIndex[22]),new CellSet(forwardVisibleCellIndex[23]),new CellSet(forwardVisibleCellIndex[24]),new CellSet(forwardVisibleCellIndex[25]),new CellSet(forwardVisibleCellIndex[26]), + new CellSet(forwardVisibleCellIndex[27]),new CellSet(forwardVisibleCellIndex[28]),new CellSet(forwardVisibleCellIndex[29]),new CellSet(forwardVisibleCellIndex[30]),new CellSet(forwardVisibleCellIndex[31]),new CellSet(forwardVisibleCellIndex[32]),new CellSet(forwardVisibleCellIndex[33]),new CellSet(forwardVisibleCellIndex[34]),new CellSet(forwardVisibleCellIndex[35]), + new CellSet(forwardVisibleCellIndex[36]),new CellSet(forwardVisibleCellIndex[37]),new CellSet(forwardVisibleCellIndex[38]),new CellSet(forwardVisibleCellIndex[39]),new CellSet(forwardVisibleCellIndex[40]),new CellSet(forwardVisibleCellIndex[41]),new CellSet(forwardVisibleCellIndex[42]),new CellSet(forwardVisibleCellIndex[43]),new CellSet(forwardVisibleCellIndex[44]), + new CellSet(forwardVisibleCellIndex[45]),new CellSet(forwardVisibleCellIndex[46]),new CellSet(forwardVisibleCellIndex[47]),new CellSet(forwardVisibleCellIndex[48]),new CellSet(forwardVisibleCellIndex[49]),new CellSet(forwardVisibleCellIndex[50]),new CellSet(forwardVisibleCellIndex[51]),new CellSet(forwardVisibleCellIndex[52]),new CellSet(forwardVisibleCellIndex[53]), + new CellSet(forwardVisibleCellIndex[54]),new CellSet(forwardVisibleCellIndex[55]),new CellSet(forwardVisibleCellIndex[56]),new CellSet(forwardVisibleCellIndex[57]),new CellSet(forwardVisibleCellIndex[58]),new CellSet(forwardVisibleCellIndex[59]),new CellSet(forwardVisibleCellIndex[60]),new CellSet(forwardVisibleCellIndex[61]),new CellSet(forwardVisibleCellIndex[62]), + new CellSet(forwardVisibleCellIndex[63]),new CellSet(forwardVisibleCellIndex[64]),new CellSet(forwardVisibleCellIndex[65]),new CellSet(forwardVisibleCellIndex[66]),new CellSet(forwardVisibleCellIndex[67]),new CellSet(forwardVisibleCellIndex[68]),new CellSet(forwardVisibleCellIndex[69]),new CellSet(forwardVisibleCellIndex[70]),new CellSet(forwardVisibleCellIndex[71]), + new CellSet(forwardVisibleCellIndex[72]),new CellSet(forwardVisibleCellIndex[73]),new CellSet(forwardVisibleCellIndex[74]),new CellSet(forwardVisibleCellIndex[75]),new CellSet(forwardVisibleCellIndex[76]),new CellSet(forwardVisibleCellIndex[77]),new CellSet(forwardVisibleCellIndex[78]),new CellSet(forwardVisibleCellIndex[79]),new CellSet(forwardVisibleCellIndex[80]) + }; + } + + + /** * Set the value of a cell * @param x the horizontal coordinate of the cell @@ -539,7 +901,7 @@ public void setCellPotentialValues(int index, BitSet values) { */ public Cell getFirstCancellerOf(Cell target, int value) { int[] visible = Grid.visibleCellIndex[target.getIndex()]; - for(int i = 0; i < 20; i++) { + for(int i = 0; i < (Settings.getInstance().isBlocks() ? 20 : 16); i++) { if(cellValues[visible[i]] == value) return Grid.getCell(visible[i]); } return null; diff --git a/diuf/sudoku/Settings.java b/diuf/sudoku/Settings.java index 44a764e..791efd9 100644 --- a/diuf/sudoku/Settings.java +++ b/diuf/sudoku/Settings.java @@ -17,8 +17,8 @@ public class Settings { public final static int VERSION = 1; - public final static int REVISION = 10; - public final static String SUBREV = ".4"; + public final static int REVISION = 11; + public final static String SUBREV = ".1"; public final static String releaseDate = "2019-11-14"; public final static String releaseYear = "2019"; public final static String releaseLicence = "Lesser General Public License"; diff --git a/diuf/sudoku/generator/Generator.java b/diuf/sudoku/generator/Generator.java index 1859490..58e73ad 100644 --- a/diuf/sudoku/generator/Generator.java +++ b/diuf/sudoku/generator/Generator.java @@ -107,9 +107,11 @@ public Grid generate(Random rnd, Symmetry symmetry) { for (Point p : points) { //Cell cell = grid.getCell(p.x, p.y); //if (cell.getValue() != 0) { + grid.resetGiven(y * 9 + x); if (grid.getCellValue(p.x, p.y) != 0) { //cell.setValue(0); grid.setCellValue(p.x, p.y, 0); + cellRemoved = true; } } @@ -124,8 +126,10 @@ public Grid generate(Random rnd, Symmetry symmetry) { assert false : "Invalid grid"; } else { // Failed. Put the cells back and try with next cell - for (Point p : points) + for (Point p : points){ grid.setCellValue(p.x, p.y, solution.getCellValue(p.x, p.y)); + grid.setGiven(y * 9 + x); + } //attempts += 1; } } diff --git a/diuf/sudoku/gui/SudokuFrame.java b/diuf/sudoku/gui/SudokuFrame.java index a0ee94f..2a8a4a3 100644 --- a/diuf/sudoku/gui/SudokuFrame.java +++ b/diuf/sudoku/gui/SudokuFrame.java @@ -99,7 +99,7 @@ public class SudokuFrame extends JFrame implements Asker { private JRadioButtonMenuItem mitChessMode = null; private JCheckBoxMenuItem mitAntiAliasing = null; private JMenu variantsMenu = null; - private JCheckBoxMenuItem mitVanilla = null; + //private JCheckBoxMenuItem mitVanilla = null; private JCheckBoxMenuItem mitLQ = null; private JCheckBoxMenuItem mitX = null; private JCheckBoxMenuItem mitDG = null; @@ -328,8 +328,10 @@ private void initialize() { Experimental = ".1"; ExSuffix ="New Rating "; } + if (!getInstance().isBlocks()) + ExSuffix += "Latin Square "; if (getInstance().isBringBackSE121()) { - Experimental =" (Original Sudoku Explainer 1.2.1 mode)"; + Experimental =" (Explainer 1.2.1 mode)"; } this.setTitle(ExSuffix + "Sukaku Explainer " + VERSION + "." + REVISION + SUBREV + Experimental); JMenuBar menuBar = getJJMenuBar(); @@ -1389,7 +1391,7 @@ private JMenu getVariantsMenu() { variantsMenu = new JMenu(); variantsMenu.setText("Variants"); //variantsMenu.setMnemonic(java.awt.event.KeyEvent.VK_H); - variantsMenu.add(getMitVanilla()); + //variantsMenu.add(getMitVanilla()); variantsMenu.add(getMitLQ()); //getMitShowWelcome().setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0)); //variantsMenu.addSeparator(); @@ -1399,7 +1401,7 @@ private JMenu getVariantsMenu() { return variantsMenu; } - private JCheckBoxMenuItem getMitVanilla() { + /* private JCheckBoxMenuItem getMitVanilla() { if (mitVanilla == null) { mitVanilla = new JCheckBoxMenuItem(); mitVanilla.setText("Vanilla Sudoku"); @@ -1407,13 +1409,13 @@ private JCheckBoxMenuItem getMitVanilla() { mitVanilla.setToolTipText("Vanilla Sudoku with Row, Columns and Blocks"); mitVanilla.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent e) { - mitVanilla.setSelected(true); + mitVanilla.setSelected(!mitLQ.isSelected()); } }); } return mitVanilla; } - +*/ private JCheckBoxMenuItem getMitLQ() { if (mitLQ == null) { mitLQ = new JCheckBoxMenuItem(); @@ -1422,7 +1424,15 @@ private JCheckBoxMenuItem getMitLQ() { mitLQ.setToolTipText("Latin Square with Row and columns only. No Blocks"); mitLQ.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent e) { - mitLQ.setSelected(false); + Settings.getInstance().setBlocks(!mitLQ.isSelected()); + Grid.changeVisibleCells(); + if (Settings.getInstance().isBlocks()) { + } + engine.clearGrid(); + engine.clearHints(); + initialize(); + repaintViews(); + showWelcomeText(); } }); } diff --git a/diuf/sudoku/gui/SudokuPanel.java b/diuf/sudoku/gui/SudokuPanel.java index ce9767e..7b0e33e 100644 --- a/diuf/sudoku/gui/SudokuPanel.java +++ b/diuf/sudoku/gui/SudokuPanel.java @@ -442,7 +442,7 @@ private boolean initFillColor(Graphics g, Cell cell, boolean origin) { Color col; boolean ret = false; - if ((cell.getX() / 3 % 2 == 0) ^ (cell.getY() / 3 % 2 == 0)) { + if (((cell.getX() / 3 % 2 == 0) ^ (cell.getY() / 3 % 2 == 0)) && Settings.getInstance().isBlocks()) { col = alternativeColor; } else { col = Color.white; @@ -622,7 +622,7 @@ private void paintSelectionAndFocus(Graphics g) { private void paintGrid(Graphics g) { for (int i = 0; i <= 9; i++) { int lineWidth; - if (i % 3 == 0) { + if ((i % 3 == 0 && Settings.getInstance().isBlocks()) || i % 9 == 0) { lineWidth = 4; g.setColor(borderColor); } else { diff --git a/diuf/sudoku/solver/Solver.java b/diuf/sudoku/solver/Solver.java index 9bffc1f..f8cddd9 100644 --- a/diuf/sudoku/solver/Solver.java +++ b/diuf/sudoku/solver/Solver.java @@ -193,12 +193,14 @@ public Solver(Grid grid) { } else { addIfWorth(SolvingTechnique.HiddenSingle, directHintProducers, new HiddenSingle()); - addIfWorth(SolvingTechnique.DirectPointing, directHintProducers, new Locking(true)); + if (Settings.getInstance().isBlocks()) + addIfWorth(SolvingTechnique.DirectPointing, directHintProducers, new Locking(true)); addIfWorth(SolvingTechnique.DirectHiddenPair, directHintProducers, new HiddenSet(2, true)); addIfWorth(SolvingTechnique.NakedSingle, directHintProducers, new NakedSingle()); addIfWorth(SolvingTechnique.DirectHiddenTriplet, directHintProducers, new HiddenSet(3, true)); indirectHintProducers = new ArrayList(); - addIfWorth(SolvingTechnique.PointingClaiming, indirectHintProducers, new Locking(false)); + if (Settings.getInstance().isBlocks()) + addIfWorth(SolvingTechnique.PointingClaiming, indirectHintProducers, new Locking(false)); addIfWorth(SolvingTechnique.NakedPair, indirectHintProducers, new NakedSet(2)); addIfWorth(SolvingTechnique.XWing, indirectHintProducers, new Fisherman(2)); addIfWorth(SolvingTechnique.HiddenPair, indirectHintProducers, new HiddenSet(2, false)); diff --git a/diuf/sudoku/solver/checks/BruteForceAnalysis.java b/diuf/sudoku/solver/checks/BruteForceAnalysis.java index 1ad9c94..4f5d786 100644 --- a/diuf/sudoku/solver/checks/BruteForceAnalysis.java +++ b/diuf/sudoku/solver/checks/BruteForceAnalysis.java @@ -225,7 +225,7 @@ private boolean analyse(Grid grid, boolean isReverse, Random rnd, * imply that the sudoku has no solution. */ private boolean isFillable(Grid grid) { - for (int regionTypeIndex = 0; regionTypeIndex < 3; regionTypeIndex++) { + for (int regionTypeIndex = (Settings.getInstance().isBlocks() ? 0 : 1); regionTypeIndex < 3; regionTypeIndex++) { Grid.Region[] regions = Grid.getRegions(regionTypeIndex); for (int i = 0; i < 9; i++) { Grid.Region region = regions[i]; diff --git a/diuf/sudoku/solver/checks/NoDoubles.java b/diuf/sudoku/solver/checks/NoDoubles.java index 20817e1..3be2a80 100644 --- a/diuf/sudoku/solver/checks/NoDoubles.java +++ b/diuf/sudoku/solver/checks/NoDoubles.java @@ -23,7 +23,7 @@ public void getHints(Grid grid, HintsAccumulator accu) // Iterate on region types //for (Class regionType : Grid.getRegionTypes()) { // Grid.Region[] regions = grid.getRegions(regionType); - for (int regionTypeIndex = 0; regionTypeIndex < 3; regionTypeIndex++) { + for (int regionTypeIndex = (Settings.getInstance().isBlocks() ? 0 : 1); regionTypeIndex < 3; regionTypeIndex++) { Grid.Region[] regions = Grid.getRegions(regionTypeIndex); // Iterate on occurances of a region @@ -74,7 +74,7 @@ public boolean isValid(Grid grid) { // Iterate on region types //for (Class regionType : Grid.getRegionTypes()) { // Grid.Region[] regions = grid.getRegions(regionType); - for (int regionTypeIndex = 0; regionTypeIndex < 3; regionTypeIndex++) { + for (int regionTypeIndex = (Settings.getInstance().isBlocks() ? 0 : 1); regionTypeIndex < 3; regionTypeIndex++) { Grid.Region[] regions = Grid.getRegions(regionTypeIndex); // Iterate on occurances of a region diff --git a/diuf/sudoku/solver/rules/HiddenSet.java b/diuf/sudoku/solver/rules/HiddenSet.java index 47ddac4..2dced80 100644 --- a/diuf/sudoku/solver/rules/HiddenSet.java +++ b/diuf/sudoku/solver/rules/HiddenSet.java @@ -31,7 +31,8 @@ public HiddenSet(int degree, boolean isDirect) { } public void getHints(Grid grid, HintsAccumulator accu) throws InterruptedException { - getHints(grid, 0, accu); //block + if (Settings.getInstance().isBlocks()) + getHints(grid, 0, accu); //block getHints(grid, 2, accu); //column getHints(grid, 1, accu); //row } diff --git a/diuf/sudoku/solver/rules/HiddenSingle.java b/diuf/sudoku/solver/rules/HiddenSingle.java index e86490c..8307cd2 100644 --- a/diuf/sudoku/solver/rules/HiddenSingle.java +++ b/diuf/sudoku/solver/rules/HiddenSingle.java @@ -18,11 +18,13 @@ public class HiddenSingle implements DirectHintProducer { public void getHints(Grid grid, HintsAccumulator accu) throws InterruptedException { // First alone cells (last empty cell in a region) - getHints(grid, 0, accu, true); //block + if (Settings.getInstance().isBlocks()) + getHints(grid, 0, accu, true); //block getHints(grid, 2, accu, true); //column getHints(grid, 1, accu, true); //row // Then hidden cells - getHints(grid, 0, accu, false); //block + if (Settings.getInstance().isBlocks()) + getHints(grid, 0, accu, false); //block getHints(grid, 2, accu, false); //column getHints(grid, 1, accu, false); //row } diff --git a/diuf/sudoku/solver/rules/NakedSet.java b/diuf/sudoku/solver/rules/NakedSet.java index 0491fce..4374477 100644 --- a/diuf/sudoku/solver/rules/NakedSet.java +++ b/diuf/sudoku/solver/rules/NakedSet.java @@ -26,7 +26,8 @@ public NakedSet(int degree) { } public void getHints(Grid grid, HintsAccumulator accu) throws InterruptedException { - getHints(grid, 0, accu); //block + if (Settings.getInstance().isBlocks()) + getHints(grid, 0, accu); //block getHints(grid, 2, accu); //column getHints(grid, 1, accu); //row } diff --git a/diuf/sudoku/solver/rules/StrongLinks.java b/diuf/sudoku/solver/rules/StrongLinks.java index 40aa56c..dbac324 100644 --- a/diuf/sudoku/solver/rules/StrongLinks.java +++ b/diuf/sudoku/solver/rules/StrongLinks.java @@ -74,6 +74,12 @@ public void getHints(Grid grid, HintsAccumulator accu) throws InterruptedExcepti int linkNumber = 0; int setNumber = 0; while (linkNumber < degree) { + //For Latin Square + if (!Settings.getInstance().isBlocks() && setsAll[setNumber][8 - degree + linkNumber] == 0) { + linkNumber = 0; + setNumber++; + continue; + } Set[linkNumber] = setsAll[setNumber][8 - degree + linkNumber]; linkNumber++; if (linkNumber == degree && (setNumber < 45 && (degree == 8 || setsAll[setNumber][8 - degree - 1] != 1))) { @@ -140,20 +146,21 @@ else if (bridge2Support == null) { else if (bridge2.getY() == bridge1Support.getY() && bridge1.getY() == bridge2Support.getY()) return (Grid.Row)Grid.getRegionAt(1,bridge1.getIndex()); } - if (bridge1.getB() == bridge2.getB()) { - if (bridge1Support == null && bridge2Support == null) - return (Grid.Block)Grid.getRegionAt(0,bridge1.getIndex()); - else if (bridge1Support == null) { - if (bridge1.getB() == bridge2Support.getB()) - return (Grid.Block)Grid.getRegionAt(0,bridge1.getIndex()); - } - else if (bridge2Support == null) { - if (bridge2.getB() == bridge1Support.getB()) + if (Settings.getInstance().isBlocks()) + if (bridge1.getB() == bridge2.getB()) { + if (bridge1Support == null && bridge2Support == null) return (Grid.Block)Grid.getRegionAt(0,bridge1.getIndex()); - } - else if (bridge2.getB() == bridge1Support.getB() && bridge1.getB() == bridge2Support.getB()) - return (Grid.Block)Grid.getRegionAt(0,bridge1.getIndex()); - } + else if (bridge1Support == null) { + if (bridge1.getB() == bridge2Support.getB()) + return (Grid.Block)Grid.getRegionAt(0,bridge1.getIndex()); + } + else if (bridge2Support == null) { + if (bridge2.getB() == bridge1Support.getB()) + return (Grid.Block)Grid.getRegionAt(0,bridge1.getIndex()); + } + else if (bridge2.getB() == bridge1Support.getB() && bridge1.getB() == bridge2Support.getB()) + return (Grid.Block)Grid.getRegionAt(0,bridge1.getIndex()); + } return null; } diff --git a/diuf/sudoku/solver/rules/TurbotFish.java b/diuf/sudoku/solver/rules/TurbotFish.java index 21b5d68..c6a7f10 100644 --- a/diuf/sudoku/solver/rules/TurbotFish.java +++ b/diuf/sudoku/solver/rules/TurbotFish.java @@ -27,7 +27,7 @@ public void getHints(Grid grid, HintsAccumulator accu) throws InterruptedExcepti }; List hintsFinal = new ArrayList(); List hintsStart; - for (int i = 0; i < 5 ; i++) { + for (int i = 0; i < (Settings.getInstance().isBlocks() ? 5 : 3) ; i++) { hintsStart = getHints(grid, Sets[i][0], Sets[i][1]); for (TurbotFishHint hint : hintsStart) hintsFinal.add(hint); @@ -87,20 +87,21 @@ else if (bridge2Support == null) { else if (bridge2.getY() == bridge1Support.getY()) return (Grid.Row)Grid.getRegionAt(1,bridge1.getIndex()); } - if (bridge1.getB() == bridge2.getB()) { - if (bridge1Support == null && bridge2Support == null) - return (Grid.Block)Grid.getRegionAt(0,bridge1.getIndex()); - else if (bridge1Support == null) { - if (bridge1.getB() == bridge2Support.getB()) + if (Settings.getInstance().isBlocks()) + if (bridge1.getB() == bridge2.getB()) { + if (bridge1Support == null && bridge2Support == null) return (Grid.Block)Grid.getRegionAt(0,bridge1.getIndex()); - } - else if (bridge2Support == null) { - if (bridge2.getB() == bridge1Support.getB()) - return (Grid.Block)Grid.getRegionAt(0,bridge1.getIndex()); - } - else if (bridge2.getB() == bridge1Support.getB()) - return (Grid.Block)Grid.getRegionAt(0,bridge1.getIndex()); - } + else if (bridge1Support == null) { + if (bridge1.getB() == bridge2Support.getB()) + return (Grid.Block)Grid.getRegionAt(0,bridge1.getIndex()); + } + else if (bridge2Support == null) { + if (bridge2.getB() == bridge1Support.getB()) + return (Grid.Block)Grid.getRegionAt(0,bridge1.getIndex()); + } + else if (bridge2.getB() == bridge1Support.getB()) + return (Grid.Block)Grid.getRegionAt(0,bridge1.getIndex()); + } return null; } diff --git a/diuf/sudoku/solver/rules/chaining/Chaining.java b/diuf/sudoku/solver/rules/chaining/Chaining.java index 4cc104a..20ab13d 100644 --- a/diuf/sudoku/solver/rules/chaining/Chaining.java +++ b/diuf/sudoku/solver/rules/chaining/Chaining.java @@ -506,7 +506,7 @@ private void doBinaryChaining(Grid grid, Potential pOn, Potential pOff, private void doRegionChainings(Grid grid, List result, Cell cell, int value, LinkedSet onToOn, LinkedSet onToOff) { - for (int regionTypeIndex = 0; regionTypeIndex < 3; regionTypeIndex++) { + for (int regionTypeIndex = (Settings.getInstance().isBlocks() ? 0 : 1); regionTypeIndex < 3; regionTypeIndex++) { Grid.Region region = Grid.getRegionAt(regionTypeIndex, cell.getIndex()); BitSet potentialPositions = region.getPotentialPositions(grid, value); @@ -591,18 +591,20 @@ private Set getOnToOff(Grid grid, Potential p, boolean isYChainEnable Grid.Region box = Grid.getRegionAt(0, potentialCellIndex); BitSet boxPositions = box.copyPotentialPositions(grid, p.value); boxPositions.clear(box.indexOf(p.cell)); - for (int i = boxPositions.nextSetBit(0); i >= 0; i = boxPositions.nextSetBit(i + 1)) { - Cell cell = box.getCell(i); - result.add(new Potential(cell, p.value, false, p, - getRegionCause(0), - "the value can occur only once in the " + box.toString())); - } + if (Settings.getInstance().isBlocks()) + for (int i = boxPositions.nextSetBit(0); i >= 0; i = boxPositions.nextSetBit(i + 1)) { + Cell cell = box.getCell(i); + result.add(new Potential(cell, p.value, false, p, + getRegionCause(0), + "the value can occur only once in the " + box.toString())); + } Grid.Region row = Grid.getRegionAt(1, potentialCellIndex); BitSet rowPositions = row.copyPotentialPositions(grid, p.value); rowPositions.clear(row.indexOf(p.cell)); for (int i = rowPositions.nextSetBit(0); i >= 0; i = rowPositions.nextSetBit(i + 1)) { Cell cell = row.getCell(i); - if(box.contains(cell)) continue; + if (Settings.getInstance().isBlocks()) + if(box.contains(cell)) continue; result.add(new Potential(cell, p.value, false, p, getRegionCause(1), "the value can occur only once in the " + row.toString())); @@ -612,7 +614,8 @@ private Set getOnToOff(Grid grid, Potential p, boolean isYChainEnable colPositions.clear(col.indexOf(p.cell)); for (int i = colPositions.nextSetBit(0); i >= 0; i = colPositions.nextSetBit(i + 1)) { Cell cell = col.getCell(i); - if(box.contains(cell)) continue; + if (Settings.getInstance().isBlocks()) + if(box.contains(cell)) continue; result.add(new Potential(cell, p.value, false, p, getRegionCause(2), "the value can occur only once in the " + col.toString())); @@ -660,7 +663,7 @@ private void addHiddenParentsOfRegion(Potential p, Grid grid, Grid source, } static Potential.Cause getRegionCause(Region region) { //still in use by collectRuleParents where for regionchaining region is used for repaint and stings - if (region instanceof Block) + if (region instanceof Block && Settings.getInstance().isBlocks()) return Potential.Cause.HiddenBlock; else if (region instanceof Column) return Potential.Cause.HiddenColumn; @@ -709,7 +712,7 @@ private Set getOffToOn(Grid grid, Potential p, Grid source, if (isXChainEnabled) { // Second rule: if there are only two positions for this potential, the other one gets on int thisValue = p.value; - for(int regionTypeIndex = 0; regionTypeIndex < 3; regionTypeIndex++) { + for(int regionTypeIndex = (Settings.getInstance().isBlocks() ? 0 : 1); regionTypeIndex < 3; regionTypeIndex++) { Region r = Grid.regions[regionTypeIndex][Grid.cellRegions[thisCellIndex][regionTypeIndex]]; int otherPosition = -1; for(int regionCellIndex = 0; regionCellIndex < 9; regionCellIndex++) { diff --git a/diuf/sudoku/solver/rules/unique/BivalueUniversalGrave.java b/diuf/sudoku/solver/rules/unique/BivalueUniversalGrave.java index 153ae20..b9893b9 100644 --- a/diuf/sudoku/solver/rules/unique/BivalueUniversalGrave.java +++ b/diuf/sudoku/solver/rules/unique/BivalueUniversalGrave.java @@ -31,7 +31,7 @@ public void getHints(Grid grid, HintsAccumulator accu) throws InterruptedExcepti CellSet allExtraCells = null; int onlyValue = 0; boolean oneValue = true; - for (int regionTypeIndex = 0; regionTypeIndex < 3; regionTypeIndex++) { + for (int regionTypeIndex = (Settings.getInstance().isBlocks() ? 0 : 1); regionTypeIndex < 3; regionTypeIndex++) { Grid.Region[] regions = Grid.getRegions(regionTypeIndex); for (int i = 0; i < regions.length; i++) { Grid.Region region = regions[i]; @@ -113,7 +113,7 @@ public void getHints(Grid grid, HintsAccumulator accu) throws InterruptedExcepti } } else { - for (int regionTypeIndex = 0; regionTypeIndex < 3; regionTypeIndex++) { + for (int regionTypeIndex = (Settings.getInstance().isBlocks() ? 0 : 1); regionTypeIndex < 3; regionTypeIndex++) { Grid.Region[] regions = Grid.getRegions(regionTypeIndex); for (int i = 0; i < regions.length; i++) { Grid.Region region = regions[i]; @@ -173,7 +173,7 @@ public void getHints(Grid grid, HintsAccumulator accu) throws InterruptedExcepti } // When bug values have been removed, all remaining candidates must have // two positions in each region - for (int regionTypeIndex = 0; regionTypeIndex < 3; regionTypeIndex++) { + for (int regionTypeIndex = (Settings.getInstance().isBlocks() ? 0 : 1); regionTypeIndex < 3; regionTypeIndex++) { Grid.Region[] regions = Grid.getRegions(regionTypeIndex); for (int i = 0; i < regions.length; i++) { Grid.Region region = regions[i]; @@ -243,7 +243,7 @@ private void addBug3Hint(HintsAccumulator accu, List bugCells, if (Settings.getInstance().islkSudokuBUG()) { for (int degree = 2; degree <= 6; degree++) { //for (Class regionType : grid.getRegionTypes()) { - for (int regionTypeIndex = 0; regionTypeIndex < 3; regionTypeIndex++) { + for (int regionTypeIndex = (Settings.getInstance().isBlocks() ? 0 : 1); regionTypeIndex < 3; regionTypeIndex++) { // Look for a region of this type shared by bugCells Grid.Region region = null; for (Cell cell : bugCells) { @@ -323,7 +323,7 @@ private void addBug3Hint(HintsAccumulator accu, List bugCells, } // for (degree) } else { - for (int regionTypeIndex = 0; regionTypeIndex < 3; regionTypeIndex++) { + for (int regionTypeIndex = (Settings.getInstance().isBlocks() ? 0 : 1); regionTypeIndex < 3; regionTypeIndex++) { // Look for a region of this type shared by bugCells Grid.Region region = null; for (Cell cell : bugCells) { @@ -422,7 +422,7 @@ private void addBug4Hint(HintsAccumulator accu, List bugCells, return; // No BUG type 4 //for (Class regionType : Grid.getRegionTypes()) { - for (int regionTypeIndex = 0; regionTypeIndex < 3; regionTypeIndex++) { + for (int regionTypeIndex = (Settings.getInstance().isBlocks() ? 0 : 1); regionTypeIndex < 3; regionTypeIndex++) { // Look for a region of this type shared by all bugCells Grid.Region region = null; for (Cell cell : bugCells) { diff --git a/diuf/sudoku/solver/rules/unique/UniqueLoops.java b/diuf/sudoku/solver/rules/unique/UniqueLoops.java index a40d7bc..5b001df 100644 --- a/diuf/sudoku/solver/rules/unique/UniqueLoops.java +++ b/diuf/sudoku/solver/rules/unique/UniqueLoops.java @@ -142,7 +142,7 @@ private void checkForLoops(Grid grid, Cell cell, int v1, int v2, int lastRegionTypeIndex, Collection> results) { loop.add(cell); if (Settings.getInstance().islkSudokuURUL()) { - for (int regionTypeIndex = 0; regionTypeIndex < 3; regionTypeIndex++) { + for (int regionTypeIndex = (Settings.getInstance().isBlocks() ? 0 : 1); regionTypeIndex < 3; regionTypeIndex++) { if (regionTypeIndex != lastRegionTypeIndex) { Grid.Region region = Grid.getRegionAt(regionTypeIndex, cell.getIndex()); for (int i = 0; i < 9; i++) { @@ -182,7 +182,7 @@ private void checkForLoops(Grid grid, Cell cell, int v1, int v2, } else { exValues = (BitSet)exValues.clone(); // Ensure we cleanup ourself - for (int regionTypeIndex = 0; regionTypeIndex < 3; regionTypeIndex++) { + for (int regionTypeIndex = (Settings.getInstance().isBlocks() ? 0 : 1); regionTypeIndex < 3; regionTypeIndex++) { if (regionTypeIndex != lastRegionTypeIndex) { Grid.Region region = Grid.getRegionAt(regionTypeIndex, cell.getIndex()); for (int i = 0; i < 9; i++) { @@ -241,7 +241,7 @@ private boolean isValidLoop(Grid grid, List loop) { HashSet visitedEven = new HashSet(); boolean isOdd = false; for (Cell cell : loop) { - for (int regionTypeIndex = 0; regionTypeIndex < 3; regionTypeIndex++) { + for (int regionTypeIndex = (Settings.getInstance().isBlocks() ? 0 : 1); regionTypeIndex < 3; regionTypeIndex++) { Grid.Region region = Grid.getRegionAt(regionTypeIndex, cell.getIndex()); if (isOdd) { if (visitedOdd.contains(region)) @@ -322,7 +322,7 @@ private Collection createType3Hints(Grid grid, List loop, // Look for Naked and hidden Sets. Iterate on degree if (Settings.getInstance().islkSudokuURUL()) { for (int degree = 2; degree <= 7; degree++) { - for (int regionTypeIndex = 0; regionTypeIndex < 3; regionTypeIndex++) { + for (int regionTypeIndex = (Settings.getInstance().isBlocks() ? 0 : 1); regionTypeIndex < 3; regionTypeIndex++) { Grid.Region region = Grid.getRegionAt(regionTypeIndex, c1.getIndex()); if (region.equals(Grid.getRegionAt(regionTypeIndex, c2.getIndex()))) { // Region common to c1 and c2 @@ -418,7 +418,7 @@ private Collection createType3Hints(Grid grid, List loop, } else { for (int degree = extra.cardinality(); degree <= 7; degree++) { - for (int regionTypeIndex = 0; regionTypeIndex < 3; regionTypeIndex++) { + for (int regionTypeIndex = (Settings.getInstance().isBlocks() ? 0 : 1); regionTypeIndex < 3; regionTypeIndex++) { Grid.Region region = Grid.getRegionAt(regionTypeIndex, c1.getIndex()); if (region.equals(Grid.getRegionAt(regionTypeIndex, c2.getIndex()))) { // Region common to c1 and c2 @@ -595,7 +595,7 @@ private UniqueLoopHint createType4Hint(Grid grid, List loop, Cell c1, Cell // Look for v1 or v2 locked in a region of c1 and c2 Grid.Region r1 = null; Grid.Region r2 = null; - for (int regionTypeIndex = 0; regionTypeIndex < 3; regionTypeIndex++) { + for (int regionTypeIndex = (Settings.getInstance().isBlocks() ? 0 : 1); regionTypeIndex < 3; regionTypeIndex++) { Grid.Region region = Grid.getRegionAt(regionTypeIndex, c1.getIndex()); if (region.equals(Grid.getRegionAt(regionTypeIndex, c2.getIndex()))) { // Region common to c1 and c2 diff --git a/diuf/sudoku/test/serate.java b/diuf/sudoku/test/serate.java index 1aad38a..c3aa3a6 100644 --- a/diuf/sudoku/test/serate.java +++ b/diuf/sudoku/test/serate.java @@ -282,7 +282,8 @@ public static void main(String[] args) { int batchSolving = 0; boolean islkSudokuBUG = true; //Fix to BUG algorithm by lkSudoku boolean islkSudokuURUL = true; //Fix to UR and UL algorithm by lkSudoku - char want = 0; + boolean isBlocks = true; //Blocks are y default enabled. Disable to transform into Latin square (LQ) + char want = 0; int arg; //long t; //not used anymore long tt = System.currentTimeMillis(); @@ -333,7 +334,9 @@ else if (s.equals("islkSudokuBUG")) else if (s.equals("islkSudokuURUL")) c = 'U'; else if (s.equals("showArguments")) - c = 'S'; + c = 'S'; + else if (s.equals("isBlocks")) + c = 'Q'; else if (s.equals("after")) c = 'a'; else if (s.equals("before")) @@ -369,7 +372,8 @@ else if (++arg < args.length) { case 'B': case 'N': case 'G': - case 'U': + case 'U': + case 'Q': case '~': if (v == null) usage(a, 1); @@ -441,6 +445,10 @@ else if (++arg < args.length) { islkSudokuURUL = Integer.parseInt(v) != 0 ? true : false; Settings.getInstance().setlkSudokuURUL(islkSudokuURUL);//0: islkSudokuURUL disabled //1:islkSudokuURUL enabled break; + case 'Q': + isBlocks = Integer.parseInt(v) != 0 ? true : false; + Settings.getInstance().setBlocks(isBlocks);//0: isBlocks disabled //1:isBlocks enabled + break; case '~': setTechniques(v, showArguments); break;