This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
package sudoku;
|
||||
|
||||
import sudoku.constraint.IConstraint;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -10,13 +13,34 @@ import java.util.List;
|
||||
public class MultiDoku {
|
||||
|
||||
private final List<Sudoku> subGrids;
|
||||
private final List<Sudoku> subGrids;
|
||||
|
||||
public MultiDoku(List<Sudoku> subGrids) {
|
||||
this.subGrids = subGrids;
|
||||
}
|
||||
|
||||
Sudoku getSubGrid(int index) {
|
||||
return this.subGrids.get(index);
|
||||
public int getNbSubGrids(){
|
||||
return subGrids.size();
|
||||
}
|
||||
|
||||
public Sudoku getSubGrid(int i){
|
||||
return subGrids.get(i);
|
||||
}
|
||||
|
||||
public boolean isValid(List<IConstraint> constraints){
|
||||
for (Sudoku sudoku : subGrids){
|
||||
if (!sudoku.isValid(constraints))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public List<MutableCell> getMutableCells(){
|
||||
List<MutableCell> mutableCells = new ArrayList<>();
|
||||
for (Sudoku sudoku : subGrids){
|
||||
mutableCells.addAll(sudoku.getMutableCells());
|
||||
}
|
||||
return mutableCells;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -25,6 +25,11 @@ public class MutableCell extends Cell{
|
||||
setSymboleIndex(NOSYMBOLE);
|
||||
}
|
||||
|
||||
public void clear(int indexSymbol) {
|
||||
hintsSymbolIndex.remove(indexSymbol);
|
||||
this.clear();
|
||||
}
|
||||
|
||||
public List<Integer> getHints() {
|
||||
return this.hintsSymbolIndex;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
package sudoku;
|
||||
|
||||
import sudoku.constraint.IConstraint;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -22,8 +26,27 @@ public class Sudoku {
|
||||
return this.cells.get(index);
|
||||
}
|
||||
|
||||
public Cell getCell(int i) {
|
||||
return this.cells.get(i);
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return this.blocks.size();
|
||||
}
|
||||
|
||||
public Boolean isValid(List<IConstraint> constraints) {
|
||||
//not implemented
|
||||
//for eachcase check contraintes
|
||||
throw new Error("Function isValid() not implemented");
|
||||
}
|
||||
|
||||
public List<MutableCell> getMutableCells() {
|
||||
List<MutableCell> mutableCells = new ArrayList<>();
|
||||
for (Cell cell : this.cells) {
|
||||
if (cell instanceof MutableCell){
|
||||
mutableCells.add((MutableCell) cell);
|
||||
}
|
||||
}
|
||||
return mutableCells;
|
||||
}
|
||||
}
|
||||
|
||||
39
app/src/main/java/sudoku/solver/Solver.java
Normal file
39
app/src/main/java/sudoku/solver/Solver.java
Normal file
@@ -0,0 +1,39 @@
|
||||
package sudoku.solver;
|
||||
|
||||
import sudoku.Cell;
|
||||
import sudoku.MultiDoku;
|
||||
import sudoku.MutableCell;
|
||||
import sudoku.Sudoku;
|
||||
import sudoku.constraint.IConstraint;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
public class Solver {
|
||||
Caretaker stack;
|
||||
|
||||
public Solver() {
|
||||
}
|
||||
|
||||
public MultiDoku solve(MultiDoku doku, List<IConstraint> constraints) throws Exception {
|
||||
if (!doku.isValid(constraints)) {
|
||||
throw new Exception("Invalid doku");
|
||||
}
|
||||
List<MutableCell> allMutableCells = doku.getMutableCells();
|
||||
List<MutableCell> remainingCellsToCheck = new ArrayList<>(allMutableCells);
|
||||
Random rand = new Random();
|
||||
while (!remainingCellsToCheck.isEmpty()) {
|
||||
int indexCurrentCell = rand.nextInt(remainingCellsToCheck.size());
|
||||
MutableCell currentCell = remainingCellsToCheck.get(indexCurrentCell);
|
||||
if (currentCell.getHints().isEmpty()){
|
||||
MutableCell modify = allMutableCells.get(stack.undo());
|
||||
modify.clear(modify.getSymboleIndex());
|
||||
}
|
||||
|
||||
|
||||
remainingCellsToCheck.remove(indexCurrentCell);
|
||||
}
|
||||
return doku;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user