I am coding Hangman game with javaFX, somehow the code assumes all of my guesses are wrong and always considered a mistake even if it’s correct. So the user ends up always losing. I think it has something to do with the while loop I am using to make sure all redundancy of any letter is dealt with (like in “banana” if the user enters “a”, the currect progress should be displayed as “-a-a-a” instead of “-a—-“) but i don’t know: here is my controller class as well as word, if you need any other classes let me know!
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.List;
public class Controller {
//inject an element defined in fxml to controller!
@FXML
//pane is a form container
private Pane man;
@FXML
private Pane base1;
@FXML
private Pane base2;
@FXML
private Pane base3;
@FXML
private Pane pole;
@FXML
private Pane rod;
@FXML
private Pane rope1;
@FXML
private Pane rope2;
@FXML
private Text text;
@FXML
private Pane buttons;
@FXML
private Text winStatus;
@FXML
private Text realWord;
private int mistakes;
private int correct;
private String[] table;
private String word;
private String category;
private List<String> myLetters;
private List<String> answer;
public Controller(){
}
public void initialize() {
base1.setVisible(false);
base2.setVisible(false);
base3.setVisible(false);
pole.setVisible(false);
rod.setVisible(false);
rope1.setVisible(false);
rope2.setVisible(false);
man.setVisible(false);
buttons.setDisable(false);
mistakes=0;
correct=0;
table = Words.getRandomWord(2);
word=table[1];
category=table[0];
myLetters = Arrays.asList(word.split(""));
answer = Arrays.asList(new String[myLetters.size()*2]);
for(int i=0; i<myLetters.size()*2; i++){
if(i%2==0){
answer.set(i, "_");
}else{
answer.set(i, " ");
}
}
String res = String.join("", answer);
text.setText(res);
//hide winstatus and realword in case of a new game!
winStatus.setText("");
realWord.setText("");
}
//when do i call for u ?when the event i declared happened!
public void onClick(ActionEvent event){
//getText return string not char
String letter = ((Button)event.getSource()).getText();
((Button) event.getSource()).setDisable(true);
if(myLetters.contains(letter)){
correct++;
int letterIndex = myLetters.indexOf(letter);
while (letterIndex != -1) {
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);
}
}
else{
mistakes++;
if(mistakes ==1) base1.setVisible(true);
else if (mistakes ==2) base2.setVisible(true);
else if (mistakes ==3) base3.setVisible(true);
else if (mistakes ==4) pole.setVisible(true);
else if (mistakes ==5) rod.setVisible(true);
else if (mistakes ==6) rope1.setVisible(true);
else if (mistakes ==7) rope2.setVisible(true);
else if (mistakes ==8){
rope2.setVisible(false); //7bal lmachan9a around the neck
man.setVisible(true);//sad face+body
winStatus.setText("You Lose!");
realWord.setText("The actual word was " + word);
buttons.setDisable(true);
}
}
}
public void newGame(){
//i don't get what does the loops does
//why enable here and enable in initialize?
for(int i=0; i<26; i++){
buttons.getChildren().get(i).setDisable(false);
}
initialize();
}
}
package sample;
import java.util.Random;
public class Words{
public static String[] getRandomWord(int languageChoice) {
String[] fruits = {"strawberry", "banana", "orange", "apple", "cherry", "peach", "kiwi", "lemon", "grape", "mango"};
String[] countries = {"tunisia", "spain", "japan", "qatar", "greece", "palestine", "italy", "canada", "germany", "china"};
String[] animals = {"shark", "wolf", "cat", "cow", "snake", "horse", "dog", "lion", "rabbit", "zebra"};
String[] languages = {"arabic", "korean", "english", "russian", "turkish", "hindi", "spanish", "french", "german", "chinese"};
String[] occupations = {"doctor", "engineer", "teacher", "baker", "clown", "pilot", "lawyer", "chef", "nurse", "police"};
String[] fruit = {"fraise", "bananes", "orange", "pomme", "cerise", "pêche", "kiwi", "citron", "raisin", "mangue"};
String[] pays = {"tunisie", "espagne", "japon", "qatar", "grèce", "palestine", "italie", "canada", "allemagne", "chine"};
String[] animaux = {"requin", "loup", "chat", "vache", "serpent", "cheval", "chien", "lion", "lapin", "zèbre"};
String[] langues = {"arabe", "coréen", "anglais", "russe", "turc", "hindi", "espagnol", "français", "allemand", "chinois"};
String[] professions = {"médecin", "ingénieur", "enseignant", "boulanger", "clown", "pilote", "avocat", "chef", "infirmier", "police"};
String category=null;
String word=null;
int categoryIndex = new Random().nextInt(5)+1;
if (languageChoice == 2) {
switch (categoryIndex) {
case 1:
category = "fruits";
word = fruits[new Random().nextInt(fruits.length)];
break;
case 2:
category = "countries";
word = countries[new Random().nextInt(countries.length)];
break;
case 3:
category = "animals";
word = animals[new Random().nextInt(animals.length)];
break;
case 4:
category = "languages";
word = languages[new Random().nextInt(languages.length)];
break;
case 5:
category = "occupations";
word = occupations[new Random().nextInt(occupations.length)];
break;
}
}
else {
switch (categoryIndex) {
case 1:
category = "fruits";
word = fruit[new Random().nextInt(fruit.length)];
break;
case 2:
category = "pays";
word = pays[new Random().nextInt(pays.length)];
break;
case 3:
category = "animaux";
word = animaux[new Random().nextInt(animaux.length)];
break;
case 4:
category = "langues";
word = langues[new Random().nextInt(langues.length)];
break;
case 5:
category = "professions";
word = professions[new Random().nextInt(professions.length)];
break;
}
}
return new String[]{category, word};
}
} ```