diff --git a/app/src/main/java/sudoku/Grille.java b/app/src/main/java/sudoku/Grille.java index a309fdc..53b255b 100644 --- a/app/src/main/java/sudoku/Grille.java +++ b/app/src/main/java/sudoku/Grille.java @@ -11,162 +11,162 @@ public class Grille { private final Case[][] cases; private final ArrayList blocs; private ArrayList symbolesPossibles; - - public Grille(int taille) { - this.taille = taille; - this.cases = new Case[taille][taille]; - this.blocs = new ArrayList<>(); - this.symbolesPossibles = new ArrayList<>(); - - // Initialiser les cases - for (int i = 0; i < taille; i++) { - for (int j = 0; j < taille; j++) { - cases[i][j] = new Case(i, j, null); - } + + public Grille(int taille) { + this.taille = taille; + this.cases = new Case[taille][taille]; + this.blocs = new ArrayList<>(); + this.symbolesPossibles = new ArrayList<>(); + + // Initialiser les cases + for (int i = 0; i < taille; i++) { + for (int j = 0; j < taille; j++) { + cases[i][j] = new Case(i, j, null); } } - - public void setCase(int ligne, int colonne, Symbole symbole) { - try { - if (symbole != null && !symbolesPossibles.contains(symbole)) { - throw new IllegalArgumentException("Symbole non autorisé : " + symbole); - } - cases[ligne][colonne].setSymbole(symbole); - } catch (Exception e) { - Console.errorln(e.getMessage()); + } + + public void setCase(int ligne, int colonne, Symbole symbole) { + try { + if (symbole != null && !symbolesPossibles.contains(symbole)) { + throw new IllegalArgumentException("Symbole non autorisé : " + symbole); } - + cases[ligne][colonne].setSymbole(symbole); + } catch (Exception e) { + Console.errorln(e.getMessage()); } - - public Case getCase(int ligne, int colonne) { - return cases[ligne][colonne]; + + } + + public Case getCase(int ligne, int colonne) { + return cases[ligne][colonne]; + } + + /** + * Crée un bloc à partir des positions spécifiées + + Exemple : + sudoku.getGrille().createBloc(Arrays.asList( + new int[] { 0, 0 }, + new int[] { 0, 1 }, + new int[] { 0, 2 }, + new int[] { 1, 0 }, + new int[] { 1, 1 }, + new int[] { 1, 2 }, + new int[] { 2, 0 }, + new int[] { 2, 1 }, + new int[] { 2, 2 })); + * @param positions + * @return + */ + public Bloc createBloc(List positions) { + Bloc bloc = new Bloc(); + for (int[] pos : positions) { + bloc.addCase(cases[pos[0]][pos[1]]); } - - /** - * Crée un bloc à partir des positions spécifiées - - Exemple : - sudoku.getGrille().createBloc(Arrays.asList( - new int[] { 0, 0 }, - new int[] { 0, 1 }, - new int[] { 0, 2 }, - new int[] { 1, 0 }, - new int[] { 1, 1 }, - new int[] { 1, 2 }, - new int[] { 2, 0 }, - new int[] { 2, 1 }, - new int[] { 2, 2 })); - * @param positions - * @return - */ - public Bloc createBloc(List positions) { - Bloc bloc = new Bloc(); - for (int[] pos : positions) { - bloc.addCase(cases[pos[0]][pos[1]]); + blocs.add(bloc); + return bloc; + } + + public void createSquareBlocs() { + try { + int blocSize = (int) Math.sqrt(taille); + if (blocSize * blocSize != taille) { + throw new IllegalArgumentException("La taille de la grille doit être un carré parfait."); } - blocs.add(bloc); - return bloc; - } - - public void createSquareBlocs() { - try { - int blocSize = (int) Math.sqrt(taille); - if (blocSize * blocSize != taille) { - throw new IllegalArgumentException("La taille de la grille doit être un carré parfait."); - } - for (int i = 0; i < taille; i += blocSize) { - for (int j = 0; j < taille; j += blocSize) { - List positions = new ArrayList<>(); - for (int k = 0; k < blocSize; k++) { - for (int l = 0; l < blocSize; l++) { - positions.add(new int[] { i + k, j + l }); - } + for (int i = 0; i < taille; i += blocSize) { + for (int j = 0; j < taille; j += blocSize) { + List positions = new ArrayList<>(); + for (int k = 0; k < blocSize; k++) { + for (int l = 0; l < blocSize; l++) { + positions.add(new int[] { i + k, j + l }); } - createBloc(positions); } + createBloc(positions); } - } catch (IllegalArgumentException e) { - Console.errorln(e.getMessage()); } + } catch (IllegalArgumentException e) { + Console.errorln(e.getMessage()); } - - // TO MOVE TO FUTUR CONSTRAINTS - // public boolean isValid() { - // for (Bloc bloc : blocs) { - // if (!bloc.isValid()) { - // return false; - // } - // } - // return true; - // } - - public void printBlocs() { - for (Bloc bloc : blocs) { - System.out.println(bloc.toString()); + } + + // TO MOVE TO FUTUR CONSTRAINTS + // public boolean isValid() { + // for (Bloc bloc : blocs) { + // if (!bloc.isValid()) { + // return false; + // } + // } + // return true; + // } + + public void printBlocs() { + for (Bloc bloc : blocs) { + System.out.println(bloc.toString()); + } + } + + public void askSetSymbolesPossibles() { + try (Scanner scanner = new Scanner(System.in)) { + System.out.println(("Choisissez le type de symboles :")); + System.out.println(("1. Nombres")); + System.out.println(("2. Caractères")); + System.out.println(("3. Texte/Emoji")); + + int choix = 0; + try { + choix = Integer.parseInt(scanner.nextLine()); + } catch (NumberFormatException e) { + Console.errorln("Choix invalide"); + return; } - } - - public void askSetSymbolesPossibles() { - try (Scanner scanner = new Scanner(System.in)) { - System.out.println(("Choisissez le type de symboles :")); - System.out.println(("1. Nombres")); - System.out.println(("2. Caractères")); - System.out.println(("3. Texte/Emoji")); - - int choix = 0; - try { - choix = Integer.parseInt(scanner.nextLine()); - } catch (NumberFormatException e) { - Console.errorln("Choix invalide"); - return; - } - - for (int i = 0; i < taille; i++) { - System.out.println(("Entrez le symbole " + (i + 1) + "/" + taille + " :")); - String input = scanner.nextLine(); - - switch (choix) { - case 1: // Nombres - try { - symbolesPossibles.add(Symbole.of(Integer.parseInt(input))); - } catch (NumberFormatException e) { - Console.errorln("Veuillez entrer un nombre valide"); - i--; - } - break; - - case 2: // Caractères - if (input.length() == 1) { - symbolesPossibles.add(Symbole.of(input.charAt(0))); - } else { - Console.errorln("Veuillez entrer un seul caractère"); - i--; - } - break; - - case 3: // Texte/Emoji - if (!input.isEmpty()) { - symbolesPossibles.add(Symbole.of(input)); - } else { - Console.errorln("Le symbole ne peut pas être vide"); - i--; - } - break; - - default: - Console.errorln("Type non supporté"); - return; - } + + for (int i = 0; i < taille; i++) { + System.out.println(("Entrez le symbole " + (i + 1) + "/" + taille + " :")); + String input = scanner.nextLine(); + + switch (choix) { + case 1: // Nombres + try { + symbolesPossibles.add(Symbole.of(Integer.parseInt(input))); + } catch (NumberFormatException e) { + Console.errorln("Veuillez entrer un nombre valide"); + i--; + } + break; + + case 2: // Caractères + if (input.length() == 1) { + symbolesPossibles.add(Symbole.of(input.charAt(0))); + } else { + Console.errorln("Veuillez entrer un seul caractère"); + i--; + } + break; + + case 3: // Texte/Emoji + if (!input.isEmpty()) { + symbolesPossibles.add(Symbole.of(input)); + } else { + Console.errorln("Le symbole ne peut pas être vide"); + i--; + } + break; + + default: + Console.errorln("Type non supporté"); + return; } } } - - public List getSymbolesPossibles() { - return symbolesPossibles; - } - - public void setSymbolesPossibles(ArrayList symbolesPossibles) { - this.symbolesPossibles = symbolesPossibles; + } + + public List getSymbolesPossibles() { + return symbolesPossibles; + } + + public void setSymbolesPossibles(ArrayList symbolesPossibles) { + this.symbolesPossibles = symbolesPossibles; } public void printSymbolesPossibles() { diff --git a/app/src/test/java/sudoku/AppTest.java b/app/src/test/java/sudoku/AppTest.java index 62d8714..c1aaab5 100644 --- a/app/src/test/java/sudoku/AppTest.java +++ b/app/src/test/java/sudoku/AppTest.java @@ -5,13 +5,51 @@ package sudoku; import org.junit.jupiter.api.Test; +import java.util.ArrayList; +import java.util.Arrays; + import static org.junit.jupiter.api.Assertions.assertEquals; public class AppTest { @Test public void testExample() { + //Grille.askSetSymbolesPossibles(); System.out.println("DOING TEST"); - assertEquals(2 + 2, 4); - System.out.println("DONE TEST"); + System.out.println(new App().getGreeting()); + + // 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)))); + + sudoku.getGrille().setCase(0, 0, Symbole.of(1)); + sudoku.getGrille().setCase(6, 1, Symbole.of(2)); + sudoku.getGrille().setCase(2, 2, Symbole.of(3)); + sudoku.getGrille().setCase(0, 3, Symbole.of(4)); + sudoku.getGrille().setCase(4, 4, Symbole.of(5)); + sudoku.getGrille().setCase(0, 5, Symbole.of(6)); + sudoku.getGrille().setCase(5, 6, Symbole.of(7)); + sudoku.getGrille().setCase(0, 7, Symbole.of(8)); + sudoku.getGrille().setCase(4, 8, Symbole.of(9)); + + sudoku.getGrille().createSquareBlocs(); + + System.out.println("Sudoku :"); + System.out.println(sudoku.getGrille().toString()); + + System.out.println("Blocs :"); + sudoku.getGrille().printBlocs(); + + System.out.println("Symboles possibles :"); + sudoku.getGrille().printSymbolesPossibles(); + System.out.println("DONE TEST"); } } \ No newline at end of file diff --git a/app/src/test/java/sudoku/TestBloc.java b/app/src/test/java/sudoku/TestBloc.java new file mode 100644 index 0000000..62ada44 --- /dev/null +++ b/app/src/test/java/sudoku/TestBloc.java @@ -0,0 +1,50 @@ +package sudoku; + +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Arrays; + +public class TestBloc { + @Test + public void testBloc() { + System.out.println("TEST BLOC : "); + System.out.println(new App().getGreeting()); + // 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)))); + + sudoku.getGrille().setCase(0, 0, Symbole.of(1)); + sudoku.getGrille().setCase(6, 1, Symbole.of(2)); + sudoku.getGrille().setCase(2, 2, Symbole.of(3)); + sudoku.getGrille().setCase(0, 3, Symbole.of(4)); + sudoku.getGrille().setCase(4, 4, Symbole.of(5)); + sudoku.getGrille().setCase(0, 5, Symbole.of(6)); + sudoku.getGrille().setCase(5, 6, Symbole.of(7)); + sudoku.getGrille().setCase(0, 7, Symbole.of(8)); + sudoku.getGrille().setCase(4, 8, Symbole.of(9)); + //doublon ligne + sudoku.getGrille().setCase(0, 1, Symbole.of(1)); + + sudoku.getGrille().createSquareBlocs(); + + System.out.println("Sudoku :"); + System.out.println(sudoku.getGrille().toString()); + + System.out.println("Blocs :"); + sudoku.getGrille().printBlocs(); + + System.out.println("Symboles possibles :"); + sudoku.getGrille().printSymbolesPossibles(); + System.out.println("FIN TEST BLOC"); + } +} diff --git a/app/src/test/java/sudoku/TestColonne.java b/app/src/test/java/sudoku/TestColonne.java new file mode 100644 index 0000000..b88e15c --- /dev/null +++ b/app/src/test/java/sudoku/TestColonne.java @@ -0,0 +1,52 @@ +package sudoku; + +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Arrays; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class TestColonne { + @Test + public void testColonne() { + System.out.println("TEST COL : "); + System.out.println(new App().getGreeting()); + // 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)))); + + sudoku.getGrille().setCase(0, 0, Symbole.of(1)); + sudoku.getGrille().setCase(6, 1, Symbole.of(2)); + sudoku.getGrille().setCase(2, 2, Symbole.of(3)); + sudoku.getGrille().setCase(0, 3, Symbole.of(4)); + sudoku.getGrille().setCase(4, 4, Symbole.of(5)); + sudoku.getGrille().setCase(0, 5, Symbole.of(6)); + sudoku.getGrille().setCase(5, 6, Symbole.of(7)); + sudoku.getGrille().setCase(0, 7, Symbole.of(8)); + sudoku.getGrille().setCase(4, 8, Symbole.of(9)); + //doublon ligne + sudoku.getGrille().setCase(0, 0, Symbole.of(1)); + + sudoku.getGrille().createSquareBlocs(); + + System.out.println("Sudoku :"); + System.out.println(sudoku.getGrille().toString()); + + System.out.println("Blocs :"); + sudoku.getGrille().printBlocs(); + + System.out.println("Symboles possibles :"); + sudoku.getGrille().printSymbolesPossibles(); + System.out.println("FIN TEST COL"); + } +} diff --git a/app/src/test/java/sudoku/TestLigne.java b/app/src/test/java/sudoku/TestLigne.java new file mode 100644 index 0000000..826bd46 --- /dev/null +++ b/app/src/test/java/sudoku/TestLigne.java @@ -0,0 +1,50 @@ +package sudoku; + +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.Arrays; + +public class TestLigne { + @Test + public void testLigne() { + System.out.println("TEST LIGNE : "); + System.out.println(new App().getGreeting()); + // 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)))); + + sudoku.getGrille().setCase(0, 0, Symbole.of(1)); + sudoku.getGrille().setCase(6, 1, Symbole.of(2)); + sudoku.getGrille().setCase(2, 2, Symbole.of(3)); + sudoku.getGrille().setCase(0, 3, Symbole.of(4)); + sudoku.getGrille().setCase(4, 4, Symbole.of(5)); + sudoku.getGrille().setCase(0, 5, Symbole.of(6)); + sudoku.getGrille().setCase(5, 6, Symbole.of(7)); + sudoku.getGrille().setCase(0, 7, Symbole.of(8)); + sudoku.getGrille().setCase(4, 8, Symbole.of(9)); + //doublon ligne + sudoku.getGrille().setCase(0, 4, Symbole.of(1)); + + sudoku.getGrille().createSquareBlocs(); + + System.out.println("Sudoku :"); + System.out.println(sudoku.getGrille().toString()); + + System.out.println("Blocs :"); + sudoku.getGrille().printBlocs(); + + System.out.println("Symboles possibles :"); + sudoku.getGrille().printSymbolesPossibles(); + System.out.println("FIN TEST LIGNE"); + } +}