forked from menault/TD3_DEV51_Qualite_Algo
143 lines
3.9 KiB
Java
143 lines
3.9 KiB
Java
import java.util.*;
|
|
|
|
public class GameState {
|
|
|
|
private String word;
|
|
private char[] hiddenWord;
|
|
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, String difficulty) {
|
|
this.word = wordToGuess.toLowerCase();
|
|
this.difficulty = difficulty;
|
|
this.hiddenWord = new char[word.length()];
|
|
|
|
// INITIALISATION CORRIGÉE : montrer les espaces directement
|
|
for (int i = 0; i < word.length(); i++) {
|
|
char c = word.charAt(i);
|
|
if (c == ' ') {
|
|
hiddenWord[i] = ' '; // Espace visible dès le début
|
|
} else {
|
|
hiddenWord[i] = '_'; // Lettres cachées
|
|
}
|
|
}
|
|
|
|
this.triedLetters = new HashSet<>();
|
|
this.errors = 0;
|
|
this.score = 0;
|
|
this.startTime = System.currentTimeMillis();
|
|
|
|
// Ajouter l'espace comme lettre déjà "devinée"
|
|
triedLetters.add(' ');
|
|
}
|
|
|
|
/*Fonction pour essayer une lettre*/
|
|
public void tryLetter(char letter) {
|
|
letter = Character.toLowerCase(letter);
|
|
|
|
// Ne pas compter l'espace comme une tentative
|
|
if (letter == ' ') {
|
|
return;
|
|
}
|
|
|
|
triedLetters.add(letter);
|
|
boolean found = false;
|
|
|
|
for (int i = 0; i < word.length(); i++) {
|
|
if (word.charAt(i) == letter) {
|
|
hiddenWord[i] = letter;
|
|
found = true;
|
|
}
|
|
}
|
|
|
|
if (!found) {
|
|
errors++;
|
|
}
|
|
|
|
// Mettre à jour le score après chaque tentative
|
|
updateScore();
|
|
}
|
|
|
|
/*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;
|
|
}
|
|
|
|
public boolean hasTriedLetter(char letter) {
|
|
letter = Character.toLowerCase(letter);
|
|
return triedLetters.contains(letter);
|
|
}
|
|
|
|
public boolean isWon() {
|
|
for (int i = 0; i < hiddenWord.length; i++) {
|
|
// Ignorer les espaces dans la vérification
|
|
if (word.charAt(i) != ' ' && hiddenWord[i] == '_') {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public boolean isLost() {
|
|
return errors >= MAX_ERRORS;
|
|
}
|
|
|
|
public int getErrors() {
|
|
return errors;
|
|
}
|
|
|
|
public String getHiddenWord() {
|
|
StringBuilder sb = new StringBuilder();
|
|
for (int i = 0; i < hiddenWord.length; i++) {
|
|
sb.append(hiddenWord[i]);
|
|
if (i < hiddenWord.length - 1) {
|
|
sb.append(' ');
|
|
}
|
|
}
|
|
return sb.toString();
|
|
}
|
|
|
|
public String getWord() {
|
|
return word;
|
|
}
|
|
} |