This commit is contained in:
@@ -13,16 +13,18 @@ import sudoku.structure.Sudoku;
|
|||||||
|
|
||||||
public class RenderableMultidoku {
|
public class RenderableMultidoku {
|
||||||
|
|
||||||
|
private final MultiDoku doku;
|
||||||
private final List<Cell> cells;
|
private final List<Cell> cells;
|
||||||
private final List<Block> blocks;
|
private final List<Block> blocks;
|
||||||
private final int width;
|
private final int width;
|
||||||
private final int height;
|
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.cells = cells;
|
||||||
this.blocks = blocks;
|
this.blocks = blocks;
|
||||||
this.width = width;
|
this.width = width;
|
||||||
this.height = height;
|
this.height = height;
|
||||||
|
this.doku = doku;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getWidth() {
|
public int getWidth() {
|
||||||
@@ -45,24 +47,39 @@ public class RenderableMultidoku {
|
|||||||
return cells.get(index);
|
return cells.get(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean setCellValue(int x, int y, int value) {
|
public boolean setCellValue(Cell cell, int value) {
|
||||||
return false;
|
// 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 record PositionConstraint(Sudoku sudoku1, Sudoku sudoku2, Coordinate offset) {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Coordinate getConstraint(Sudoku sudoku1, Sudoku sudoku2) {
|
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 i = 0; i < sudoku1.getSize(); i++) {
|
||||||
for (int j = 0; j < sudoku2.getSize(); j++) {
|
for (int j = 0; j < sudoku2.getSize(); j++) {
|
||||||
Block block1 = sudoku1.getBlocks().get(i);
|
Block block1 = sudoku1.getBlocks().get(i);
|
||||||
Block block2 = sudoku2.getBlocks().get(j);
|
Block block2 = sudoku2.getBlocks().get(j);
|
||||||
if (block1 == block2) {
|
if (block1 == block2) {
|
||||||
int block1X = 0;
|
int block1X = i % blockHeight;
|
||||||
int block1Y = 0;
|
int block1Y = i / blockHeight;
|
||||||
int block2X = 0;
|
int block2X = j % blockHeight;
|
||||||
int block2Y = 0;
|
int block2Y = j / blockHeight;
|
||||||
return new Coordinate(block1X - block2X, block1Y - block2Y);
|
return new Coordinate((block1X - block2X) * blockWidth, (block1Y - block2Y) * blockHeight);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -111,15 +128,16 @@ public class RenderableMultidoku {
|
|||||||
int blockWidth = maxSudoku.getBlockWidth();
|
int blockWidth = maxSudoku.getBlockWidth();
|
||||||
int blockHeight = maxSudoku.getSize() / blockWidth;
|
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) {
|
public static RenderableMultidoku fromMultidoku(MultiDoku doku) {
|
||||||
if (doku.getNbSubGrids() == 1) {
|
if (doku.getNbSubGrids() == 1) {
|
||||||
Sudoku sudoku = doku.getSubGrid(0);
|
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<>();
|
Map<Sudoku, Coordinate> sudokusOffset = new HashMap<>();
|
||||||
|
// coordinates in cell
|
||||||
List<PositionConstraint> positionConstraints = new ArrayList<>();
|
List<PositionConstraint> positionConstraints = new ArrayList<>();
|
||||||
for (Sudoku sudoku1 : doku.getSubGrids()) {
|
for (Sudoku sudoku1 : doku.getSubGrids()) {
|
||||||
for (Sudoku sudoku2 : doku.getSubGrids()) {
|
for (Sudoku sudoku2 : doku.getSubGrids()) {
|
||||||
@@ -154,24 +172,35 @@ public class RenderableMultidoku {
|
|||||||
entry.setValue(entry.getValue().sub(minCoordinate));
|
entry.setValue(entry.getValue().sub(minCoordinate));
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Block> blocks = new ArrayList<>();
|
Coordinate maxCoordinate = getMaxSudokuCoordinate(sudokusOffset);
|
||||||
List<Cell> cells = new ArrayList<>();
|
|
||||||
|
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()) {
|
for (Block block : sudoku.getBlocks()) {
|
||||||
if (!blocks.contains(block)) {
|
if (!blocks.contains(block)) {
|
||||||
blocks.add(block);
|
blocks.add(block);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (Cell cell : sudoku.getCells()) {
|
|
||||||
if (!cells.contains(cell))
|
|
||||||
cells.add(cell);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Coordinate maxCoordinate = getMaxSudokuCoordinate(sudokusOffset);
|
return new RenderableMultidoku(doku, blocks, cells, maxCoordinate.getX(), maxCoordinate.getY());
|
||||||
|
|
||||||
return new RenderableMultidoku(blocks, cells, maxCoordinate.getX(), maxCoordinate.getY());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ import sudoku.structure.MultiDoku;
|
|||||||
public class SudokuRenderer {
|
public class SudokuRenderer {
|
||||||
|
|
||||||
private final RenderableMultidoku doku;
|
private final RenderableMultidoku doku;
|
||||||
private int currentIndex = -1;
|
private Cell currentCell = null;
|
||||||
private final Map<Block, Color> colorPalette;
|
private final Map<Block, Color> colorPalette;
|
||||||
|
|
||||||
public SudokuRenderer(MultiDoku doku) {
|
public SudokuRenderer(MultiDoku doku) {
|
||||||
@@ -38,15 +38,13 @@ public class SudokuRenderer {
|
|||||||
|
|
||||||
private void renderPopup() {
|
private void renderPopup() {
|
||||||
if (ImGui.beginPopup("editPopup")) {
|
if (ImGui.beginPopup("editPopup")) {
|
||||||
Cell cell = doku.getCell(currentIndex);
|
Block block = currentCell.getBlock();
|
||||||
Block block = cell.getBlock();
|
|
||||||
int symbolCount = block.getCells().size();
|
int symbolCount = block.getCells().size();
|
||||||
for (int i = 1; i < symbolCount + 1; i++) {
|
for (int i = 1; i < symbolCount + 1; i++) {
|
||||||
if (i % (int) (Math.sqrt(symbolCount)) != 1)
|
if (i % (int) (Math.sqrt(symbolCount)) != 1)
|
||||||
ImGui.sameLine();
|
ImGui.sameLine();
|
||||||
if (ImGui.button(Integer.toString(i), new ImVec2(50, 50))) {
|
if (ImGui.button(Integer.toString(i), new ImVec2(50, 50))) {
|
||||||
this.doku.setCellValue(currentIndex % symbolCount,
|
this.doku.setCellValue(currentCell, i - 1);
|
||||||
currentIndex / symbolCount, i - 1);
|
|
||||||
ImGui.closeCurrentPopup();
|
ImGui.closeCurrentPopup();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -76,7 +74,7 @@ public class SudokuRenderer {
|
|||||||
cellText += Integer.toString(symbol + 1);
|
cellText += Integer.toString(symbol + 1);
|
||||||
if (ImGui.button(cellText + "##" + index, new ImVec2(50, 50))) {
|
if (ImGui.button(cellText + "##" + index, new ImVec2(50, 50))) {
|
||||||
ImGui.openPopup("editPopup");
|
ImGui.openPopup("editPopup");
|
||||||
currentIndex = index;
|
currentCell = cell;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ImGui.popStyleColor();
|
ImGui.popStyleColor();
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ public class MultiPlayerView extends BaseView {
|
|||||||
} else {
|
} else {
|
||||||
if (ImGui.button("Démarrer")) {
|
if (ImGui.button("Démarrer")) {
|
||||||
// temp
|
// temp
|
||||||
MultiDoku doku = SudokuFactory.createBasicEmptySquareSudoku(5);
|
MultiDoku doku = SudokuFactory.createBasicSquareMultidoku(3);
|
||||||
this.server.startGame(doku);
|
this.server.startGame(doku);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -88,6 +88,7 @@ public class SudokuSerializer {
|
|||||||
|
|
||||||
jsonSudoku.put("cells", cellsJsonArray);
|
jsonSudoku.put("cells", cellsJsonArray);
|
||||||
jsonSudoku.put("blocks", blocksJsonArray);
|
jsonSudoku.put("blocks", blocksJsonArray);
|
||||||
|
jsonSudoku.put("blockWidth", sudoku.getBlockWidth());
|
||||||
jsonSudokus.put(i, jsonSudoku);
|
jsonSudokus.put(i, jsonSudoku);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -151,7 +152,9 @@ public class SudokuSerializer {
|
|||||||
sudokuBlocks.add(blocks.get(blockID));
|
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);
|
return new MultiDoku(sudokus);
|
||||||
|
|||||||
@@ -46,13 +46,19 @@ public class Sudoku {
|
|||||||
return index < getSize() * getSize();
|
return index < getSize() * getSize();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean tryPlaceCellSymbol(int x, int y, int value) {
|
public boolean canBePlaced(int x, int y, int value) {
|
||||||
assert (isValidCoords(x, y));
|
|
||||||
for (IConstraint constraint : this.constraints) {
|
for (IConstraint constraint : this.constraints) {
|
||||||
if (!constraint.canBePlaced(this, x, y, value)) {
|
if (!constraint.canBePlaced(this, x, y, value)) {
|
||||||
return false;
|
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 cell = getCell(x, y);
|
||||||
cell.setSymbolIndex(value);
|
cell.setSymbolIndex(value);
|
||||||
return true;
|
return true;
|
||||||
@@ -293,4 +299,9 @@ public class Sudoku {
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setBlockWidth(int blockWidth) {
|
||||||
|
this.blockWidth = blockWidth;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,19 @@
|
|||||||
package sudoku.structure;
|
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.BlockConstraint;
|
||||||
import sudoku.constraint.ColumnConstraint;
|
import sudoku.constraint.ColumnConstraint;
|
||||||
import sudoku.constraint.IConstraint;
|
import sudoku.constraint.IConstraint;
|
||||||
import sudoku.constraint.LineConstraint;
|
import sudoku.constraint.LineConstraint;
|
||||||
import sudoku.structure.*;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class SudokuFactory {
|
public class SudokuFactory {
|
||||||
|
|
||||||
|
private static List<IConstraint> DEFAULT_CONSTRAINTS = Arrays.asList(new BlockConstraint(), new LineConstraint(), new ColumnConstraint());
|
||||||
|
|
||||||
private static List<Cell> initCells(int size) {
|
private static List<Cell> initCells(int size) {
|
||||||
List<Cell> cells = new ArrayList<>(size * size);
|
List<Cell> cells = new ArrayList<>(size * size);
|
||||||
for (int i = 0; i < size * size; i++) {
|
for (int i = 0; i < size * size; i++) {
|
||||||
@@ -44,17 +46,8 @@ public class SudokuFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static MultiDoku createBasicEmptyRectangleSudoku(int widthBlock, int heightBlock) {
|
public static MultiDoku createBasicEmptyRectangleSudoku(int widthBlock, int heightBlock) {
|
||||||
int symbolCount = widthBlock * heightBlock;
|
Sudoku s = createRectangleSudoku(widthBlock, heightBlock);
|
||||||
List<Cell> cases = initCells(symbolCount);
|
return new MultiDoku(Arrays.asList(s));
|
||||||
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) {
|
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