Sometimes I am halfway through the Hangman game and the “time is up” alert interrupts me not on its perfect time. The remaining time is 40 secs as an example and it tells me my time is up, but the problem is that this happens sometimes and NOT in every single game.It might be due to the JavaFX timer features but I’m no pro. Maybe there is sthg I need to know. I posted asking for help after trying to figure it out myself and not being able to, I don’t need someone to come tell me this is a code dump. I wouldn’t have posted if I figured it out myself in the 1st place!
Controller class:
```
//libraries import
public class Controller {
//other FXML elements
@FXML
private Text time;
@FXML
private Text time1;
//variables used defined;
public void initialize() {
language1.setText("Please select a language 1.English 2.French ");
english.setVisible(true);
french.setVisible(true);
//all others elements are invisible
}
public void onClick(ActionEvent event){
String letter = ((Button)event.getSource()).getText().toLowerCase();
((Button) event.getSource()).setDisable(true);
time1.setText("");
String str1 = timer.remain();
time.setText("The remining time is "+str1+" seconds");}
if(myLetters.contains(letter)){
int letterIndex = myLetters.indexOf(letter);
while (letterIndex != -1) {
correct++;
myLetters.set(letterIndex, "*");
answer.set(letterIndex*2, letter);
letterIndex = myLetters.indexOf(letter);
String res = String.join("", answer);
text.setText(res);
}
if(correct==word.length()){
winStatus.setText("You Win!");}
buttons.setDisable(true);
timer.stop();
}
}
else{
mistakes++;
if(timer.isFinished()){
timer.stop();
timer.showAlertEnglish();
buttons.setDisable(true);
}
if(mistakes ==1) base1.setVisible(true);
// show other parts of hangman
else if (mistakes ==8){
man.setVisible(true);
winStatus.setText("You Lose!");
realWord.setText("The actual word was " + word);
buttons.setDisable(true);
timer.stop();
}
}
}
public void select(ActionEvent event) {
//chnages the game due to the selected language
time1.setText("Vous avez 1 minute. Allez !");
timer.start();
}
public void newGame(){
time.setText("");
for(int i=0; i<35; i++){
buttons.getChildren().get(i).setDisable(false);
}
newGameButton.setText("");
initialize();
}
}```
Timer class:
``` mport java.util.Timer;
import java.util.TimerTask;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
public class timer {
private static long startTime;
private static long remaining;
private static long remainingTimeSeconds;
private static long remainingTimeMilliSeconds;
private static boolean timeUp;
private static Timer timer;
public static void start() {
startTime = System.currentTimeMillis();
timeUp = false;
timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
timeUp = true;
}
}, 60 * 1000);
}
public static String remain() {
remaining=(60 * 1000) - (System.currentTimeMillis() - startTime);
remainingTimeSeconds = (remaining)/ 1000;
remainingTimeMilliSeconds= (remaining)%1000;
if (remainingTimeMilliSeconds<0) {remainingTimeMilliSeconds=0;}
String ch=String.valueOf(remainingTimeSeconds)+"."+String.valueOf(remainingTimeMilliSeconds);
return ch;
}
public static boolean isFinished() {
return timeUp;
}
public static void stop() {
timeUp=true;
}
public static void showAlertEnglish() {
Alert alert = new Alert(AlertType.WARNING);
alert.setTitle("Time's Up!");
alert.setHeaderText(null);
alert.setContentText("You ran out of time. You lose!");
alert.showAndWait();
}
public static void showAlertFrench() {
Alert alert = new Alert(AlertType.WARNING);
alert.setTitle("Temps écoulé !");
alert.setHeaderText(null);
alert.setContentText("Vous avez dépassé le temps imparti. Vous avez perdu !");
alert.showAndWait();
}
}
```