begin multidoku display code

This commit is contained in:
2025-01-26 22:19:56 +01:00
parent 3bb610b9c2
commit 3a0279afe3
5 changed files with 190 additions and 25 deletions

View File

@@ -0,0 +1,134 @@
package gui;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import sudoku.structure.Block;
import sudoku.structure.Cell;
import sudoku.structure.Coordinate;
import sudoku.structure.MultiDoku;
import sudoku.structure.Sudoku;
public class RenderableMultidoku {
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) {
this.cells = cells;
this.blocks = blocks;
this.width = width;
this.height = height;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public List<Block> getBlocks() {
return this.blocks;
}
public Cell getCell(int x, int y) {
return getCell(y * width + x);
}
public Cell getCell(int index) {
return cells.get(index);
}
public boolean setCellValue(int x, int y, int value) {
return false;
}
private static record PositionConstraint(Sudoku sudoku1, Sudoku sudoku2, Coordinate offset) {
}
private static Coordinate getConstraint(Sudoku sudoku1, Sudoku sudoku2) {
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);
}
}
}
return null;
}
private static boolean hasContraint(Sudoku sudoku1, Sudoku sudoku2, List<PositionConstraint> positionConstraints) {
for (PositionConstraint constraint : positionConstraints) {
if ((constraint.sudoku1 == sudoku1 && constraint.sudoku2 == sudoku2)
|| (constraint.sudoku1 == sudoku2 && constraint.sudoku2 == sudoku1))
return true;
}
return false;
}
private static Coordinate getMinSudokuOffset(Map<Sudoku, Coordinate> sudokusOffset) {
// TODO
return null;
}
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());
}
Map<Sudoku, Coordinate> sudokusOffset = new HashMap<>();
List<PositionConstraint> positionConstraints = new ArrayList<>();
for (Sudoku sudoku1 : doku.getSubGrids()) {
for (Sudoku sudoku2 : doku.getSubGrids()) {
if (sudoku1 == sudoku2)
continue;
Coordinate constraint = getConstraint(sudoku1, sudoku2);
if (constraint != null && !hasContraint(sudoku1, sudoku2, positionConstraints)) {
positionConstraints.add(new PositionConstraint(sudoku1, sudoku2, constraint));
}
}
}
for (PositionConstraint constraint : positionConstraints) {
if (!sudokusOffset.containsKey(constraint.sudoku1) && !sudokusOffset.containsKey(constraint.sudoku2)) {
sudokusOffset.put(constraint.sudoku1, new Coordinate(0, 0));
sudokusOffset.put(constraint.sudoku2, constraint.offset());
}
if (sudokusOffset.containsKey(constraint.sudoku1)) {
sudokusOffset.put(constraint.sudoku2, constraint.offset.add(sudokusOffset.get(constraint.sudoku1)));
} else {
sudokusOffset.put(constraint.sudoku1, constraint.offset.add(sudokusOffset.get(constraint.sudoku2)));
}
}
Coordinate minCoordinate = getMinSudokuOffset(sudokusOffset);
for (var entry : sudokusOffset.entrySet()) {
entry.setValue(entry.getValue().sub(minCoordinate));
}
List<Block> blocks = new ArrayList<>();
List<Cell> cells = new ArrayList<>();
// TODO: dernière étape
return new RenderableMultidoku(blocks, cells, 0, 0);
}
}