This commit is contained in:
75
app/src/main/java/sudoku/structure/SudokuFactory.java
Normal file
75
app/src/main/java/sudoku/structure/SudokuFactory.java
Normal file
@@ -0,0 +1,75 @@
|
||||
package sudoku.structure;
|
||||
|
||||
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<Cell> initCells(int size) {
|
||||
List<Cell> cells = new ArrayList<>(size * size);
|
||||
for (int i = 0; i < size * size; i++) {
|
||||
cells.add(new Cell());
|
||||
}
|
||||
return cells;
|
||||
}
|
||||
|
||||
private static List<Block> initRectangleBlocs(List<Cell> cells, int width, int height) {
|
||||
List<Block> blocs = new ArrayList<>();
|
||||
int size = width * height;
|
||||
for (int i = 0; i < size; i++) {
|
||||
Block newBlock = new Block();
|
||||
int blockX = i % height;
|
||||
int blockY = i / height;
|
||||
for (int y = 0; y < height; y++) {
|
||||
for (int x = 0; x < width; x++) {
|
||||
int index = ((y + blockY * height) * size + (x + blockX * width));
|
||||
Cell blockCell = cells.get(index);
|
||||
blockCell.setBlock(newBlock);
|
||||
// List<Block> blockContainers = new ArrayList<>();
|
||||
// blockContainers.add(newBlock);
|
||||
// blockCell.setBlockContainers(blockContainers);
|
||||
newBlock.addCell(blockCell);
|
||||
}
|
||||
}
|
||||
blocs.add(newBlock);
|
||||
}
|
||||
return blocs;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public static MultiDoku createBasicEmptySquareSudoku(int size) {
|
||||
return createBasicEmptyRectangleSudoku(size, size);
|
||||
}
|
||||
|
||||
public static void setIMMutableCells(MultiDoku doku, Map<Coordinate, Integer> immutableCells) {
|
||||
immutableCells.forEach((coordinate, symbol) -> {
|
||||
for (Sudoku sudoku : doku.getSubGrids()) {
|
||||
Cell cell = sudoku.getCell(coordinate.getX(), coordinate.getY());
|
||||
if (cell != null) {
|
||||
cell.setSymbolIndex(symbol);
|
||||
cell.setImmutable();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user