TazZat
TazZat

Reputation: 1

Guessing letters, matching letters, and replacing underlines (Java beginner hangman game)

I'm working on a hangman game for a beginner's Java course and I'm completely stuck. I can't figure out or understand how to replace a letter when it is guessed (turning "_ _ _ _ _" into "h _ _ _ _" when "h" is inputted as a guess) . Along with that I can't figure out how to guess letters in the first place and have a function check them to make sure they match with the inputted word and tell the user when it is correct. I would love some feedback!

Here is the code I have so far:

import java.util.*; // Imports Java Utiliy packages for later use

public class Hangman {

private static Scanner input = new Scanner(System.in);
public static String userSubmittedWord;
public static String userGuessedLetter;
private static int remainingAttempts = 0;


public static void main(String[] args) {
    
    System.out.println("Welcome to Hangman. Please enter a word.");
    userSubmittedWord = input.next().toString();
    
    System.out.println("Your word is: " + userSubmittedWord + ". Press the enter key to continue.");
    try
    {
        System.in.read();
    }  
    catch(Exception e)
    {}  

    System.out.print("\033[H\033[2J");  
    System.out.flush(); 

    String []ul = new String[userSubmittedWord.length()];

    System.out.println("Please enter a singular letter as a guess, or attempt to guess the whole word.");

    for(int i = 0; i < userSubmittedWord.length(); i++){
        ul[i] = "_ ";
        System.out.print(ul[i]);
    }

    System.out.println(" ");
    userGuessedLetter = input.next();

    if(userSubmittedWord.equals(userGuessedLetter)){
        System.out.println("Congratulations! You have won. The word was: " + userSubmittedWord);
    }

    boolean correct = false;
    for(int i = 0; i < userSubmittedWord.length(); i++){
        if (userSubmittedWord.substring(i, i+1).equalsIgnoreCase(userGuessedLetter)) {
        correct = true;
        }
    }
}

public static void hangmanImage() {
    if (remainingAttempts == 1) {
        System.out.println("Incorrect. Guess again, you have 5 guesses remaining.");
        System.out.println(" | ");
        System.out.println(" O ");
        System.out.println();
    }
    if (remainingAttempts == 2) {
        System.out.println("Tough luck. Guess again, you have 4 guesses remaining.");
        System.out.println(" | ");
        System.out.println(" O ");
        System.out.println(" | ");
        System.out.println(" | ");
        System.out.println();
    }
    if (remainingAttempts == 3) {
        System.out.println("Ihat's unfortunate. Guess again, you have 3 guesses remaining.");
        System.out.println(" | ");
        System.out.println(" O ");
        System.out.println("/| ");
        System.out.println(" | ");
        System.out.println();

    }
    if (remainingAttempts == 4) {
        System.out.println("Sad. Guess again, you have 2 guesses remaining.");
        System.out.println(" | ");
        System.out.println(" O ");
        System.out.println("/|\\ ");
        System.out.println(" | ");
        System.out.println();
    }
    if (remainingAttempts == 5) {
        System.out.println("Pain. Guess again, you have 1 guesses remaining.");
        System.out.println(" | ");
        System.out.println(" O ");
        System.out.println("/|\\ ");
        System.out.println(" | ");
        System.out.println("/ ");
        System.out.println();
    }
    if (remainingAttempts == 6) {
        System.out.println("Better luck next time my friend...");
        System.out.println(" | ");
        System.out.println(" O ");
        System.out.println("/|\\ ");
        System.out.println(" | ");
        System.out.println("/ \\");
        System.out.println();
        System.out.println("GAME OVER! The word was " + userSubmittedWord);
    }
}
}

Upvotes: 0

Views: 190

Answers (1)

imraklr
imraklr

Reputation: 317

You don't need to replace the underscores just use an empty String, here I've used String ul="";. Modifications in your main method:-

public static void main(String[] args) {
    System.out.println("Welcome to Hangman. Please enter a word.");
    userSubmittedWord = input.next().toString();
    int len = userSubmittedWord.length();
    System.out.println("Your word is: " + userSubmittedWord + ". Press the enter key to continue.");
    try
    {
        System.in.read();
    }  
    catch(Exception e)
    {}  
    System.out.print("\033[H\033[2J");  
    System.out.flush(); 
    String ul = "";// No use of this - String []ul = new String[userSubmittedWord.length()];
    for(int i=0;i<len;i++)
    ul+="_";            //Placing as much underscores as the length of the userSubmittedWord is
    System.out.println("Please enter a singular letter as a guess, or attempt to guess the whole word.");
    System.out.println("Progress:::: "+ul);
    for(int i=0;i<userSubmittedWord.length();i++) {
        ul = ul.substring(0,i)+input.next()+ul.substring(i,len-1);  //Taking out 1 underscore per loop from the end.
        System.out.println("Progress::::"+ul);
        if(ul.charAt(i)==userSubmittedWord.charAt(i)) {
            remainingAttempts++;
            hangmanImage();
        }
        if(ul.equals(userSubmittedWord)&&i==0)
        {
            System.out.println("Congratulations! You have won. The word was: " + userSubmittedWord);
            break;
        }
    }
}

Upvotes: 0

Related Questions