I have to complete this programming homework which consists of building a hangman game. However, the problem is: I do not know how to make it so that, when you guess a correct letter, it reveals that letter in the hidden word while still keeping the other parts of the word hidden.
For example :
The hidden word is REVEAL.
You guess "E".
It should now show ".E.E.." in the user's screen.
If you guys could help me out with this, I'd be extremely grateful, especially since I'm in a big hurry!
By the way, since I live in Quebec, Canada, French is our primary language, so my code contains some French on it (like the name of the variables and the text printed on the screen, which I all translate in comments).
Secondly, since we haven't learned how to use "import", we have to use a class named "Keyboard" in order to ask the user to type something. For a String, we use "variable = Keyboard.readString();.
public class Pendu {
public static void main(String[] args) {
String difficulte, motMystere, lettreEntree, nouvellePartie, lettresDejaEntrees = ""; // difficulte = difficulty ; motMystere = hidden word ; lettreEntree = letter guessed ; nouvellePartie = new game ; lettresDejaEntrees = letters already guessed
int nombreErreurs, nombreLettres, numeroEssai = 1; // nombreErreurs = number of mistakes ; nombreLettres = number of letters in the word ; numeroEssai = which attempt it is (attempt #1, ect.)
boolean invalideOuNon = true; //invalideOuNon = a boolean which checks if the letter is invalid or not (it has to be from A to Z or a question mark(a question marks makes you give up) to be valid)
System.out.println("JEU DU BONHOMME PENDU");
System.out.println("---------------------");
System.out.println("");
do {
System.out.print("Entrez le niveau de difficulte (F=Facile, I=Intermediaire, D=Difficile) : "); // Facile = Easy ; Intermediaire = Intermediate ; Difficile = Difficult
difficulte = Keyboard.readString().toUpperCase();
}
while (!difficulte.equals("F") && !difficulte.equals("I") && !difficulte.equals("D"));
if (difficulte.equals("F"))
nombreErreurs = 8;
else if (difficulte.equals("I"))
nombreErreurs = 6;
else
nombreErreurs = 4;
motMystere = Dictionnaire.genereMot();
motMystere = motMystere.toUpperCase();
nombreLettres = motMystere.length();
System.out.println("Vous devez trouvez le mot mystere en faisant moins de " +nombreErreurs +" erreurs."); // "You have to find the hidden word without exceeding said number of mistakes."
System.out.println("Generation d'un mot mystere..."); // "Generating a hidden word..."
System.out.println("Le mot mystere contient " +nombreLettres +" lettres."); // "The hidden word contains said number of letters."
do {
// This for loop prints on the screen a number of dots corresponding to the number of letters in the hidden word.
for (i = 0; i < nombreLettres; i++) {
System.out.print(".");
}
System.out.print("Essai #" +numeroEssai +", entrez une lettre (A-Z) ou ? pour abandonner : "); // "Guess a letter (A-Z) or type ? to give up."
lettreEntree = Keyboard.readString().toUpperCase();
lettreInvalideOuNon = lettreEntree.matches("[^A-Z?]"); // This checks if the character typed by the user is valid (am I doing it right, by the way?).
if (lettreInvalideOuNon == false) {
System.out.println("Veuillez entrez une lettre ou symbole valide."); // "Please enter a valid letter."
while
}
else if (lettreEntree.equals("?")) {
System.out.println("Vous avez perdu!"); // "You lost!"
System.out.println("La solution du mot mystere etait " +motMystere +"."); // "The solution was ..."
do {
System.out.println("Voulez-vous jouer une autre partie du Bonhomme Pendu (O/N)? "); // "Would you like to play another game (Y/N)?"
nouvellePartie = Keyboard.readString().toUpperCase();
// Will come back here later.
if (nouvellePartie == "O") {
}
else (nouvellePartie == "N") {
}
while (!nouvellePartie.equals("O") && !nouvellePartie.equals("N"))
else (lettreInvalideOuNon == true) {
if (lettresDejaEntrees.contains(lettreEntree)) {
System.out.println("Vous avez deja essaye la lettre " +lettreEntree +", essayez encore."); // "You already guessed this letter, please try again."
while
else // THIS is the part I DON'T understand.
if (motMystere.contains(lettreEntree)) { // This checks if the hidden word contains the guessed letter.
// What do I do here?