A. Rahman A. Rahman - 9 months ago 42
Java Question

How to make levels/rounds in my java hangman code?

I need help with trying to make levels/rounds in the game.please if anybody can include methods for the rounds that will help me a lot! Thanks! `

import java.util.Scanner;
import java.util.Arrays;

public class Hangman{

public static void main(String []args){

Scanner Input = new Scanner(System.in);
String original, reverse = "";
Scanner in = new Scanner(System.in);


System.out.println("Welcome to Hangman!");
System.out.println("Enter your name to play.");
original = in.nextLine();


int length = original.length();

for ( int i = length - 1 ; i >= 0 ; i-- )
reverse = reverse + original.charAt(i);

System.out.println("Your name entered in reverse is: "+reverse);
System.out.println("Press enter to continue:");
in.nextLine();


String[] CollectionOfWords =
{"","world","about","again","heart","pizza",
"earth","stars","light","music","party",
"Seven","three","happy","water","amber",
"apple","piano","green","mouth","suger",
"stone","japan","china","after","smile",
"south","house","stake","guess","wrong",
"thing","write","white","black","lemon",
"anime","static","outer","input","grand",
"lives","twice","print","hang","woman",
"power","today","India","night","candy",
"puppy","latin","vegan","phone","April",
"forum","birth","other","Chris","Irish",
"paste","queen","grace","crazy","plant",
"knife","spike","Darth","Vader","eagle",
"Egypt","range","fists","fight","glory",
"March","smart","magic","codes","rolls",
"match","honor","glass","board","teams",
"bully","Zebra","under","mango","brain",
"dirty","eight","zeros","train","cycle",
"break","necks","terms","slide","Large"};



int randNumber = (int) Math.ceil (Math.random() * CollectionOfWords.length);
int counter = 5;
String radmWord = CollectionOfWords[randNumber];

char[] genRadmLetter = radmWord.toCharArray();
char[] genRadmLetter2 = radmWord.toCharArray();
for (int x = 0; x<genRadmLetter.length; x++){
genRadmLetter[x]='-';
}

System.out.println(String.valueOf(genRadmLetter));
System.out.println("Hello"+" "+ reverse+" "+"guess a letter");
char guessedLetter = Input.next().charAt(0);
int RW = radmWord.indexOf(guessedLetter);

if (RW >= 0 ){

genRadmLetter[RW] = guessedLetter;
System.out.println(genRadmLetter);

}

if (RW == -1){
System.out.println("Wrong letter, try again.");
counter = counter - 1;
System.out.println("Lives left: " + counter);
}

while (counter != 0) {

System.out.println("Guess a letter.");
guessedLetter = Input.next().charAt(0);
RW = radmWord.indexOf(guessedLetter);

if (RW >= 0 ){

genRadmLetter[RW] = guessedLetter;
System.out.println(genRadmLetter);
}

if (RW == -1){
System.out.println("Wrong letter, try again.");
counter = counter - 1;
System.out.println("Lives left: " + counter);
}

boolean result = Arrays.equals(genRadmLetter, genRadmLetter2);
if (result == true){
return;
}

if (counter == 0){
break;
}

}

if (counter == 0){
System.out.println("You lost. The word was: " + radmWord);
System.out.println("Press enter to continue:");
Scanner scan = new Scanner(System.in);


String s1 = scan.nextLine();

if (s1.equals("quit")){

}

Scanner input = new Scanner(System.in);
while (true) {
System.out.println("Please enter your choice: ");
System.out.println("If you want to leave, type \"quit\"");

String s=input.next();
if(s.equals("quit")){
System.out.println("Thanks for playing the game!");
System.exit(0);
}

if(counter>0)
System.out.println("Well done, you have guessed the word.");
System.out.println("Your final score is: " + counter);


}
}
}
}

Answer Source

You need to do something like this.

public static void main(String[] args) {
        play();
    }

And then at the end, add something like this.

while (true) {
                System.out.println("Please enter your choice: ");
                System.out.println("If you want to leave, type \"quit\"");

                String s=input.next();
                if(s.equals("quit")){
                    System.out.println("Thanks for playing the game!");
                    System.exit(0);
                } else if (s.equals("play")) {
                    play();
                }

                if(counter>0)  
                    System.out.println("Well done, you have guessed the word.");
                System.out.println("Your final score is: " + counter);
            }

Move all that code into a play method.