Skip to content

Commit

Permalink
Merge pull request #3 from danydudiot/refactor
Browse files Browse the repository at this point in the history
refactor et doc
  • Loading branch information
danydudiot authored Apr 8, 2024
2 parents 84c5c87 + 00b50eb commit 57562ba
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/controleur/Controleur.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public abstract class Controleur {
* Méthode permettant d'initialiser un certain nombre de joueurs (pour l'instant, fixé à 2)
*/
protected void initJoueur() {
int nbJoueur = 2; //Possiblité de demander à l'ihm un nombre de joueurs ou par le biais d'un futur paramètre
int nbJoueur = 2; //Possibilité de demander à l'ihm un nombre de joueurs ou par le biais d'un futur paramètre
lesJoueurs = new Joueur[nbJoueur];
for (int i = 0; i < nbJoueur; i++) {
lesJoueurs[i] = new Joueur(ihm.demanderNomJoueur(i+1));
Expand Down
13 changes: 4 additions & 9 deletions src/controleur/ControleurJeuNim.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,17 @@ public void jouer() {
toursDeJeu();
}
/**
* Demande le coup à l'ihm et demander au plateau de réaliser le coup.
* @throws FormatReponseInvalide Si la réponse n'est pas au format `m n`.
* Demande le coup à l'ihm et demande au plateau de réaliser le coup.
* @throws NombreBatonnetsInvalide Si le nombre de bâtonnets dans le tas à inférieur au retrait demandé ou que la limite de bâtonnets par coup a été atteinte.
* @throws NumeroTasInvalide Si le tas demandé est inconnue.
*/
protected void getCoup() throws FormatReponseInvalide, NombreBatonnetsInvalide, NumeroTasInvalide {
protected void getCoup() throws NombreBatonnetsInvalide, NumeroTasInvalide {
int[] candidate = ihm.demanderCoupNim(getNomJoueurCourant());
plateau.retirerBatonnets(candidate[0], candidate[1]);
}

/**
* Incrément le nombre de victoires du joueur courant
* Incrémente le nombre de victoires du joueur courant
* et gére l'affiche du vainqueur.
*/
protected void victoire(){
Expand All @@ -72,10 +71,6 @@ protected void victoire(){
protected void setOption(){
//appel de l'ihm et transfère dans le plateau
int batonnetMax = ihm.setOptionNim();
if (batonnetMax > 0) {
plateau.setOption(batonnetMax);
} else {
ihm.afficherErreur("Merci d'entrer un entier Positif");
}
plateau.setOption(batonnetMax);
}
}
9 changes: 7 additions & 2 deletions src/modele/Plateau.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
public abstract class Plateau {

/**
* Remplit et/ou réinitialise le plateau
* Réinitialise le plateau
*/
public abstract void reset();

/**
* Permet de verifier si une partie est finie
* Permet de vérifier si une partie est finie
* @return true si la partie est fini, sinon false
*/
public abstract boolean verifierFin();
Expand Down Expand Up @@ -45,6 +45,11 @@ public abstract class Plateau {
* @throws NombreBatonnetsInvalide Si le nombre de bâtonnets à retirer est invalide (négatif ou supérieur au nombre actuel de bâtonnets dans le tas).
*/
public abstract void retirerBatonnets(int m, int n) throws NombreBatonnetsInvalide, NumeroTasInvalide;

/**
* Permet de régler les options d'un jeu
* @param opt le paramètre de l'option
*/
public abstract void setOption(int opt);
public abstract void rotation(boolean sens, int joueur) throws PlusDeRotations;
public abstract void setRotations(int i);
Expand Down
27 changes: 20 additions & 7 deletions src/modele/PlateauNim.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

/**
* Classe représentant un plateau de jeu de Nim
* hérite de Plateau
* @see modele.Plateau
*/
public class PlateauNim extends Plateau {
/**
Expand All @@ -19,7 +21,7 @@ public class PlateauNim extends Plateau {
private int nombreTas;

/**
* Le nombre Maximum de batonnets a retirable par coups
* Le nombre Maximum de bâtonnets retirable par coups
*/
private int maxBatonnets;

Expand Down Expand Up @@ -77,14 +79,14 @@ public int[] getPlateau() {
* @param m Le numéro du tas à partir duquel retirer les bâtonnets.
* @param n Le nombre de bâtonnets à retirer.
* @throws NumeroTasInvalide Si le numéro du tas est invalide (hors des limites ou égal à zéro).
* @throws NombreBatonnetsInvalide Si le nombre de bâtonnets à retirer est invalide (négatif ou supérieur au nombre actuel de bâtonnets dans le tas).
* @throws NombreBatonnetsInvalide Si le nombre de bâtonnets à retirer est invalide (négatif ou supérieur au nombre actuel de bâtonnets dans le tas) ou que la limite est atteinte.
*/
public void retirerBatonnets(int m, int n) throws NombreBatonnetsInvalide, NumeroTasInvalide {
if ( m > nombreTas || m <= 0 ) {
throw new NumeroTasInvalide("Vous avez sélectionné un tas inconnu.");
}
if ( n > maxBatonnets ) {
throw new NombreBatonnetsInvalide("Vous avez selectionner trop de batonnets (max " + maxBatonnets + " batonnets par coup)");
throw new NombreBatonnetsInvalide("Vous avez sélectionner trop de bâtonnets (max " + maxBatonnets + " bâtonnets par coup)");
}
lesTas[m-1].retirerBatonnet(n);
}
Expand All @@ -106,24 +108,35 @@ public String toString() {
String batonnets = t.toString();
int tailleMarge = (largeur - batonnets.length()) / 2;
String marge = " ".repeat(tailleMarge);
// s += Integer.toString(i + 1) + " : " + marge + batonnets + marge + "\n";
s += (i+1) + ": " + marge + batonnets + marge + "\n";
}
return s;
}

/**
* Setter pour le nombre Maximum de batonnets retirable en un coup
* @param maxBatonnets
* Setter pour le nombre Maximum de bâtonnets retirable en un coup
* @param maxBatonnets le nombre maximum de bâtonnets (pour ne pas avoir de limite 0)
*/
public void setOption(int maxBatonnets) {
this.maxBatonnets = maxBatonnets;
if (maxBatonnets == 0) {
this.maxBatonnets= Integer.MAX_VALUE;
} else {
this.maxBatonnets = maxBatonnets;
}
}

/**
* Méthode non utilisable
* @deprecated
*/
public void placerJeton(byte colonne, byte joueur) throws ColonnePleine, FormatReponseInvalide{
throw new UnsupportedOperationException("Méthode non implémentée");
}

/**
* Méthode non utilisable
* @deprecated
*/
public boolean verifierVictoire(){
throw new UnsupportedOperationException("Méthode non implémentée");
}
Expand Down
6 changes: 6 additions & 0 deletions src/modele/PlateauP4.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,16 @@ public String toString() {
return string;
}


/**
* Méthode non utilisable
* @deprecated
*/
public void retirerBatonnets(int m, int n) throws NombreBatonnetsInvalide, NumeroTasInvalide {
throw new UnsupportedOperationException("Méthode non implémentée");
}


public void setRotations(int i){
if (i == 0) {
rotations = false;
Expand Down
19 changes: 10 additions & 9 deletions src/vue/Ihm.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,20 @@ public int[] demanderCoupNim(String nomJoueur) {
}

/**
* <p>Demande au joueur le nombre maximum de batonnets à retirer en un seul coup</p>
* <p>Demande au joueur le nombre maximum de bâtonnets à retirer en un seul coup</p>
* @return un entier strictement positif
*/
public int setOptionNim() {
Pattern pattern = Pattern.compile("^[0-9]+$");
while (true) {
System.out.println("Veuillez entrer le nombre maximum de batonnet retirable d'un seul coup.");
System.out.println("Veuillez entrer le nombre maximum de bâtonnet retirable d'un seul coup (0 pour ne pas mettre de limite).");
if (scanner.hasNextLine()) {
String candidate = scanner.nextLine();
Matcher matcher = pattern.matcher(candidate);
if (matcher.find()) {
return Integer.parseInt(candidate);
} else {
System.out.println("Merci d'entrer un entier positif");
afficherErreur("Merci d'entrer un entier positif");
}
}
}
Expand All @@ -139,7 +139,8 @@ public boolean demanderJouerEncore() {
return line.equals("y");
}
}
System.out.println("\n\n⚠ Vous avez répondu avec autre chose que `y` ou `n`.");

afficherErreur("Vous avez répondu avec autre chose que `y` ou `n`.");
}
}

Expand Down Expand Up @@ -199,7 +200,7 @@ public byte demanderCoupP4(String nomJoueur) {
return candidate;
}
scanner.nextLine();
System.out.println("\n\nVeuillez entrer un entier compris entre 1 et 7");
afficherErreur("Veuillez entrer un entier compris entre 1 et 7");
}
}

Expand All @@ -220,7 +221,7 @@ public boolean demanderJeu() {
return line.equals("nim");
}
}
System.out.println("\n\nVous avez répondu avec autre chose que `p4` ou `nim`.");
afficherErreur("Vous avez répondu avec autre chose que `p4` ou `nim`.");
}
}

Expand All @@ -240,7 +241,7 @@ public boolean demanderActivationRotation() {
return line.equals("y");
}
}
System.out.println("\n\nVous avez répondu avec autre chose que `y` ou `n`.");
afficherErreur("Vous avez répondu avec autre chose que `y` ou `n`.");
}
}

Expand All @@ -260,7 +261,7 @@ public boolean demanderCoupOuRotation(String nomJoueur) {
return line.equals("c");
}
}
System.out.println("\n\nVous avez répondu avec autre chose que 'c' ou 'r'.");
afficherErreur("Vous avez répondu avec autre chose que 'c' ou 'r'.");
}
}

Expand All @@ -280,7 +281,7 @@ public boolean demanderRotation(String nomJoueur) {
return line.equals("h");
}
}
System.out.println("\n\nVous avez répondu avec autre chose que 'h' ou 'a'.");
afficherErreur("Vous avez répondu avec autre chose que 'h' ou 'a'.");
}
}
}

0 comments on commit 57562ba

Please sign in to comment.