Files
DEV/DEV.2.1/CM-blanc/CM-1/2.Morpion/Configuration.java

43 lines
1.2 KiB
Java
Raw Normal View History

2025-03-17 17:11:48 +01:00
public class Configuration {
2025-09-30 09:43:41 +02:00
private char[] grille;
2025-03-17 17:11:48 +01:00
/*
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() {
2025-09-30 09:43:41 +02:00
this.grille = new char[]{'n','n','n','n','n','n','n','n','n'};
2025-03-17 17:11:48 +01:00
}
2025-09-30 09:43:41 +02:00
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';
2025-03-17 17:11:48 +01:00
}
public void jouer(int position, char joueur) {
2025-09-30 09:43:41 +02:00
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();
}
}
}
2025-03-17 17:11:48 +01:00
}