From e6354d9b48d44005e41b2bd9e624160dad88372b Mon Sep 17 00:00:00 2001 From: Vincent Guillet Date: Mon, 28 Apr 2025 17:58:41 +0200 Subject: [PATCH] =?UTF-8?q?Ajout=20de=20la=20m=C3=A9thode=20lancerJeu()=20?= =?UTF-8?q?avec=20toute=20la=20logique=20du=20programme?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- com/humanbooster/exercices/JeuDevinette.java | 31 ++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/com/humanbooster/exercices/JeuDevinette.java b/com/humanbooster/exercices/JeuDevinette.java index 3d60a8f..fc52d4e 100644 --- a/com/humanbooster/exercices/JeuDevinette.java +++ b/com/humanbooster/exercices/JeuDevinette.java @@ -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; }