feat(controllers/game): add score, chrono & finished game method

This commit is contained in:
2025-10-08 16:21:59 +02:00
parent 111dcad559
commit 7bc1bb498f

View File

@@ -1,44 +1,122 @@
package Controllers; package Controllers;
import Models.Word; import Models.Word;
import Views.HangedManView;
public class Game { public class Game {
private Word word; private Word word;
private int errors; private HangedManView hangedManView;
private final int maxErrors = 6;
private int errors;
private int maxErrors = 9;
private String wrongLetters;
private long startTime;
private long endTime;
private final int maxTime = 120000; // 2 minutes
// Constructor
public Game(Word word) { public Game(Word word) {
this.word = word; this.word = word;
this.hangedManView = hangedManView;
this.errors = 0; this.errors = 0;
this.wrongLetters = "";
this.startTime = System.currentTimeMillis();
this.endTime = 0;
} }
/* Verify error number */ // Getters - Errors
public int getErrors() { public int getErrors() {
return this.errors; return this.errors;
} }
/* Put error max */
public int getMaxErrors() { // Getters - Wrong Letters
return this.maxErrors; public String getWrongLetters() {
return this.wrongLetters;
} }
/* Vue call method VerifyLetter, check status letter false/True */ // Determine if the game is lost
private boolean isLost() {
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) { public boolean playLetter(char c) {
boolean correct = word.VerifyLetter(c); boolean correct = word.VerifyLetter(c);
if (!correct) { if (!correct) {
errors++; errors++;
hangedManView.setState(errors);
wrongLetters += c + " ";
hangedManView.setWrongLetters(wrongLetters);
if (isLost()) {
finishedGame();
}
} else {
hangedManView.setCorrectLetters(word.getHiddenWord());
if (word.IsComplete()) {
finishedGame();
}
} }
return correct; return correct;
} }
/* All letter is completed : Won */ // Finish the game
public boolean isWon() { private boolean finishedGame() {
return word.IsComplete(); this.endTime = System.currentTimeMillis();
if (isLost()) {
return false;
} }
/* Lost if maxErrors is greater than or egal to errors */ boolean isWon = isWon();
public boolean isLost() { return isWon;
return errors >= maxErrors; }
// 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;
} }
} }