This commit is contained in:
EmmanuelTiamzon
2025-09-30 09:43:41 +02:00
parent f7de13bc2a
commit 7019a3b7ea
176 changed files with 9458 additions and 149 deletions

View File

@@ -1,27 +1,43 @@
public class Configuration {
private char[9] grille;
private char[] grille;
/*
On veut que chaque case soit un x ou un o et que si elle est vide alors n
la première case est 1 la deuxième 2, etc...
*/
public Configuration() {
this.grille = {'n','n','n','n','n','n','n','n','n'};
this.grille = new char[]{'n','n','n','n','n','n','n','n','n'};
}
public static int estLibre(int posGrille) {
if(this.grille[posGrille-1] == 'n') {
return true;
}
public boolean estLibre(int posGrille) {
if (posGrille < 1 || posGrille > 9) {
throw new IllegalArgumentException("La position doit être entre 1 et 9.");
}
return this.grille[posGrille - 1] == 'n';
}
public void jouer(int position, char joueur) {
if(joueur == 'x') {
this.grille[position-1] = 'x';
} else if(joueur == 'o') {
this.grille[position-1] = 'o';
}
}
if (position < 1 || position > 9) {
throw new IllegalArgumentException("La position doit être entre 1 et 9.");
}
if (joueur != 'x' && joueur != 'o') {
throw new IllegalArgumentException("Le joueur doit être 'x' ou 'o'.");
}
if (!estLibre(position)) {
throw new IllegalArgumentException("Cette case est déjà occupée.");
}
this.grille[position - 1] = joueur;
}
public void afficherGrille() {
for (int i = 0; i < 9; i++) {
System.out.print(this.grille[i] + " ");
if ((i + 1) % 3 == 0) {
System.out.println();
}
}
}
}

View File

@@ -0,0 +1,38 @@
public class Main {
public static void main(String[] args) {
Configuration jeu = new Configuration();
// Affichage de la grille initiale
jeu.afficherGrille();
System.out.println();
// Test de estLibre
System.out.println("Case 1 libre ? " + jeu.estLibre(1)); // true
// Test de jouer
jeu.jouer(1, 'x');
jeu.afficherGrille();
System.out.println("Case 1 libre ? " + jeu.estLibre(1)); // false
// Test de jouer sur une case occupée
try {
jeu.jouer(1, 'o'); // Doit lever une exception
} catch (Exception e) {
System.out.println(e.getMessage());
}
// Test de jouer avec une position invalide
try {
jeu.jouer(10, 'x'); // Doit lever une exception
} catch (Exception e) {
System.out.println(e.getMessage());
}
// Test de jouer avec un mauvais symbole
try {
jeu.jouer(2, 'z'); // Doit lever une exception
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}