Gwendal in main : add test on futur constraints and enhance input symbol method #1

Merged
Morph01 merged 6 commits from Gwendal into main 2025-01-04 15:56:23 +00:00
5 changed files with 95 additions and 84 deletions
Showing only changes of commit 3e6aa821fc - Show all commits

View File

@@ -2,7 +2,6 @@ package sudoku;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;
import sudoku.core.Console;
@@ -45,18 +44,19 @@ public class Grille {
/**
* 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 }));
*
* 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
*/
@@ -93,12 +93,12 @@ public class Grille {
// TO MOVE TO FUTUR CONSTRAINTS
// public boolean isValid() {
// for (Bloc bloc : blocs) {
// if (!bloc.isValid()) {
// return false;
// }
// }
// return true;
// for (Bloc bloc : blocs) {
// if (!bloc.isValid()) {
// return false;
// }
// }
// return true;
// }
public void printBlocs() {
@@ -109,92 +109,76 @@ public class Grille {
public void askSetSymbolesPossibles() {
Scanner scanner = new Scanner(System.in);
try{
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"));
try {
Console.infoln("Choisissez le type de symboles :");
Console.infoln("1. Nombres");
Console.infoln("2. Lettres");
Console.infoln("3. Texte/Emoji");
int choix = 0;
try {
choix = scanner.nextInt();
scanner.nextLine();
choix = Integer.parseInt(scanner.nextLine());
if (choix < 1 || choix > 3) {
throw new NumberFormatException("Choix invalide");
}
} 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 {
Symbole intTemp = Symbole.of(Integer.parseInt(input));
//Evaluation de la du caractère qui vient d'être saisit
for (Symbole symbole : symbolesPossibles) {
if (Objects.equals(intTemp.toString(), symbole.toString())){
Console.errorln("Ce symbole existe déja, veuillez entrer un autre symbole");
throw new NumberFormatException("Ce symbole existe déja");
}
Symbole intTemp = Symbole.of(input);
if (intTemp.isInt()) {
if (symbolesPossibles.contains(intTemp)) {
Console.errorln("Ce symbole existe déjà, veuillez entrer un autre symbole");
i--;
} else {
symbolesPossibles.add(intTemp);
}
symbolesPossibles.add(Symbole.of(Integer.parseInt(input)));
} catch (NumberFormatException e) {
} else {
Console.errorln("Veuillez entrer un nombre valide");
i--;
}
break;
case 2: // Caractères
if (input.length() == 1) {
try {
Symbole charTemp = Symbole.of(input);
//Evaluation de la du caractère qui vient d'être saisit
for (Symbole symbole : symbolesPossibles) {
if (Objects.equals(charTemp.toString(), symbole.toString())) {
Console.errorln("Ce symbole existe déja, veuillez entrer un autre symbole");
throw new NumberFormatException("Ce symbole existe déja");
}
}
symbolesPossibles.add(Symbole.of(input.charAt(0)));
}catch (NumberFormatException e) {
Console.errorln("Veuillez entrer un caractère valide");
case 2: // Lettres
if (input.length() == 1 && Symbole.of(input.charAt(0)).isLetter()) {
Symbole charTemp = Symbole.of(input.charAt(0));
if (symbolesPossibles.contains(charTemp)) {
Console.errorln("Ce symbole existe déjà, veuillez entrer un autre symbole");
i--;
} else {
symbolesPossibles.add(charTemp);
}
} else {
Console.errorln("Veuillez entrer un seul caractère");
Console.errorln("Veuillez entrer une lettre valide");
i--;
}
break;
case 3: // Texte/Emoji
if (!input.isEmpty()) {
try {
Symbole textTemp = Symbole.of(input);
//Evaluation de la du caractère qui vient d'être saisit
for (Symbole symbole : symbolesPossibles) {
if (Objects.equals(textTemp.toString(), symbole.toString())) {
Console.errorln("Ce symbole existe déja, veuillez entrer un autre symbole");
throw new NumberFormatException("Ce symbole existe déja");
}
}
symbolesPossibles.add(Symbole.of(input));
}catch (NumberFormatException e) {
Console.errorln("Veuillez entrer un texte ou un emoji valide");
i--;
}
} else {
Console.errorln("Le symbole ne peut pas être vide");
Symbole stringTemp = Symbole.of(input);
if (symbolesPossibles.contains(stringTemp)) {
Console.errorln("Ce symbole existe déjà, veuillez entrer un autre symbole");
i--;
} else {
symbolesPossibles.add(stringTemp);
}
break;
default:
Console.errorln("Type non supporté");
return;
}
}
}catch (Exception e) {
} catch (Exception e) {
System.out.println("Une erreur est survenue : " + e.getMessage());
}finally {
} finally {
scanner.close();
}
}

View File

@@ -20,6 +20,19 @@ public class Symbole {
return new Symbole(String.valueOf(c));
}
public boolean isInt() {
try {
Integer.parseInt(valeur);
return true;
} catch (NumberFormatException e) {
return false;
}
}
public boolean isLetter() {
return valeur.length() == 1 && Character.isLetter(valeur.charAt(0));
}
@Override
public String toString() {
return valeur;
@@ -27,9 +40,11 @@ public class Symbole {
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (this == obj)
return true;
if (obj == null || getClass() != obj.getClass()) return false;
if (obj == null || getClass() != obj.getClass())
return false;
Symbole symbole = (Symbole) obj;
return valeur.equals(symbole.valeur);
}

View File

@@ -11,14 +11,18 @@ public class Console {
static final String ANSI_BOLD = "\u001B[1m";
public static void errorln(String message) {
System.err.println(ANSI_RED + ANSI_BOLD + message + ANSI_RESET);
System.err.println(ANSI_RED + ANSI_BOLD + message + ANSI_RESET + "\n");
}
public static void successln(String message) {
System.out.println(ANSI_GREEN + ANSI_BOLD + message + ANSI_RESET);
System.out.println(ANSI_GREEN + ANSI_BOLD + message + ANSI_RESET + "\n");
}
public static void warnln(String message) {
System.out.println(ANSI_YELLOW + ANSI_BOLD+ message + ANSI_RESET);
System.out.println(ANSI_YELLOW + ANSI_BOLD + message + ANSI_RESET + "\n");
}
public static void infoln(String message) {
System.out.println(message + "\n");
}
}

View File

@@ -1,10 +1,8 @@
/*
* This Java source file was generated by the Gradle 'init' task.
*/
package sudoku;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.Arrays;
@@ -13,13 +11,25 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
public class AppTest {
@Test
public void testExample() {
System.out.println("DOING TEST");
System.out.println(new App().getGreeting());
// Simulate user input
String simulatedInput = "1\n1\n2\n3\n4\n5\n6\n7\n8\n9\n";
System.setIn(new ByteArrayInputStream(simulatedInput.getBytes()));
// Create a new Sudoku
Sudoku sudoku = new Sudoku(9);
sudoku.getGrille().askSetSymbolesPossibles();
System.out.println("DONE TEST");
// Verify the symbols
ArrayList<Symbole> expectedSymbols = 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)));
assertEquals(expectedSymbols, sudoku.getGrille().getSymbolesPossibles());
}
}

View File

@@ -5,8 +5,6 @@ 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() {
@@ -34,7 +32,7 @@ public class TestColonne {
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 colonne
// doublon colonne
sudoku.getGrille().setCase(4, 0, Symbole.of(1));
sudoku.getGrille().createSquareBlocs();