Hello everybody,
I'm stuck with my Java program. This is the point. When a user had tried for nine times, the user has the choice to restart Hangman or close the program.
But when they hit the y or n key during the game, it's just one of the 26 characters.
I have add the code of my main.java
Thanks in advance for all your help :)
Kind regards,
MeandJava
package hangman;
/*
* Author Patrick
*/
class Galg {
public void print(int i) {
switch(i) {
case 0:
System.out.println("____________________________________________________________________");
System.out.println(" * * * * * ********* * * * * * ");
System.out.println(" * * * * * * * * * * * * * * * * * ");
System.out.println(" ********* ***** * * * * *** * * * ***** * * * ");
System.out.println(" * * * * * ** * * * * * * * ** ");
System.out.println(" * * * * * * ********* * * * * * * ");
System.out.println("____________________________________________________________________");
System.out.println("Press q to quit this game.");
System.out.println(" ");
System.out.println("| ");
System.out.println("| ");
System.out.println("| ");
System.out.println("| _ _ ");
break;
case 1:
System.out.println("_____ ");
System.out.println("| ");
System.out.println("| ");
System.out.println("| ");
System.out.println("| _ _ ");
break;
case 2:
System.out.println("_____ ");
System.out.println("|/ ");
System.out.println("| ");
System.out.println("| ");
System.out.println("| _ _ ");
break;
case 3:
System.out.println("_____ ");
System.out.println("|/ | ");
System.out.println("| ");
System.out.println("| ");
System.out.println("| _ _ ");
break;
case 4:
System.out.println("_____ ");
System.out.println("|/ | ");
System.out.println("| 0 ");
System.out.println("| ");
System.out.println("| _ _ ");
break;
case 5:
System.out.println("_____ ");
System.out.println("|/ | ");
System.out.println("| 0 ");
System.out.println("| | ");
System.out.println("| ");
System.out.println("| _ _ ");
break;
case 6:
System.out.println("_____ ");
System.out.println("|/ | ");
System.out.println("| 0 ");
System.out.println("| /| ");
System.out.println("| ");
System.out.println("| _ _ ");
break;
case 7:
System.out.println("_____ ");
System.out.println("|/ | ");
System.out.println("| 0 ");
System.out.println("| /|\\");
System.out.println("| ");
System.out.println("| _ _ ");
break;
case 8:
System.out.println("_____ ");
System.out.println("|/ | ");
System.out.println("| 0 ");
System.out.println("| /|\\");
System.out.println("| / ");
System.out.println("| _ _ ");
break;
case 9:
System.out.println("_____ ");
System.out.println("|/ | ");
System.out.println("| 0 ");
System.out.println("| /|\\");
System.out.println("| / \\");
System.out.println("| _ _ ");
System.out.println();
System.out.println(" _____________________________________________________________________________________ ");
System.out.println("| ********* * * * ********* ********* * * ********* ********* |");
System.out.println("| * * * * * * * * * * * * * * * |");
System.out.println("| * *** ***** * * * ****** * * * * ****** ********* |");
System.out.println("| * * * * * * * * * * * * * * |");
System.out.println("| ********* * * * * ********* ********* * ********* * * |");
System.out.println("|_____________________________________________________________________________________|");
}
}
}
public class Main {
public static void main(String args[])
throws java.io.IOException {
// Loops stays running when the right characters are not guessed.
GuessedLetters GL = new GuessedLetters();
Galg g = new Galg(); //New object Galg
Word h = new Word(); // New object Word
int wrong = 0;
int tries = 0;
for(;;) {
tries++;
g.print(wrong);
System.out.println();
char keuzes = 0;
System.out.println("Next Letter: ");
while (keuzes < 'a' | keuzes > 'z'){
keuzes = (char) System.in.read();
}
GL.add(keuzes);
GL.print();
boolean geraden = h.processGuess(keuzes);
if(h.hasBeenGuessed() == true) {
System.out.println("Congratulations! You guessed the word! \nWanna try again press yes: y or no: n.");
break;
}
h.print();
System.out.println("********************************************************************");
if (keuzes == 'q') {
break;
}
if(geraden == false) {
wrong++;
}
if(tries == 9){
System.out.println("You are dead! \nWanna try again? Press yes: y or no: n");
while (keuzes == 'y' | keuzes == 'n'){
keuzes = (char) System.in.read();
}
if(keuzes == 'y') {
System.out.println("Hangman will restart. Wait a second...");
wrong = 0;
tries = 0;
continue;
}
if(keuzes == 'n'){
System.out.println("Thanks for playing. See you next time!");
break;
}
}
}
}
}