Merge branch 'NewControllers'

# Conflicts:
#	TD3_DEV51_Qualite_Algo/src/main/java/fr/iutfbleau/TD3_DEV51_Qualite_Algo/Controllers/Game.java
#	TD3_DEV51_Qualite_Algo/src/main/java/fr/iutfbleau/TD3_DEV51_Qualite_Algo/Models/Word.java
This commit is contained in:
2025-10-08 16:44:28 +02:00
2 changed files with 107 additions and 14 deletions

View File

@@ -4,19 +4,32 @@ import fr.iutfbleau.TD3_DEV51_Qualite_Algo.Models.Difficulty;
import fr.iutfbleau.TD3_DEV51_Qualite_Algo.Models.Word; import fr.iutfbleau.TD3_DEV51_Qualite_Algo.Models.Word;
import java.util.*; import java.util.*;
import fr.iutfbleau.TD3_DEV51_Qualite_Algo.Models.Word;
import fr.iutfbleau.TD3_DEV51_Qualite_Algo.View.hangedManView;
public class Game { public class Game {
private Word word; private Word word;
private hangedManView hangedManView;
private int errors; private int errors;
private int maxErrors = 9;
private String wrongLetters;
private long startTime;
private long endTime;
private final int maxTime = 120000; // 2 minutes
private List<Word> allwords; private List<Word> allwords;
private Difficulty difficulty; private Difficulty difficulty;
private final int maxErrors = 6;
/* Hangmam Game */ /* Hangmam Game */
public Game(Word word ,List<Word> allWord) { // Constructor
public Game(Word word ,List<Word> allWord,hangedManView hangedManView) {
this.word = word; this.word = word;
this.hangedManView = hangedManView;
this.errors = 0; this.errors = 0;
this.wrongLetters = "";
this.startTime = System.currentTimeMillis();
this.endTime = 0;
switch (difficulty) { switch (difficulty) {
case EASY: case EASY:
this.word = selectWord(allWord, 0 , 7 ); this.word = selectWord(allWord, 0 , 7 );
@@ -43,7 +56,7 @@ public class Game {
/* Selected word level Difficult */ /* Selected word level Difficult */
private List<Word> selectTwoWords(List<Word> list) { private List<Word> selectTwoWords(List<Word> list) {
return list.subList(0, Math.min(2, list.size())); return list.subList(0, Math.min(2, list.size()));
} }
public boolean playLetter(char caractere) { public boolean playLetter(char caractere) {
@@ -59,7 +72,7 @@ public class Game {
return correct; return correct;
} }
/* Verify error number */ // Getters - Errors
public int getErrors() { public int getErrors() {
return this.errors; return this.errors;
} }
@@ -67,14 +80,94 @@ public class Game {
public int getMaxErrors() { public int getMaxErrors() {
return this.maxErrors; return this.maxErrors;
} }
// Getters - Wrong Letters
/* All letter is completed : Won */ public String getWrongLetters() {
public boolean isWon() { return this.wrongLetters;
return word.IsComplete();
} }
/* Lost if maxErrors is greater than or egal to errors */ // Determine if the game is lost
public boolean isLost() { private boolean isLost() {
return errors >= maxErrors; return errors >= maxErrors;
} }
}
// Determine if the game is won
public boolean isWon() {
int score = calculateScore();
if (word.IsComplete() && score > 0) {
return true;
}
return false;
}
// From view, verify if the letter is correct & finish the game if necessary (lost or won)
public boolean playLetter(char c) {
boolean correct = word.VerifyLetter(c);
if (!correct) {
errors++;
hangedManView.setState(errors);
wrongLetters += c + " ";
hangedManView.setWrongLetters(wrongLetters);
if (isLost()) {
finishedGame();
}
} else {
hangedManView.setCorrectLetters(word.getHiddenWord());
if (word.IsComplete()) {
finishedGame();
}
}
return correct;
}
// Finish the game
private boolean finishedGame() {
this.endTime = System.currentTimeMillis();
if (isLost()) {
return false;
}
boolean isWon = isWon();
return isWon;
}
// For view, return the remaining time
public long getRemainingTime() {
long elapsed = (System.currentTimeMillis() - startTime) / 1000;
long remaining = (maxTime / 1000) - elapsed;
if (remaining < 0) {
remaining = 0;
finishedGame();
}
return Math.max(remaining, 0);
}
// Get elapsed time for calculateScore
private long calculateTime() {
long elapsedTime = (endTime - startTime) / 1000;
return elapsedTime;
}
// Calculate Score
public int calculateScore() {
long time = calculateTime();
int baseScore = 1000;
int errorPenalty = errors * 50;
int timePenalty = (int) time * 2;
int score = baseScore - errorPenalty - timePenalty;
if (score < 0) {
score = 0;
}
return score;
}
}

View File

@@ -39,5 +39,5 @@ public class Word {
public String getWord() { public String getWord() {
return word; return word;
} }
} }