feat: implement constraint classes for Sudoku (row, column, block)
This commit is contained in:
5
app/src/main/java/sudoku/Contrainte.java
Normal file
5
app/src/main/java/sudoku/Contrainte.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package sudoku;
|
||||
|
||||
public interface Contrainte {
|
||||
boolean estRespectee(Grille grille, Case c);
|
||||
}
|
||||
24
app/src/main/java/sudoku/ContrainteBloc.java
Normal file
24
app/src/main/java/sudoku/ContrainteBloc.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package sudoku;
|
||||
|
||||
import sudoku.core.Console;
|
||||
|
||||
public class ContrainteBloc implements Contrainte {
|
||||
@Override
|
||||
public boolean estRespectee(Grille grille, Case c) {
|
||||
int blocSize = (int) Math.sqrt(grille.getTaille());
|
||||
int startRow = (c.getLigne() / blocSize) * blocSize;
|
||||
int startCol = (c.getColonne() / blocSize) * blocSize;
|
||||
Symbole symbole = c.getSymbole();
|
||||
|
||||
for (int i = 0; i < blocSize; i++) {
|
||||
for (int j = 0; j < blocSize; j++) {
|
||||
Case currentCase = grille.getCase(startRow + i, startCol + j);
|
||||
if (currentCase != c && currentCase.getSymbole() != null && currentCase.getSymbole().equals(symbole)) {
|
||||
Console.errorln("La contrainte de bloc n'est pas respectee: ligne=" + (startRow + i) + ", col=" + (startCol + j) + ", symbole=" + symbole);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
19
app/src/main/java/sudoku/ContrainteColonne.java
Normal file
19
app/src/main/java/sudoku/ContrainteColonne.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package sudoku;
|
||||
|
||||
import sudoku.core.Console;
|
||||
|
||||
public class ContrainteColonne implements Contrainte {
|
||||
@Override
|
||||
public boolean estRespectee(Grille grille, Case c) {
|
||||
int colonne = c.getColonne();
|
||||
Symbole symbole = c.getSymbole();
|
||||
for (int ligne = 0; ligne < grille.getTaille(); ligne++) {
|
||||
Case currentCase = grille.getCase(ligne, colonne);
|
||||
if (currentCase != c && currentCase.getSymbole() != null && currentCase.getSymbole().equals(symbole)) {
|
||||
Console.errorln("La contrainte de colonne n'est pas respectee: ligne=" + ligne + ", col=" + colonne + ", symbole=" + symbole);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
19
app/src/main/java/sudoku/ContrainteLigne.java
Normal file
19
app/src/main/java/sudoku/ContrainteLigne.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package sudoku;
|
||||
|
||||
import sudoku.core.Console;
|
||||
|
||||
public class ContrainteLigne implements Contrainte {
|
||||
@Override
|
||||
public boolean estRespectee(Grille grille, Case c) {
|
||||
int ligne = c.getLigne();
|
||||
Symbole symbole = c.getSymbole();
|
||||
for (int col = 0; col < grille.getTaille(); col++) {
|
||||
Case currentCase = grille.getCase(ligne, col);
|
||||
if (currentCase != c && currentCase.getSymbole() != null && currentCase.getSymbole().equals(symbole)) {
|
||||
Console.errorln("La contrainte de ligne n'est pas respectee: ligne=" + ligne + ", col=" + col + ", symbole=" + symbole);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user