kap
kap

Reputation: 1

Hangman Java Help - How do I replace guessed letters?

I Need help with a simple hangman game. I have the game running, I just need to know where and how to add a guessed letter into the correct lines of an array. Also is there any code I can omit to make my project more readable?

public static void main(String[] args)
{

    //create scanner to read input
    Scanner keyboard = new Scanner(System.in);

    //create string name for player
    String name; 

    //get player's name
    System.out.println("What is you name?");
    name = keyboard.next();

    //Print welcome message
    System.out.println("Hello " + name + "! Welcome to a game of Hangman!");
    System.out.println("Guess letters until you correctly guess the word ");
    System.out.println("or you fully assemble a complete hangman.");
    System.out.println("You have a 3 letter word!");



    Random rand = new Random();
    String [] Word = {"hat"};

    String secretWord = Word[rand.nextInt(Word.length)];// word from the char array and stores it in a variable 
    char [] array = secretWord.toCharArray();   //converts the word from the array into a char array
    //char [] array = new char [secretWord.length()];
    //counter
    int i = 0;
    char input = 0;

    //counts wins
    int winCount = 0;

    //counts losses
    int tries = 20;
    int wrongCounter = 0;
    ArrayList<Character> list = new ArrayList<Character>();


    while(i<tries)
    {

     //allows player to guess a letter
        System.out.println("Guess a letter : ");        
        input = keyboard.next().charAt(0);
        list.add(input);

      //displays guessed letter
        System.out.println("Guessed Letters are : "+ input);

        for(int j = 0; j<notSame(array).length;j++)
        {

            if(findCorrect(array,input, list)>0)
            {
                j++;

                System.out.println("You got it right");


                winCount++;
                break;
            }
         else if(!(input ==notSame(array)[j]))
            {

               System.out.println("You got it wrong");

                wrongCounter++;
                break;


            }

            }

          //tests to see if the player won
            if(winCount == notSame(array).length)
            {
                System.out.println("You have Won!");
                System.out.println(secretWord + " is the correct word!");
                break;
            }
            else if(hangMan(wrongCounter)){
                break;
            }
        }

        System.out.println();
    }

    public static boolean hangMan(int wrongCounter) 
    {

        boolean isOver= false;
        System.out.println("------------|");

        switch(wrongCounter)
        {
        case 1:
            System.out.println("|           O");
            break;
        case 2 :
            System.out.println("|           O");
            System.out.println("|           |");
            break;
        case 3 :
            System.out.println("|           O");
            System.out.println("|          /|");
            break;
        case 4:
            System.out.println("|           O");
            System.out.println("|          /|\\");
            System.out.println();
            break;
        case 5:
            System.out.println("|           O");
            System.out.println("|          /|\\");
            System.out.println("|          /");
            break;
        case 6:
            System.out.println("|           O");
            System.out.println("|          /|\\");
            System.out.println("|          / \\");
            System.out.println("Sorry, you lost");
            System.out.println("hat is the correct word");
            isOver = true;
            break;
        }
        return isOver;

    }

    //removes all same letters from the array
    private static char[] notSame(char[] a) {
        HashSet<Integer> keys = new HashSet<Integer>();
        char[] result = new char[a.length];
        int j = 0;
        for (int i = 0 ; i < a.length; i++) 
        {

            if (keys.add((int) a[i])) 
            {
                result[j] = a[i];
                j++;
            }
        }
        return Arrays.copyOf(result, j);
    }
    //tests to see if the users input it equal to any of the letters in the array
    public static int findCorrect(char [] a,char Input,ArrayList list)
    {
        int count = 0;
        for(int i = 0;i<notSame(a).length;i++)
        {
            if(notSame(a)[i]==Input)
                count++;
        }
        return count;

    }
}

Upvotes: 0

Views: 2414

Answers (1)

Kyle
Kyle

Reputation: 156

The output for the guessed letters should be System.out.println("Guessed Letters are : "+ list.toString());

That way it shows all of the letters guessed and not just the last one.

To display the word as blanked out lines until each letter is guessed define a new char array

    String [] Word = {"hat"};

    String secretWord = Word[rand.nextInt(Word.length)];/

    // modified
    char [] displayArray = new char[secretWord.length()];
    for(int i=0; i < Word[0].length();i++){
      displayArray[i] += '_';
    }

Then update it when it is correct (around line 65 or so)

    if(findCorrect(array,input, list)>0)
    {
         j++;            
         //modified
         int correctLocation = secretWord.indexOf(""+input);
         displayArray[correctLocation] = array[correctLocation];
         System.out.println("You got it right");

         winCount++;
         break;
    }

Then at the end of that for loop display it

    //modified
    System.out.println("Word is : " + new String(displayArray));

    //tests to see if the player won
    if(winCount == notSame(array).length)
    {
        System.out.println("You have Won!");
        System.out.println(secretWord + " is the correct word!");
        break;
    }
    else if(hangMan(wrongCounter)){
        break;
    }

Upvotes: 1

Related Questions