Ajout de la méthode lancerJeu() avec toute la logique du programme
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package com.humanbooster.exercices;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class JeuDevinette {
|
||||
|
||||
private int nombreADeviner;
|
||||
@@ -16,6 +18,35 @@ public class JeuDevinette {
|
||||
this.tentatives = new int[MAX_TENTATIVES];
|
||||
}
|
||||
|
||||
public void lancerJeu() {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
System.out.println("Bienvenue dans le jeu de devinettes !");
|
||||
System.out.println("Devinez le nombre entre " + MIN + " et " + MAX +". Vous avez " + MAX_TENTATIVES + " tentatives.");
|
||||
|
||||
while (nombreDeTentatives < MAX_TENTATIVES) {
|
||||
System.out.print("Tentative " + (nombreDeTentatives + 1) + " : ");
|
||||
int proposition = scanner.nextInt();
|
||||
tentatives[nombreDeTentatives] = proposition;
|
||||
nombreDeTentatives++;
|
||||
|
||||
if (proposition == nombreADeviner) {
|
||||
System.out.println("Bravo ! Vous avez deviné le nombre en " + nombreDeTentatives + " tentatives.");
|
||||
break;
|
||||
} else if (proposition < nombreADeviner) {
|
||||
System.out.println("C'est plus grand !");
|
||||
} else {
|
||||
System.out.println("C'est plus petit");
|
||||
}
|
||||
|
||||
if (nombreDeTentatives == MAX_TENTATIVES) {
|
||||
System.out.println("Désolé, vous avez atteint le nombre maximum de tentatives.");
|
||||
System.out.println("Le nombre était : " + nombreADeviner);
|
||||
}
|
||||
}
|
||||
|
||||
afficherHistorique();
|
||||
}
|
||||
|
||||
private int getRandomBetween(int min, int max) {
|
||||
return (int)(Math.random() * (max - min + 1)) + min;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user