forked from menault/TD3_DEV51_Qualite_Algo
ajout du chrono et score
This commit is contained in:
@@ -7,7 +7,18 @@ import java.util.Set;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Logique principale du jeu du pendu (back).
|
* Logique principale du jeu du pendu (back).
|
||||||
* Gère le mot, les lettres trouvées, et les conditions de victoire/défaite.
|
* Ajoute un score et un chronomètre.
|
||||||
|
*
|
||||||
|
* Règles de score (simples) :
|
||||||
|
* - Lettre correcte : +10 points
|
||||||
|
* - Lettre incorrecte: -5 points (le score ne descend pas sous 0)
|
||||||
|
* - Bonus victoire : + (restant * 10) + bonus temps
|
||||||
|
* * bonus temps = max(0, 60 - secondes) * 2 (jusqu'à 120 pts si < 60s)
|
||||||
|
* - Défaite : pas de bonus
|
||||||
|
*
|
||||||
|
* Chronomètre :
|
||||||
|
* - Démarre à la création de la partie
|
||||||
|
* - S'arrête définitivement à la fin (victoire/défaite)
|
||||||
*/
|
*/
|
||||||
public class Game {
|
public class Game {
|
||||||
private final String word;
|
private final String word;
|
||||||
@@ -16,21 +27,32 @@ public class Game {
|
|||||||
private final int maxErrors;
|
private final int maxErrors;
|
||||||
private int errors;
|
private int errors;
|
||||||
|
|
||||||
|
// Score & chrono
|
||||||
|
private int score = 0;
|
||||||
|
private long startNano; // début de partie (System.nanoTime)
|
||||||
|
private long endNano = -1L; // fin (sinon -1 = en cours)
|
||||||
|
|
||||||
public Game(String word, int maxErrors) {
|
public Game(String word, int maxErrors) {
|
||||||
this.word = word.toLowerCase();
|
this.word = word.toLowerCase();
|
||||||
this.maxErrors = maxErrors;
|
this.maxErrors = maxErrors;
|
||||||
|
this.startNano = System.nanoTime(); // démarre le chrono à la création
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Tente une lettre et renvoie le résultat */
|
/** Tente une lettre et renvoie le résultat + ajuste le score */
|
||||||
public Result play(char letter) {
|
public Result play(char letter) {
|
||||||
char c = Character.toLowerCase(letter);
|
char c = Character.toLowerCase(letter);
|
||||||
if (all.contains(c)) return Result.ALREADY;
|
if (all.contains(c)) return Result.ALREADY;
|
||||||
|
|
||||||
all.add(c);
|
all.add(c);
|
||||||
if (word.indexOf(c) >= 0) {
|
if (word.indexOf(c) >= 0) {
|
||||||
correct.add(c);
|
correct.add(c);
|
||||||
|
addScore(10);
|
||||||
|
// Si la lettre trouvée fait gagner immédiatement, on finalise ici
|
||||||
|
if (isWin()) end(true);
|
||||||
return Result.HIT;
|
return Result.HIT;
|
||||||
} else {
|
} else {
|
||||||
errors++;
|
addScore(-5);
|
||||||
|
if (isLose()) end(false);
|
||||||
return Result.MISS;
|
return Result.MISS;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -62,7 +84,7 @@ public class Game {
|
|||||||
return errors >= maxErrors;
|
return errors >= maxErrors;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Renvoie le nombre d'erreurs actuelles */
|
/** Nombre d'erreurs actuelles */
|
||||||
public int getErrors() { return errors; }
|
public int getErrors() { return errors; }
|
||||||
|
|
||||||
/** Liste les lettres déjà essayées */
|
/** Liste les lettres déjà essayées */
|
||||||
@@ -73,4 +95,38 @@ public class Game {
|
|||||||
for (Character ch : sorted) out.add(String.valueOf(ch));
|
for (Character ch : sorted) out.add(String.valueOf(ch));
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
// -Score & Chrono
|
||||||
|
|
||||||
|
/** Retourne le score courant */
|
||||||
|
public int getScore() { return score; }
|
||||||
|
|
||||||
|
/** Secondes écoulées depuis le début (si finie, temps figé) */
|
||||||
|
public long getElapsedSeconds() {
|
||||||
|
long end = (endNano > 0L) ? endNano : System.nanoTime();
|
||||||
|
long deltaNs = end - startNano;
|
||||||
|
if (deltaNs < 0) deltaNs = 0;
|
||||||
|
return deltaNs / 1_000_000_000L;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Termine la partie (victoire/défaite) et applique le bonus si gagné */
|
||||||
|
public void end(boolean win) {
|
||||||
|
if (endNano > 0L) return; // déjà terminé
|
||||||
|
endNano = System.nanoTime();
|
||||||
|
if (win) {
|
||||||
|
int remaining = Math.max(0, maxErrors - errors);
|
||||||
|
int timeBonus = (int) Math.max(0, 60 - getElapsedSeconds()) * 2;
|
||||||
|
addScore(remaining * 10 + timeBonus);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- utilitaires privés ---
|
||||||
|
private void addScore(int delta) {
|
||||||
|
if (delta < 0) {
|
||||||
|
// lettre ratée => +1 erreur
|
||||||
|
errors++;
|
||||||
|
}
|
||||||
|
score += delta;
|
||||||
|
if (score < 0) score = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user