feat: render multidokus
Some checks are pending
Linux arm64 / Build (push) Waiting to run

This commit is contained in:
2025-01-28 08:55:58 +01:00
parent e9a77d9826
commit 182f79d4b4
6 changed files with 146 additions and 46 deletions

View File

@@ -13,16 +13,18 @@ import sudoku.structure.Sudoku;
public class RenderableMultidoku {
private final MultiDoku doku;
private final List<Cell> cells;
private final List<Block> blocks;
private final int width;
private final int height;
public RenderableMultidoku(List<Block> blocks, List<Cell> cells, int width, int height) {
private RenderableMultidoku(MultiDoku doku, List<Block> blocks, List<Cell> cells, int width, int height) {
this.cells = cells;
this.blocks = blocks;
this.width = width;
this.height = height;
this.doku = doku;
}
public int getWidth() {
@@ -45,24 +47,39 @@ public class RenderableMultidoku {
return cells.get(index);
}
public boolean setCellValue(int x, int y, int value) {
return false;
public boolean setCellValue(Cell cell, int value) {
// TODO: fix constraints
// for (Sudoku s : doku.getSubGrids()) {
// int cellIndex = s.getCells().indexOf(cell);
// // la cellule existe
// if (cellIndex != -1) {
// int cellX = cellIndex % s.getSize();
// int cellY = cellIndex / s.getSize();
// if (s.canBePlaced(cellX, cellY, value)) {
// return false;
// }
// }
// }
cell.setSymbolIndex(value);
return true;
}
private static record PositionConstraint(Sudoku sudoku1, Sudoku sudoku2, Coordinate offset) {
}
private static Coordinate getConstraint(Sudoku sudoku1, Sudoku sudoku2) {
int blockWidth = sudoku1.getBlockWidth();
int blockHeight = sudoku1.getSize() / blockWidth;
for (int i = 0; i < sudoku1.getSize(); i++) {
for (int j = 0; j < sudoku2.getSize(); j++) {
Block block1 = sudoku1.getBlocks().get(i);
Block block2 = sudoku2.getBlocks().get(j);
if (block1 == block2) {
int block1X = 0;
int block1Y = 0;
int block2X = 0;
int block2Y = 0;
return new Coordinate(block1X - block2X, block1Y - block2Y);
int block1X = i % blockHeight;
int block1Y = i / blockHeight;
int block2X = j % blockHeight;
int block2Y = j / blockHeight;
return new Coordinate((block1X - block2X) * blockWidth, (block1Y - block2Y) * blockHeight);
}
}
}
@@ -111,15 +128,16 @@ public class RenderableMultidoku {
int blockWidth = maxSudoku.getBlockWidth();
int blockHeight = maxSudoku.getSize() / blockWidth;
return new Coordinate((maxCoordinate.getX() + blockHeight) * blockWidth, (maxCoordinate.getY() + blockWidth) * blockHeight);
return new Coordinate(maxCoordinate.getX() + maxSudoku.getSize(), maxCoordinate.getY() + maxSudoku.getSize());
}
public static RenderableMultidoku fromMultidoku(MultiDoku doku) {
if (doku.getNbSubGrids() == 1) {
Sudoku sudoku = doku.getSubGrid(0);
return new RenderableMultidoku(sudoku.getBlocks(), sudoku.getCells(), sudoku.getSize(), sudoku.getSize());
return new RenderableMultidoku(doku, sudoku.getBlocks(), sudoku.getCells(), sudoku.getSize(), sudoku.getSize());
}
Map<Sudoku, Coordinate> sudokusOffset = new HashMap<>();
// coordinates in cell
List<PositionConstraint> positionConstraints = new ArrayList<>();
for (Sudoku sudoku1 : doku.getSubGrids()) {
for (Sudoku sudoku2 : doku.getSubGrids()) {
@@ -154,24 +172,35 @@ public class RenderableMultidoku {
entry.setValue(entry.getValue().sub(minCoordinate));
}
List<Block> blocks = new ArrayList<>();
List<Cell> cells = new ArrayList<>();
Coordinate maxCoordinate = getMaxSudokuCoordinate(sudokusOffset);
List<Block> blocks = new ArrayList<>();
List<Cell> cells = new ArrayList<>(maxCoordinate.getX() * maxCoordinate.getY());
for (int i = 0; i < maxCoordinate.getX() * maxCoordinate.getY(); i++) {
cells.add(null);
}
for (var entry : sudokusOffset.entrySet()) {
Sudoku sudoku = entry.getKey();
Coordinate offset = entry.getValue();
for (int x = 0; x < sudoku.getSize(); x++) {
for (int y = 0; y < sudoku.getSize(); y++) {
Cell cell = sudoku.getCell(x, y);
int absoluteX = x + offset.getX();
int absoluteY = y + offset.getY();
cells.set(absoluteY * maxCoordinate.getX() + absoluteX, cell);
}
}
for (Sudoku sudoku : doku.getSubGrids()) {
for (Block block : sudoku.getBlocks()) {
if (!blocks.contains(block)) {
blocks.add(block);
}
}
for (Cell cell : sudoku.getCells()) {
if (!cells.contains(cell))
cells.add(cell);
}
}
Coordinate maxCoordinate = getMaxSudokuCoordinate(sudokusOffset);
return new RenderableMultidoku(blocks, cells, maxCoordinate.getX(), maxCoordinate.getY());
return new RenderableMultidoku(doku, blocks, cells, maxCoordinate.getX(), maxCoordinate.getY());
}
}