feat: add constraint management and validation to Sudoku class
This commit is contained in:
@@ -199,6 +199,10 @@ public class Grille {
|
|||||||
System.out.println(sb.toString());
|
System.out.println(sb.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getTaille() {
|
||||||
|
return taille;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|||||||
@@ -1,10 +1,32 @@
|
|||||||
package sudoku;
|
package sudoku;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class Sudoku {
|
public class Sudoku {
|
||||||
private final Grille grille;
|
private final Grille grille;
|
||||||
|
private final List<Contrainte> contraintes;
|
||||||
|
|
||||||
public Sudoku(int taille) {
|
public Sudoku(int taille) {
|
||||||
this.grille = new Grille(taille);
|
this.grille = new Grille(taille);
|
||||||
|
this.contraintes = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ajouterContrainte(Contrainte contrainte) {
|
||||||
|
contraintes.add(contrainte);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void creerBloc(List<int[]> positions) {
|
||||||
|
grille.creerBloc(positions);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean estValide(Case c) {
|
||||||
|
for (Contrainte contrainte : contraintes) {
|
||||||
|
if (!contrainte.estRespectee(grille, c)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Grille getGrille() {
|
public Grille getGrille() {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
public class AppTest {
|
public class AppTest {
|
||||||
@Test
|
@Test
|
||||||
@@ -32,4 +33,58 @@ public class AppTest {
|
|||||||
Symbole.of(9)));
|
Symbole.of(9)));
|
||||||
assertEquals(expectedSymbols, sudoku.getGrille().getSymbolesPossibles());
|
assertEquals(expectedSymbols, sudoku.getGrille().getSymbolesPossibles());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testContraintes() {
|
||||||
|
// Create a new Sudoku
|
||||||
|
Sudoku sudoku = new Sudoku(9);
|
||||||
|
sudoku.getGrille().setSymbolesPossibles(new ArrayList<>(Arrays.asList(
|
||||||
|
Symbole.of(1),
|
||||||
|
Symbole.of(2),
|
||||||
|
Symbole.of(3),
|
||||||
|
Symbole.of(4),
|
||||||
|
Symbole.of(5),
|
||||||
|
Symbole.of(6),
|
||||||
|
Symbole.of(7),
|
||||||
|
Symbole.of(8),
|
||||||
|
Symbole.of(9))));
|
||||||
|
|
||||||
|
// Add constraints
|
||||||
|
sudoku.ajouterContrainte(new ContrainteLigne());
|
||||||
|
sudoku.ajouterContrainte(new ContrainteColonne());
|
||||||
|
sudoku.ajouterContrainte(new ContrainteBloc());
|
||||||
|
|
||||||
|
// Fill the grid with valid symbols
|
||||||
|
sudoku.getGrille().setCase(0, 0, Symbole.of(1));
|
||||||
|
sudoku.getGrille().setCase(0, 1, Symbole.of(2));
|
||||||
|
sudoku.getGrille().setCase(0, 2, Symbole.of(3));
|
||||||
|
sudoku.getGrille().setCase(1, 0, Symbole.of(4));
|
||||||
|
sudoku.getGrille().setCase(1, 1, Symbole.of(5));
|
||||||
|
sudoku.getGrille().setCase(1, 2, Symbole.of(6));
|
||||||
|
sudoku.getGrille().setCase(2, 0, Symbole.of(7));
|
||||||
|
sudoku.getGrille().setCase(2, 1, Symbole.of(8));
|
||||||
|
sudoku.getGrille().setCase(2, 2, Symbole.of(9));
|
||||||
|
|
||||||
|
// Create square blocks
|
||||||
|
sudoku.getGrille().creerBlocCarre();
|
||||||
|
|
||||||
|
System.out.println(sudoku.getGrille().toString());
|
||||||
|
|
||||||
|
// Verify that the constraints are respected
|
||||||
|
assertTrue(sudoku.estValide(sudoku.getGrille().getCase(0, 0)));
|
||||||
|
assertTrue(sudoku.estValide(sudoku.getGrille().getCase(1, 1)));
|
||||||
|
assertTrue(sudoku.estValide(sudoku.getGrille().getCase(2, 2)));
|
||||||
|
|
||||||
|
// Add a duplicate symbol in the same row
|
||||||
|
// sudoku.getGrille().setCase(0, 3, Symbole.of(1));
|
||||||
|
// assertFalse(sudoku.estValide(sudoku.getGrille().getCase(0, 3)));
|
||||||
|
|
||||||
|
// Add a duplicate symbol in the same column
|
||||||
|
// sudoku.getGrille().setCase(3, 0, Symbole.of(1));
|
||||||
|
// assertFalse(sudoku.estValide(sudoku.getGrille().getCase(3, 0)));
|
||||||
|
|
||||||
|
// Add a duplicate symbol in the same block
|
||||||
|
// sudoku.getGrille().setCase(1, 1, Symbole.of(1));
|
||||||
|
// assertFalse(sudoku.estValide(sudoku.getGrille().getCase(1, 1)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,8 @@ package sudoku;
|
|||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
@@ -23,6 +25,9 @@ public class TestBloc {
|
|||||||
Symbole.of(8),
|
Symbole.of(8),
|
||||||
Symbole.of(9))));
|
Symbole.of(9))));
|
||||||
|
|
||||||
|
// Add constraints
|
||||||
|
sudoku.ajouterContrainte(new ContrainteBloc());
|
||||||
|
|
||||||
sudoku.getGrille().setCase(0, 0, Symbole.of(1));
|
sudoku.getGrille().setCase(0, 0, Symbole.of(1));
|
||||||
sudoku.getGrille().setCase(6, 1, Symbole.of(2));
|
sudoku.getGrille().setCase(6, 1, Symbole.of(2));
|
||||||
sudoku.getGrille().setCase(2, 2, Symbole.of(3));
|
sudoku.getGrille().setCase(2, 2, Symbole.of(3));
|
||||||
@@ -34,8 +39,9 @@ public class TestBloc {
|
|||||||
sudoku.getGrille().setCase(4, 8, Symbole.of(9));
|
sudoku.getGrille().setCase(4, 8, Symbole.of(9));
|
||||||
//doublon bloc
|
//doublon bloc
|
||||||
sudoku.getGrille().setCase(1, 1, Symbole.of(1));
|
sudoku.getGrille().setCase(1, 1, Symbole.of(1));
|
||||||
|
assertFalse(sudoku.estValide(sudoku.getGrille().getCase(1, 1)));
|
||||||
|
|
||||||
sudoku.getGrille().createSquareBlocs();
|
sudoku.getGrille().creerBlocCarre();
|
||||||
|
|
||||||
System.out.println("Sudoku :");
|
System.out.println("Sudoku :");
|
||||||
System.out.println(sudoku.getGrille().toString());
|
System.out.println(sudoku.getGrille().toString());
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package sudoku;
|
|||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
@@ -23,6 +25,8 @@ public class TestColonne {
|
|||||||
Symbole.of(8),
|
Symbole.of(8),
|
||||||
Symbole.of(9))));
|
Symbole.of(9))));
|
||||||
|
|
||||||
|
sudoku.ajouterContrainte(new ContrainteColonne());
|
||||||
|
|
||||||
sudoku.getGrille().setCase(0, 0, Symbole.of(1));
|
sudoku.getGrille().setCase(0, 0, Symbole.of(1));
|
||||||
sudoku.getGrille().setCase(6, 1, Symbole.of(2));
|
sudoku.getGrille().setCase(6, 1, Symbole.of(2));
|
||||||
sudoku.getGrille().setCase(2, 2, Symbole.of(3));
|
sudoku.getGrille().setCase(2, 2, Symbole.of(3));
|
||||||
@@ -34,8 +38,9 @@ public class TestColonne {
|
|||||||
sudoku.getGrille().setCase(4, 8, Symbole.of(9));
|
sudoku.getGrille().setCase(4, 8, Symbole.of(9));
|
||||||
// doublon colonne
|
// doublon colonne
|
||||||
sudoku.getGrille().setCase(4, 0, Symbole.of(1));
|
sudoku.getGrille().setCase(4, 0, Symbole.of(1));
|
||||||
|
assertFalse(sudoku.estValide(sudoku.getGrille().getCase(4, 0)));
|
||||||
|
|
||||||
sudoku.getGrille().createSquareBlocs();
|
sudoku.getGrille().creerBlocCarre();
|
||||||
|
|
||||||
System.out.println("Sudoku :");
|
System.out.println("Sudoku :");
|
||||||
System.out.println(sudoku.getGrille().toString());
|
System.out.println(sudoku.getGrille().toString());
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package sudoku;
|
|||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
@@ -23,6 +25,8 @@ public class TestLigne {
|
|||||||
Symbole.of(8),
|
Symbole.of(8),
|
||||||
Symbole.of(9))));
|
Symbole.of(9))));
|
||||||
|
|
||||||
|
sudoku.ajouterContrainte(new ContrainteLigne());
|
||||||
|
|
||||||
sudoku.getGrille().setCase(0, 0, Symbole.of(1));
|
sudoku.getGrille().setCase(0, 0, Symbole.of(1));
|
||||||
sudoku.getGrille().setCase(6, 1, Symbole.of(2));
|
sudoku.getGrille().setCase(6, 1, Symbole.of(2));
|
||||||
sudoku.getGrille().setCase(2, 2, Symbole.of(3));
|
sudoku.getGrille().setCase(2, 2, Symbole.of(3));
|
||||||
@@ -34,8 +38,9 @@ public class TestLigne {
|
|||||||
sudoku.getGrille().setCase(4, 8, Symbole.of(9));
|
sudoku.getGrille().setCase(4, 8, Symbole.of(9));
|
||||||
//doublon ligne
|
//doublon ligne
|
||||||
sudoku.getGrille().setCase(0, 4, Symbole.of(1));
|
sudoku.getGrille().setCase(0, 4, Symbole.of(1));
|
||||||
|
assertFalse(sudoku.estValide(sudoku.getGrille().getCase(0, 4)));
|
||||||
|
|
||||||
sudoku.getGrille().createSquareBlocs();
|
sudoku.getGrille().creerBlocCarre();
|
||||||
|
|
||||||
System.out.println("Sudoku :");
|
System.out.println("Sudoku :");
|
||||||
System.out.println(sudoku.getGrille().toString());
|
System.out.println(sudoku.getGrille().toString());
|
||||||
|
|||||||
Reference in New Issue
Block a user