fix: serialize

This commit is contained in:
2025-01-30 00:51:22 +01:00
committed by Melvyn
parent f1d963e546
commit c4becf2d55
11 changed files with 212 additions and 415 deletions

View File

@@ -24,85 +24,57 @@ public class MultiDoku {
}
public MultiDoku clone() {
//TODO: ahhhhhhhhhhhhhhhhhhhhhhh
// TODO: ahhhhhhhhhhhhhhhhhhhhhhh
return SudokuSerializer.deserializeSudoku(SudokuSerializer.serializeSudoku(this));
}
/**
* Renvoie le nombre de sudoku contenu dans ce MultiDoku.
*
* @return int
*/
public int getNbSubGrids(){
public int getNbSubGrids() {
return subGrids.size();
}
/**
* Renvoie la ie sudoku contenue dans ce MultiDoku.
*
* @param i int, indice du sudoku à renvoyer.
* @return Sudoku, ie Sudoku
*/
public Sudoku getSubGrid(int i){
public Sudoku getSubGrid(int i) {
return subGrids.get(i);
}
/**
* Renvoie la liste des Cells contenue dans ce MultiDoku,
* soit les Cells contenues de chaques sous-Sudoku.
*
* @return List<Cell>
*/
public List<Cell> getCells(){
public List<Cell> getCells() {
Set<Cell> cellsSet = new HashSet<>();
for (Sudoku sudoku : subGrids){
for (Sudoku sudoku : subGrids) {
cellsSet.addAll(sudoku.getCells());
}
return new ArrayList<>(cellsSet);
}
/**
* Met à jour les symboles possibles de chaque Cell.
* @throws Exception, si ce n'est pas possible.
*/
public void updateSymbolsPossibilities() throws Exception {
for (Sudoku sudoku : subGrids){
sudoku.updateSymbolsPossibilities();
}
return new ArrayList<>(cellsSet);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Multidoku {");
for (Sudoku sudoku : subGrids){
for (Sudoku sudoku : subGrids) {
sb.append("\n\t").append(sudoku.toString());
}
sb.append("\n}");
return sb.toString();
}
/**
* Renvoie les symboles possibles d'une Cell donnée.
* @param cellToFill Cell.
* @return List<Integer>, liste des symboles possible.
*/
public List<Integer> getPossibleSymbolsOfCell(Cell cellToFill) {
List<Integer> result = new ArrayList<>();
boolean hasBeenFill = false;
for (Sudoku sudoku : this.subGrids) {
if (sudoku.contains(cellToFill)) {
if (!hasBeenFill) {
result.addAll(sudoku.getPossibleSymbolsOfCell(cellToFill));
hasBeenFill = true;
} else {
result.retainAll(sudoku.getPossibleSymbolsOfCell(cellToFill));
}
}
}
return result;
}
/**
* Renvoie les sous-Sudoku
*
* @return List<Sudoku>
*/
public List<Sudoku> getSubGrids() {
@@ -111,43 +83,26 @@ public class MultiDoku {
/**
* Check si le MultiDoku est valide, en fonction de ses sous-Sudokus.
*
* @return boolean, true s'il est valide et false sinon.
*/
public boolean isSolved() {
boolean result = true;
for (Sudoku sudoku : this.subGrids) {
result = sudoku.isSolved() && result;
}
return result;
}
@Override
public boolean equals(Object object) {
if (!(object instanceof MultiDoku)) {
return false;
}
if (this.getNbSubGrids() != ((MultiDoku) object).getNbSubGrids()) {
return false;
}
for (int i = 0; i < this.getNbSubGrids(); i++) {
if (!this.getSubGrid(i).equals(((MultiDoku) object).getSubGrid(i))) {
if (!sudoku.isSolved())
return false;
}
}
return true;
}
/**
* Renvoie la 1re Cell vide des sous-Sudoku.
*
* @return Cell, une Cell vide, ou null s'il n'y en a pas.
*/
public Cell getFirstEmptyCell() {
for (Sudoku sudoku : this.subGrids) {
Cell cellTmp = sudoku.getFirstEmptyCell();
if (cellTmp != null && cellTmp.isEmpty()) {
if (cellTmp != null) {
return cellTmp;
}
}
@@ -156,11 +111,12 @@ public class MultiDoku {
/**
* Renvoie la liste des Cells préalablement remplies du MultiDoku.
*
* @return List<Cell>, vide si aucune Cell n'est remplie.
*/
public List<Cell> getFilledCells() {
List<Cell> result = new ArrayList<>();
for (Cell cell : this.getCells()){
for (Cell cell : this.getCells()) {
if (!cell.isEmpty()) {
result.add(cell);
}
@@ -170,11 +126,12 @@ public class MultiDoku {
/**
* Renvoie la liste des Cells vides du MultiDoku.
*
* @return List<Cell>, vide si aucune Cell ne l'est.
*/
public List<Cell> getEmptyCells() {
List<Cell> result = new ArrayList<>();
for (Cell cell : this.getCells()){
for (Cell cell : this.getCells()) {
if (cell.isEmpty()) {
result.add(cell);
}
@@ -182,9 +139,9 @@ public class MultiDoku {
return result;
}
/**
* Vide une Cell donnée.
*
* @param cell Cell, à vider.
*/
public void empty(Cell cell) {
@@ -195,6 +152,7 @@ public class MultiDoku {
/**
* Renvoie le nombre de Cell contenue dans le MultiDoku.
*
* @return int, nombre de Cell dans le MultiDoku.
*/
public int getNbCells() {
@@ -210,4 +168,3 @@ public class MultiDoku {
}
}
}