66 lines
2.2 KiB
Java
66 lines
2.2 KiB
Java
package com.humanbooster.exercices;
|
|
|
|
import java.util.Scanner;
|
|
|
|
public class JeuDevinette {
|
|
|
|
private int nombreADeviner;
|
|
private int nombreDeTentatives;
|
|
private int[] tentatives;
|
|
private final int MAX_TENTATIVES = 10;
|
|
|
|
private final int MIN = 1;
|
|
private final int MAX = 100;
|
|
|
|
public JeuDevinette() {
|
|
this.nombreADeviner = getRandomBetween(MIN, MAX);
|
|
this.nombreDeTentatives = 0;
|
|
this.tentatives = new int[MAX_TENTATIVES];
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
JeuDevinette jeu = new JeuDevinette();
|
|
jeu.lancerJeu();
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
private void afficherHistorique() {
|
|
System.out.println("\nHistorique des tentatives :");
|
|
for (int i = 0; i < nombreDeTentatives; i++) {
|
|
System.out.println("Tentative " + (i + 1) + " : " + tentatives[i]);
|
|
}
|
|
}
|
|
}
|