forked from menault/TD3_DEV51_Qualite_Algo
Exo 3 appliqué
This commit is contained in:
@@ -7,13 +7,19 @@ public class GameState {
|
||||
private int errors;
|
||||
private Set<Character> triedLetters;
|
||||
private static final int MAX_ERRORS = 9;
|
||||
private long startTime;
|
||||
private int score;
|
||||
private String difficulty;
|
||||
|
||||
public GameState(String wordToGuess) {
|
||||
public GameState(String wordToGuess, String difficulty) {
|
||||
this.word = wordToGuess.toLowerCase();
|
||||
this.difficulty = difficulty;
|
||||
this.hiddenWord = new char[word.length()];
|
||||
Arrays.fill(hiddenWord, '_');
|
||||
this.triedLetters = new HashSet<>();
|
||||
this.errors = 0;
|
||||
this.score = 0;
|
||||
this.startTime = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
/*Fonction pour essayer une lettre*/
|
||||
@@ -32,15 +38,58 @@ public class GameState {
|
||||
if (!found) {
|
||||
errors++;
|
||||
}
|
||||
|
||||
// Mettre à jour le score après chaque tentative
|
||||
updateScore();
|
||||
}
|
||||
|
||||
/*Fonction pour vérifier si une lettre à déjà été essayé*/
|
||||
/*Calculer le score*/
|
||||
private void updateScore() {
|
||||
long currentTime = System.currentTimeMillis();
|
||||
long timeElapsed = (currentTime - startTime) / 1000; // en secondes
|
||||
|
||||
int baseScore = 1000;
|
||||
int timeBonus = Math.max(0, 300 - (int)timeElapsed) * 2; // Bonus temps
|
||||
int errorPenalty = errors * 50; // Pénalité erreurs
|
||||
int difficultyMultiplier = getDifficultyMultiplier();
|
||||
|
||||
score = Math.max(0, (baseScore + timeBonus - errorPenalty) * difficultyMultiplier);
|
||||
}
|
||||
|
||||
private int getDifficultyMultiplier() {
|
||||
switch (difficulty.toLowerCase()) {
|
||||
case "facile": return 1;
|
||||
case "moyen": return 2;
|
||||
case "difficile": return 3;
|
||||
default: return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/*Fonction pour obtenir le score final*/
|
||||
public int getFinalScore() {
|
||||
if (isWon()) {
|
||||
updateScore(); // Dernier calcul
|
||||
return score;
|
||||
}
|
||||
return 0; // Score 0 si perdu
|
||||
}
|
||||
|
||||
/*Fonction pour obtenir le temps écoulé*/
|
||||
public long getTimeElapsed() {
|
||||
return (System.currentTimeMillis() - startTime) / 1000;
|
||||
}
|
||||
|
||||
/*Fonction pour obtenir la difficulté*/
|
||||
public String getDifficulty() {
|
||||
return difficulty;
|
||||
}
|
||||
|
||||
// Les autres méthodes restent inchangées...
|
||||
public boolean hasTriedLetter(char letter) {
|
||||
letter = Character.toLowerCase(letter);
|
||||
return triedLetters.contains(letter);
|
||||
}
|
||||
|
||||
/*Fonction pour vérifier si on a gagné*/
|
||||
public boolean isWon() {
|
||||
for (char c : hiddenWord) {
|
||||
if (c == '_') {
|
||||
@@ -50,17 +99,14 @@ public class GameState {
|
||||
return true;
|
||||
}
|
||||
|
||||
/*Fonction pour vérifier si on a perdu*/
|
||||
public boolean isLost() {
|
||||
return errors >= MAX_ERRORS;
|
||||
}
|
||||
|
||||
/*Fonction pour voir le nombre d'erreur*/
|
||||
public int getErrors() {
|
||||
return errors;
|
||||
}
|
||||
|
||||
/*Fonction pour voir le mot caché*/
|
||||
public String getHiddenWord() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < hiddenWord.length; i++) {
|
||||
@@ -72,8 +118,7 @@ public class GameState {
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/*Fonction pour voir le mot*/
|
||||
public String getWord() {
|
||||
return word;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user