This commit is contained in:
@@ -88,6 +88,7 @@ public class SudokuSerializer {
|
||||
|
||||
jsonSudoku.put("cells", cellsJsonArray);
|
||||
jsonSudoku.put("blocks", blocksJsonArray);
|
||||
jsonSudoku.put("blockWidth", sudoku.getBlockWidth());
|
||||
jsonSudokus.put(i, jsonSudoku);
|
||||
}
|
||||
|
||||
@@ -151,7 +152,9 @@ public class SudokuSerializer {
|
||||
sudokuBlocks.add(blocks.get(blockID));
|
||||
}
|
||||
|
||||
sudokus.add(new Sudoku(sudokuCells, sudokuBlocks, null));
|
||||
Sudoku s = new Sudoku(sudokuCells, sudokuBlocks, null);
|
||||
s.setBlockWidth(sudokuJsonObject.getInt("blockWidth"));
|
||||
sudokus.add(s);
|
||||
}
|
||||
|
||||
return new MultiDoku(sudokus);
|
||||
|
||||
@@ -46,13 +46,19 @@ public class Sudoku {
|
||||
return index < getSize() * getSize();
|
||||
}
|
||||
|
||||
public boolean tryPlaceCellSymbol(int x, int y, int value) {
|
||||
assert (isValidCoords(x, y));
|
||||
public boolean canBePlaced(int x, int y, int value) {
|
||||
for (IConstraint constraint : this.constraints) {
|
||||
if (!constraint.canBePlaced(this, x, y, value)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean tryPlaceCellSymbol(int x, int y, int value) {
|
||||
assert (isValidCoords(x, y));
|
||||
if (!canBePlaced(x, y, value))
|
||||
return false;
|
||||
Cell cell = getCell(x, y);
|
||||
cell.setSymbolIndex(value);
|
||||
return true;
|
||||
@@ -293,4 +299,9 @@ public class Sudoku {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void setBlockWidth(int blockWidth) {
|
||||
this.blockWidth = blockWidth;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,17 +1,19 @@
|
||||
package sudoku.structure;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import sudoku.constraint.BlockConstraint;
|
||||
import sudoku.constraint.ColumnConstraint;
|
||||
import sudoku.constraint.IConstraint;
|
||||
import sudoku.constraint.LineConstraint;
|
||||
import sudoku.structure.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class SudokuFactory {
|
||||
|
||||
private static List<IConstraint> DEFAULT_CONSTRAINTS = Arrays.asList(new BlockConstraint(), new LineConstraint(), new ColumnConstraint());
|
||||
|
||||
private static List<Cell> initCells(int size) {
|
||||
List<Cell> cells = new ArrayList<>(size * size);
|
||||
for (int i = 0; i < size * size; i++) {
|
||||
@@ -44,17 +46,8 @@ public class SudokuFactory {
|
||||
}
|
||||
|
||||
public static MultiDoku createBasicEmptyRectangleSudoku(int widthBlock, int heightBlock) {
|
||||
int symbolCount = widthBlock * heightBlock;
|
||||
List<Cell> cases = initCells(symbolCount);
|
||||
List<Block> blocs = initRectangleBlocs(cases, widthBlock, heightBlock);
|
||||
List<IConstraint> constraints = new ArrayList<>();
|
||||
constraints.add(new ColumnConstraint());
|
||||
constraints.add(new LineConstraint());
|
||||
constraints.add(new BlockConstraint());
|
||||
Sudoku s = new Sudoku(cases, blocs, constraints);
|
||||
List<Sudoku> subSudoku = new ArrayList<>();
|
||||
subSudoku.add(s);
|
||||
return new MultiDoku(subSudoku);
|
||||
Sudoku s = createRectangleSudoku(widthBlock, heightBlock);
|
||||
return new MultiDoku(Arrays.asList(s));
|
||||
}
|
||||
|
||||
public static MultiDoku createBasicEmptySquareSudoku(int size) {
|
||||
@@ -72,4 +65,70 @@ public class SudokuFactory {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static Sudoku createRectangleSudoku(int width, int height) {
|
||||
int symbolCount = width * height;
|
||||
List<Cell> cases = initCells(symbolCount);
|
||||
List<Block> blocs = initRectangleBlocs(cases, width, height);
|
||||
Sudoku s = new Sudoku(cases, blocs, DEFAULT_CONSTRAINTS);
|
||||
s.setBlockWidth(width);
|
||||
return s;
|
||||
}
|
||||
|
||||
private static Sudoku createSquareSudoku(int size) {
|
||||
return createRectangleSudoku(size, size);
|
||||
}
|
||||
|
||||
private static void linkSquareSudokus(Sudoku sudoku1, Sudoku sudoku2, Coordinate offset) {
|
||||
int blockWidth = sudoku1.getBlockWidth();
|
||||
for (int dx = 0; dx < blockWidth; dx++) {
|
||||
for (int dy = 0; dy < blockWidth; dy++) {
|
||||
int block1X = dx + offset.getX();
|
||||
int block1Y = dy + offset.getY();
|
||||
int block2X = dx;
|
||||
int block2Y = dy;
|
||||
|
||||
if ((block1X < blockWidth) && (block1X >= 0) && (block1Y >= 0) && (block1Y < blockWidth)) {
|
||||
Block block1 = sudoku1.getBlocks().get(block1Y * blockWidth + block1X);
|
||||
Block block2 = sudoku2.getBlocks().get(block2Y * blockWidth + block2X);
|
||||
|
||||
// on remplace le bloc
|
||||
sudoku2.getBlocks().set(block2Y * blockWidth + block2X, block1);
|
||||
|
||||
// on remplace les cellules
|
||||
for (int i = 0; i < block1.getCells().size(); i++) {
|
||||
Cell newCell = block1.getCells().get(i);
|
||||
Cell oldCell = block2.getCells().get(i);
|
||||
|
||||
int oldCellIndex = sudoku2.getCells().indexOf(oldCell);
|
||||
sudoku2.getCells().set(oldCellIndex, newCell);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static MultiDoku createBasicSquareMultidoku(int size) {
|
||||
assert (size > 1);
|
||||
|
||||
/**
|
||||
* 2 3
|
||||
* 1
|
||||
* 4 5
|
||||
*/
|
||||
|
||||
Sudoku sudoku1 = createSquareSudoku(size);
|
||||
|
||||
Sudoku sudoku2 = createSquareSudoku(size);
|
||||
Sudoku sudoku3 = createSquareSudoku(size);
|
||||
Sudoku sudoku4 = createSquareSudoku(size);
|
||||
Sudoku sudoku5 = createSquareSudoku(size);
|
||||
|
||||
linkSquareSudokus(sudoku1, sudoku2, new Coordinate(1 - size, 1 - size));
|
||||
linkSquareSudokus(sudoku1, sudoku3, new Coordinate(size - 1, 1 - size));
|
||||
linkSquareSudokus(sudoku1, sudoku4, new Coordinate(1 - size, size - 1));
|
||||
linkSquareSudokus(sudoku1, sudoku5, new Coordinate(size - 1, size - 1));
|
||||
|
||||
return new MultiDoku(Arrays.asList(sudoku1, sudoku2, sudoku3, sudoku4, sudoku5));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user