rastrast
rastrast

Reputation: 327

Java Hangman won't loop correctly

I am very new to programming and java (as you can see) and am really stuck on a basic problem in programming hangman. My code is not the cleanest I know, but I really would like to some input on where I am going wrong logic wise. My trouble is getting the program to output the 'hangman display' correctly with each guess. I think there is an issue with the way I have triesWrong++ set up. I also can't get the repeated letters to work properly but just can't quite figure it out.

import java.util.*;

public class HangMan
{

public static void main(String[] args)
{
    char[] alphabet =
    {
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
        'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
    };
    String[] words =
    {
        "javascript", "declaration", "object", "class", "failing"
    };
    int randomIndex = (int) (Math.random() * words.length);
    String randomWord = words[randomIndex];
    char[] explodedWord = explode(randomWord);
    char[] censoredWord = new char[randomWord.length()];
    int triesWrong = 0;
    Scanner input;
    char letterGuess;
    boolean letterFound = false;

    for (int i = 0; i < randomWord.length(); i++)
    {
        censoredWord[i] = '_';
    }

    while (!gameWon(censoredWord, explodedWord))
    {
        hangMan(triesWrong, censoredWord, alphabet);
        System.out.println("Please guess a letter: ");
        input = new Scanner(System.in);
        letterGuess = input.next().trim().charAt(0);

        char[] repeatedLetters = new char[26];

        for (int i = 0; i < alphabet.length; i++)
        {
            if (letterGuess == alphabet[i])
            {
                if(letterGuess == repeatedLetters[i])
                {
                    System.out.println("You already guessed this letter, please enter another letter.");
                }
                else
                {
                    repeatedLetters[i] = alphabet[i];
                    alphabet[i] = '_';
                }
            }
        }
        for (int i = 0; i < explodedWord.length; i++)
        {
            if (letterGuess == explodedWord[i])
            {
                censoredWord[i] = letterGuess;
                letterFound = true;
            }
        }
        if (!letterFound)
        {
            letterFound = false;
            triesWrong++;
        }
    }
}

public static char[] explode(String bank) //getWord method for string bank
{
    //create character array called explode that has length of string
    char[] explodedWord = new char[bank.length()];

    for (int i = 0; i < bank.length(); i++)
    {
        explodedWord[i] = bank.charAt(i);
    }
    return explodedWord;
}

public static void hangMan(int n, char[] dashWord, char[] alph)
{
    if (n == 0)
    {
        System.out.println("\n\n---------");
        System.out.println("|\t|");
        System.out.println("|");
        System.out.println("|");
        System.out.println("|");
        System.out.println("|");
        System.out.println("|");
        System.out.println("|");
    } else if (n == 1)
    {
        System.out.println("\n\n---------");
        System.out.println("|\t|");
        System.out.println("|\t0");
        System.out.println("|");
        System.out.println("|");
        System.out.println("|");
        System.out.println("|");
        System.out.println("|");
    } else if (n == 2)
    {
        System.out.println("\n\n---------");
        System.out.println("|\t|");
        System.out.println("|\t0");
        System.out.println("|\t|");
        System.out.println("|");
        System.out.println("|");
        System.out.println("|");
        System.out.println("|");
    } else if (n == 3)
    {
        System.out.println("\n\n---------");
        System.out.println("|\t|");
        System.out.println("|\t0");
        System.out.println("|\t|\\");
        System.out.println("|");
        System.out.println("|");
        System.out.println("|");
        System.out.println("|");
    } else if (n == 4)
    {
        System.out.println("\n\n---------");
        System.out.println("|\t|");
        System.out.println("|\t0");
        System.out.println("|      /|\\");
        System.out.println("|");
        System.out.println("|");
        System.out.println("|");
        System.out.println("|");
    } else if (n == 5)
    {
        System.out.println("\n\n---------");
        System.out.println("|\t|");
        System.out.println("|\t0");
        System.out.println("|      /|\\");
        System.out.println("|\t|");
        System.out.println("|");
        System.out.println("|");
        System.out.println("|");
    } else if (n == 6)
    {
        System.out.println("\n\n---------");
        System.out.println("|\t|");
        System.out.println("|\t0");
        System.out.println("|      /|\\");
        System.out.println("|\t|");
        System.out.println("|      /");
        System.out.println("|");
        System.out.println("|");
    } else if (n == 7)
    {
        System.out.println("\n\n---------");
        System.out.println("|\t|");
        System.out.println("|\t0");
        System.out.println("|      /|\\");
        System.out.println("|\t|");
        System.out.println("|      / \\");
        System.out.println("|");
        System.out.println("|");
        System.out.println("GAME OVER");
        System.exit(0);
    }

    System.out.println("\n\n");

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

    System.out.println("\n\n");

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

    System.out.println("\n\n");
}

public static boolean gameWon(char[] dashWord, char[] bigWord)
{       
    if(dashWord.length != bigWord.length)
    {
        return false;
    }

    for(int i = 0; i < dashWord.length; i++)
    {
        if(dashWord[i] != bigWord[i])
        {
            return false;
        }
    }
    System.out.println("Congratulations, you win!");

    return true;
}

}

Upvotes: 1

Views: 117

Answers (1)

Ronald Klop
Ronald Klop

Reputation: 116

    if (!letterFound)
    {
        letterFound = false;

If !letterFound is true it means letterFound is already false. So no need to set it to false afterwards. That is a logic flaw. I advise to move letterFound = false; to the start of the while loop. So you start fresh every loop.

Upvotes: 1

Related Questions