Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following is for a computer science program using Dr.Java. Prompt: Creating

ID: 3679214 • Letter: T

Question

The following is for a computer science program using Dr.Java.

Prompt: Creating a simple game of hangman. Write a program to ask a user to enter a word, storing each character of the string into an array. Add enough line breaks to get the string off the screen. Set up a second character array the same length as the first and fill with '_' characters. Use this second array to present to another user, allowing them to guess letters. As they guess a letter that is in the original word, fill it in in the second array and re-display. If they guess a letter that is not in the word, take away a "try". Give them 6 tries to guess the whole word or they lose.

A big thing with this that I'm having difficulty with is replacing the '_' characters with correct guesses. For example, if the mystery word is "brick", it should initially show up like this _ _ _ _ _. If the user guesses 'c', the next output should be _ _ _ c _

Explanation / Answer

import java.util.Scanner;
import java.util.regex.Pattern;

public class Test
{

public static void main(String[] args)
{

    char[] Alpha = { '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' };

    int[] guessess = new int[26];

    boolean guessed = false;

    for(int i=0;i<26;i++)
    {
        guessess[i] = 0;
    }

    String word_Guess;
    char letter;
    String Choice;
    String Array_String;

    do {
        System.out.println("Please enter a letter");
        Scanner wordInput = new Scanner(System.in);
        word_Guess = wordInput.next();
        word_Guess = word_Guess.toUpperCase();
    } while (Pattern.matches("[A-Z]+", word_Guess) == false);  

    char[] wordArray = word_Guess.toCharArray();            
    char[] guess_word = new char[word_Guess.length()];
    for (int h = 0; h < guess_word.length; h++)
        guess_word[h] = '_';

    for (int i = 0; i < 20; i++)
    {
        System.out.println();
    }

    for (int j = 0; j < 10; j++)
      {

        do {

            guessed = false;

            System.out.print("Word to guess: ");
            System.out.println(guess_word);
            System.out
                    .println("Please choose a letter or solve the word.    "
                            + "Attempts left: " + (10 - j));
            System.out.println(Alpha);
            Scanner userInput = new Scanner(System.in);
            Choice = userInput.next();
            Choice = Choice.toUpperCase();  
            letter = Choice.charAt(0);
            letter = Character.toUpperCase(letter);

            if (Character.isLetter(letter) == false)
                System.out.println("Invalid letter. Please try again.");
            else if (Choice.length() > 1
                    && Choice.length() < word_Guess.length())
                System.out.println(("Choose only one letter. Try again."));
            for (int k = 0; k < Alpha.length; k++)
            {
                if(guessess[k] == 1)
                {
                    guessed = true;
                    System.out.println("You've already tried this letter. Please try again.");
                }
                if (letter == Alpha[k])
                {
                    guessess[k] = 1;
                }
            }
        } while (Choice.length() != 1
                && Choice.length() != word_Guess.length()
                || Character.isLetter(letter) == false
                || guessed == true);

        if (Choice.length() == 1)
        {
       
         

            for (int m = 0; m < word_Guess.length(); m++)
            {
                if (letter == wordArray[m])
                {
                    guess_word[m] = wordArray[m];
                }
            }
            Array_String = new String(guess_word);
            if (Array_String.equals(word_Guess))
            {

                System.out.println(guess_word);
                System.out.print("Congratulations.");
                System.out.print("You guessed the word: ");
                System.out.print(word_Guess);
                System.out.println(" in " + (j + 1) + " guesses.");
                break;

            }

        } else if (Choice.length() == word_Guess.length())
        {
            if (Choice.equals(word_Guess)) {
                System.out.println(guess_word);
                System.out.print("Congratulations.");
                System.out.print("You guessed the word: ");
                System.out.print(word_Guess);
                if (j == 0)
                    System.out.println(" in " + (j + 1) + " guess.");
                else
                    System.out.println(" in " + (j + 1) + " guesses.");
                break;
            } else
                System.out.println("Wrong guess");
        }

        if (j >= 9)
            System.out
                    .println("Sorry!!");
                 
    }

}

}