This commit is contained in:
@@ -12,24 +12,33 @@ import sudoku.constraint.IConstraint;
|
|||||||
*/
|
*/
|
||||||
public class Sudoku {
|
public class Sudoku {
|
||||||
|
|
||||||
|
// <editor-fold defaultstate="collapsed" desc="ATTRIBUTS">
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Liste des Block contenus dans le Sudoku.
|
* Liste des Block contenus dans le Sudoku.
|
||||||
*/
|
*/
|
||||||
private final List<Block> blocks;
|
private final List<Block> blocks;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Liste des Cells contenus dans le Sudoku.
|
* Liste des Cells contenus dans le Sudoku.
|
||||||
*/
|
*/
|
||||||
private List<Cell> cells = new ArrayList<>();
|
private final List<Cell> cells;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Liste des contraintes (TODO) du Sudoku.
|
* Liste des contraintes (TODO) du Sudoku.
|
||||||
*/
|
*/
|
||||||
private final List<IConstraint> constraints;
|
private final List<IConstraint> constraints;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Largeur des Blocks s'ils sont rectangulaires, valant 0 si ce n'est pas le
|
* Largeur des Blocks s'ils sont rectangulaires,
|
||||||
* cas.
|
* valant 0 si ce n'est pas le cas.
|
||||||
*/
|
*/
|
||||||
private int blockWidth;
|
private int blockWidth;
|
||||||
|
|
||||||
|
// </editor-fold>
|
||||||
|
|
||||||
|
// <editor-fold defaultstate="collapsed" desc="METHODES">
|
||||||
|
|
||||||
public Sudoku(List<Cell> cells, List<Block> blocks, List<IConstraint> constraints) {
|
public Sudoku(List<Cell> cells, List<Block> blocks, List<IConstraint> constraints) {
|
||||||
this.cells = cells;
|
this.cells = cells;
|
||||||
this.blocks = blocks;
|
this.blocks = blocks;
|
||||||
@@ -97,31 +106,79 @@ public class Sudoku {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Vide la Cell dotn les coordonnées sont renseignées de son symbole.
|
* Vérifie si le Sudoku est résolue, soit complet et cohérent avec ses
|
||||||
*
|
* contraintes.
|
||||||
* @param x int, abscisse de la Cell voulue.
|
*
|
||||||
* @param y int, coordonnée de la Cell voulue.
|
* @return boolean, valant true si le Sudoku est résolu, false sinon.
|
||||||
*/
|
*/
|
||||||
|
public boolean isSolved() {
|
||||||
public void clearCell(int x, int y) {
|
boolean isComplete = isComplete();
|
||||||
assert (isValidCoords(x, y));
|
boolean isValid = isValid();
|
||||||
Cell cell = getCell(x, y);
|
return isComplete && isValid;
|
||||||
cell.setSymbolIndex(Cell.NOSYMBOL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Vide toutes les Cell du Sudoku.
|
* Vérifie que le Sudoku est complet, soit qu'il n'y ait aucune case vide.
|
||||||
|
*
|
||||||
|
* @return boolean, true si le Sudoku est complet, false sinon.
|
||||||
*/
|
*/
|
||||||
public void clear() {
|
private boolean isComplete() {
|
||||||
for (int i = 0; i < getSize() * getSize(); i++) {
|
return getFirstEmptyCell() == null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Vérifie si le Sudoku est valide, soit qu'il est cohérent avec ses
|
||||||
|
* contraintes.
|
||||||
|
*
|
||||||
|
* @return bollean, true si le Sudoku est valide, false sinon
|
||||||
|
*/
|
||||||
|
private boolean isValid() {
|
||||||
|
for (int i = 0; i < cells.size(); i++) {
|
||||||
Cell cell = getCell(i);
|
Cell cell = getCell(i);
|
||||||
if (cell.isMutable())
|
if (cell.isEmpty())
|
||||||
cell.setSymbolIndex(Cell.NOSYMBOL);
|
continue;
|
||||||
|
|
||||||
|
Coordinate coordinate = toCoords(i);
|
||||||
|
|
||||||
|
int symbolPlaced = cell.empty();
|
||||||
|
if (!canBePlaced(coordinate.getX(), coordinate.getY(), symbolPlaced)) {
|
||||||
|
cell.setSymbolIndex(symbolPlaced);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
cell.setSymbolIndex(symbolPlaced);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Renvoie la Cell aux coordonées données.
|
||||||
|
*
|
||||||
|
* @param x int, abscisse.
|
||||||
|
* @param y int, ordonnée.
|
||||||
|
* @return Cell, si une Cell existe aux coordonnées données, null sinon.
|
||||||
|
*/
|
||||||
|
public Cell getCell(int x, int y) {
|
||||||
|
int index = toIndex(x, y);
|
||||||
|
assert (isValidCoords(x, y));
|
||||||
|
try {
|
||||||
|
return this.cells.get(index);
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getBlockWidth() {
|
/**
|
||||||
return blockWidth;
|
* Renvoie la 1re Cell vide du Sudoku.
|
||||||
|
*
|
||||||
|
* @return Cell, une Cell vide, ou null s'il n'y en a pas.
|
||||||
|
*/
|
||||||
|
public Cell getFirstEmptyCell() {
|
||||||
|
for (Cell cell : this.cells) {
|
||||||
|
if (cell.isEmpty()) {
|
||||||
|
return cell;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -189,24 +246,14 @@ public class Sudoku {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Cell getCell(int x, int y) {
|
public boolean hasConstraint(Constraint constraint) {
|
||||||
int index = toIndex(x, y);
|
return this.constraints.contains(constraint.getConstraint());
|
||||||
assert (isValidCoords(x, y));
|
|
||||||
try {
|
|
||||||
return this.cells.get(index);
|
|
||||||
} catch (IndexOutOfBoundsException e) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Cell getCell(int i) {
|
public Cell getCell(int i) {
|
||||||
return this.cells.get(i);
|
return this.cells.get(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<IConstraint> getConstraints() {
|
|
||||||
return constraints;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getSize() {
|
public int getSize() {
|
||||||
return this.blocks.size();
|
return this.blocks.size();
|
||||||
}
|
}
|
||||||
@@ -219,16 +266,19 @@ public class Sudoku {
|
|||||||
return this.blocks;
|
return this.blocks;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public List<IConstraint> getConstraints() {
|
||||||
* Vérifie si une Cell appartient au Sudoku.
|
return constraints;
|
||||||
*
|
|
||||||
* @param cell Cell, cellule dont on veut vérifier l'appartenance au Sudoku.
|
|
||||||
* @return boolean, vaut true si la Cell appartient au Sudoku.
|
|
||||||
*/
|
|
||||||
public boolean contains(Cell cell) {
|
|
||||||
return this.cells.contains(cell);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getBlockWidth() {
|
||||||
|
return blockWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBlockWidth(int blockWidth) {
|
||||||
|
this.blockWidth = blockWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
sb.append("Sudoku {");
|
sb.append("Sudoku {");
|
||||||
@@ -243,71 +293,6 @@ public class Sudoku {
|
|||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// <editor-fold defaultstate="collapsed" desc="METHODES">
|
||||||
* Renvoie la 1re Cell vide du Sudoku.
|
|
||||||
*
|
|
||||||
* @return Cell, une Cell vide, ou null s'il n'y en a pas.
|
|
||||||
*/
|
|
||||||
public Cell getFirstEmptyCell() {
|
|
||||||
for (Cell cell : this.cells) {
|
|
||||||
if (cell.isEmpty()) {
|
|
||||||
return cell;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Vérifie si le Sudoku est résolue, soit complet et cohérent avec ses
|
|
||||||
* contraintes.
|
|
||||||
*
|
|
||||||
* @return boolean, valant true si le Sudoku est résolu, false sinon.
|
|
||||||
*/
|
|
||||||
public boolean isSolved() {
|
|
||||||
boolean isComplete = isComplete();
|
|
||||||
boolean isValid = isValid();
|
|
||||||
return isComplete && isValid;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Vérifie que le Sudoku est complet, soit qu'il n'y ait aucune case vide.
|
|
||||||
*
|
|
||||||
* @return boolean, true si le Sudoku est complet, false sinon.
|
|
||||||
*/
|
|
||||||
private boolean isComplete() {
|
|
||||||
return getFirstEmptyCell() == null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Vérifie si le Sudoku est valide, soit qu'il est cohérent avec ses
|
|
||||||
* contraintes.
|
|
||||||
*
|
|
||||||
* @return bollean, true si le Sudoku est valide, false sinon
|
|
||||||
*/
|
|
||||||
private boolean isValid() {
|
|
||||||
for (int i = 0; i < cells.size(); i++) {
|
|
||||||
Cell cell = getCell(i);
|
|
||||||
if (cell.isEmpty())
|
|
||||||
continue;
|
|
||||||
|
|
||||||
Coordinate coordinate = toCoords(i);
|
|
||||||
|
|
||||||
int symbolPlaced = cell.empty();
|
|
||||||
if (!canBePlaced(coordinate.getX(), coordinate.getY(), symbolPlaced)) {
|
|
||||||
cell.setSymbolIndex(symbolPlaced);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
cell.setSymbolIndex(symbolPlaced);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setBlockWidth(int blockWidth) {
|
|
||||||
this.blockWidth = blockWidth;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean hasConstraint(Constraint constraint) {
|
|
||||||
return this.constraints.contains(constraint.getConstraint());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user