Question

Original question IN JAVA Show the user a number of blank spaces equal to the number...

Original question 

IN JAVA

Show the user a number of blank spaces equal to the number of letters in the word For cat, you would show _ _ _.

Prompt a user to guess a letter until they have run out of guesses or guessed the full word By default, the user should be able to make 7 failed guesses before they lose the game. If the user guesses a letter that is in the target word, it does not count against their remaining guesses.

After each guess, print to the console how many letters the user has correct so far and how many more missed guesses they can make. C _ _ . You have 6 misses remaining

At the end of the game (either when the user has guessed all letters or run out of guesses), you should print to the console the full word and a message that either congratulates the user or encourages them to try again. Your program needs to tell the player how they're doing by outputting how many guesses they have left and which letters of the word they've revealed.

  • In this repository, there is a list of words. Your program must read from the list of words and select one at random for the user to guess

  • Write a log of a game played to a file. That file should tell us what the word is, how many guesses the player has left and how much of the word they still have.

  • Write a log file with every game played in a session

  • Allow the user to select how many guesses they receive when playing

  • Handle words with punctuation

    • Allow the user to choose a number of games to win and play until they win or lose that many

      brainstorm
      marble
      message
      appearance
      mother
      horizon
      veteran
      revolutionary
      burst
      allowance
      fault
      theory
      incident
      overwhelm
      admire
      fruit
      negligence
      hilarious
      frighten
      specimen
      custody
      magnitude
      snack
      kneel
      haircut
      issue
      endure
      painter
      draft
      reflection
      decide
      favour
      pasture
      charity
      baseball
      husband
      assembly
      flatware
      define
      relevance
      deviation
      umbrella
      colon
      outlook
      memorial
      reliable
      tolerant
      sticky
      arrangement
      prosecute
      linger
      hallway
      moral
      tribe
      development
      engineer
      railroad
      mutual
      angle
      bridge
      reckless
      haunt
      urgency
      import
      communication
      exclude
      wreck
      disability
      harass
      thinker
      layer
      shaft
      teacher
      meadow
      allow
      passion
      heavy
      construct
      flavor
      account
      overlook
      palace
      shout
      goalkeeper
      narrow
      jealous
      treatment
      coincide
      photography
      thoughtful
      evaluate
      calorie
      mosaic
      contract
      emergency
      youth
      innovation
      creed
      snarl
      refrigerator
I posted a question and this was the code corresponding however I am unfamiliar with the asterisk parts of the code how can I REWRITE the code without that feature? What would be the alternative?

import java.io.*;
import java.util.Scanner;

public class GuessClass
{
    // READ WORDS from the file
    private static String[] words;

    static {
        try {
            words = readWords();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String word = words[(int) (Math.random() * words.length)];
    private static String asterisk = new String(new char[word.length()]).replace("\0", "*");
    private static int guesses = 0;
    // add a new string that contains already guessed characters
    private static String alreadyGuessed="";


    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Welcome to the Hnagman Game! \nYou will have 8 tries to guess a letter in the phrase.\nGood luck!");

        while (guesses < 8 & asterisk.contains("*"))
        {
            System.out.println("Guess a letter: ");
            System.out.println(asterisk);
            String guess = sc.next();

            showWord(guess);
            // add the guessed letter to the string
            alreadyGuessed += guess;
        }
        sc.close();
    }

    private static String[] readWords() throws IOException {
        String[] strings=new String[100];
        File file = new File("src//words.txt");

        BufferedReader br = new BufferedReader(new FileReader(file));

        String st;
        int i=0;
        while ((st = br.readLine()) != null)
            strings[i++]=st;
        return strings;
    }

    public static void showWord(String guess)
    {
        String newasterisk = "";
        for (int i = 0; i < word.length(); i++)
        {
            if (word.charAt(i) == guess.charAt(0))
            {
                newasterisk += guess.charAt(0);
            }
            else if (asterisk.charAt(i) != '*')
            {
                newasterisk += word.charAt(i);
            }
            else
            {
                newasterisk += "*";
            }
        }

        if (asterisk.equals(newasterisk))
        {
            if(alreadyGuessed.contains(guess))
            {
                // already it is guessed. so, don't increase count
                System.out.println("You have already guessed it..!");
            }
            else
            {
                guesses++;
            }
            numGuesses();
        } else
        {
            asterisk = newasterisk;
        }
        if (asterisk.equals(word))
        {
            System.out.println("Yay! You guessed the whole phrase! :) " + "The phrase was: " + word);
            System.out.println("\nYou took "+guesses+" tries to guess "+word);
        }
    }

    public static void numGuesses()
    {
        if (guesses == 1)
        {
            System.out.println("That letter is not in the phrase, try again. \nYou have 7 chances left.");
        }

        if (guesses == 2)
        {
            System.out.println("That letter is not in the phrase, try again. :)\nYou have 6 chances left.");
        }

        if (guesses == 3)
        {
            System.out.println("That letter is not in the phrase, try again. :)\nYou have 5 chances left.");
        }

        if (guesses == 4)
        {
            System.out.println("That letter is not in the phrase, try again. :)\nYou have 4 chances left.");
        }

        if (guesses == 5)
        {
            System.out.println("That letter is not in the phrase, try again. (:\nYou have 3 chances left.");
        }

        if (guesses == 6)
        {
            System.out.println("That letter is not in the phrase, try again. :)\nYou have 2 chances left.");
        }

        if (guesses == 7)
        {
            System.out.println("That letter is not in the phrase, try again :)\nYou have one last chance.");
        }


        if (guesses == 8)
        {
            System.out.println("You ran out of guesses! :( Game over.");

            System.out.println("The word was: " + word);
        }
    }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

please do upvote.If you have any problem do comment and i shall be happy to help you.Thanks for using HomeworkLib.

--------------------------------------------

There is no alternative way to achieve required functionality without the asterisk feature.

I have added comments to explain the asterisk parts of code

--------------------------------------

import java.io.*;
import java.util.Scanner;

public class GuessClass
{
// READ WORDS from the file
private static String[] words;

static {
try {
words = readWords();
} catch (IOException e) {
e.printStackTrace();
}
}

private static String word = words[(int) (Math.random() * words.length)];
  
//make a string called asterisk and it will contain all letters as *
//length of asterisk= length of word
//so e.g if word=cat then asterisk=***
// private static String asterisk = new String(new char[word.length()]).replace("\0", "*");
   private static String asterisk = word.replaceAll(".", "*");

private static int guesses = 0;
// add a new string that contains already guessed characters
private static String alreadyGuessed="";


public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Welcome to the Hnagman Game! \nYou will have 8 tries to guess a letter in the phrase.\nGood luck!");

while (guesses < 8 & asterisk.contains("*"))
{
System.out.println("Guess a letter: ");
System.out.println(asterisk);
String guess = sc.next();

showWord(guess);
// add all the guessed letters to the string alreadyGuessed
alreadyGuessed += guess;
}
sc.close();
}

private static String[] readWords() throws IOException {
String[] strings=new String[100];
File file = new File("src//words.txt");

BufferedReader br = new BufferedReader(new FileReader(file));

String st;
int i=0;
while ((st = br.readLine()) != null)
strings[i++]=st;
return strings;
}

public static void showWord(String guess)
{
String newasterisk = "";
for (int i = 0; i < word.length(); i++)
{
   //if letter of guess matches with letter at position i in word
   //attach the guess at the end of newasterisk
if (word.charAt(i) == guess.charAt(0))
{
newasterisk += guess.charAt(0);
}
  
   //if letter of guess does not matches with letter at position i in word
//and the asterisk is not * at position i ,instead it is guessed
//then attach that letter at end of newasterisk
else if (asterisk.charAt(i) != '*')
{
newasterisk += word.charAt(i);
}
  
   //if letter of guess does not matches with letter at position i in word
//and the asterisk is * at position i
//then attach * at the end of newasterisk
else
{
newasterisk += "*";
}
}

//if asterisk is same as new asterisk
if (asterisk.equals(newasterisk))
{
if(alreadyGuessed.contains(guess))
{
// already it is guessed. so, don't increase count
System.out.println("You have already guessed it..!");
}
else
{
guesses++;
}
numGuesses();//show leftover guesses
} else
{
//if asterisk is not same as newasterisk
   //make asterisk = newasterisk
  
asterisk = newasterisk;
}
  
//if asterisk is same as word
//we have guessed the word
if (asterisk.equals(word))
{
System.out.println("Yay! You guessed the whole phrase! :) " + "The phrase was: " + word);
System.out.println("\nYou took "+guesses+" tries to guess "+word);
}
}

public static void numGuesses()
{
if (guesses == 1)
{
System.out.println("That letter is not in the phrase, try again. \nYou have 7 chances left.");
}

if (guesses == 2)
{
System.out.println("That letter is not in the phrase, try again. :)\nYou have 6 chances left.");
}

if (guesses == 3)
{
System.out.println("That letter is not in the phrase, try again. :)\nYou have 5 chances left.");
}

if (guesses == 4)
{
System.out.println("That letter is not in the phrase, try again. :)\nYou have 4 chances left.");
}

if (guesses == 5)
{
System.out.println("That letter is not in the phrase, try again. (:\nYou have 3 chances left.");
}

if (guesses == 6)
{
System.out.println("That letter is not in the phrase, try again. :)\nYou have 2 chances left.");
}

if (guesses == 7)
{
System.out.println("That letter is not in the phrase, try again :)\nYou have one last chance.");
}


if (guesses == 8)
{
System.out.println("You ran out of guesses! :( Game over.");

System.out.println("The word was: " + word);
}
}
}

Add a comment
Know the answer?
Add Answer to:
Original question IN JAVA Show the user a number of blank spaces equal to the number...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA Hangman Your game should have a list of at least ten phrases of your choosing.The...

    JAVA Hangman Your game should have a list of at least ten phrases of your choosing.The game chooses a random phrase from the list. This is the phrase the player tries to guess. The phrase is hidden-- all letters are replaced with asterisks. Spaces and punctuation are left unhidden. So if the phrase is "Joe Programmer", the initial partially hidden phrase is: *** ********** The user guesses a letter. All occurrences of the letter in the phrase are replaced in...

  • Please help with this Intro to programming in C assignment! Intro to Programming in C-Large Program...

    Please help with this Intro to programming in C assignment! Intro to Programming in C-Large Program 3 - Hangman Game Assignment purpose: User defined functions, character arrays, c style string member functions Write an interactive program that will allow a user to play the game of Hangman. You will need to: e You will use four character arrays: o one for the word to be guessed (solution) o one for the word in progress (starword) o one for all of...

  • Overview In this exercise you are going to recreate the classic game of hangman. Your program...

    Overview In this exercise you are going to recreate the classic game of hangman. Your program will randomly pick from a pool of words for the user who will guess letters in order to figure out the word. The user will have a limited number of wrong guesses to complete the puzzle or lose the round. Though if the user answers before running out of wrong answers, they win. Requirements Rules The program will use 10 to 15 words as...

  • Do in Python please provide screenshots aswell. Here are the requirements for your game: 1. The...

    Do in Python please provide screenshots aswell. Here are the requirements for your game: 1. The computer must select a word at random from the list of avail- able words that was provided in words.txt. The functions for loading the word list and selecting a random word have already been provided for you in ps3 hangman.py. 2. The game must be interactive; the flow of the game should go as follows: • At the start of the game, let the...

  • Create a new program, WordGuessingGame. Using a while loop, create a game for the user. The...

    Create a new program, WordGuessingGame. Using a while loop, create a game for the user. The game requires the user to guess a secret word. The game does not end until the user guesses the word. After they win the game, they are prompted to choose to play again. The secret word that user must guess is "valentine". It is a case-sensitive analysis With each guess... If the guess does not have an equal number of characters, tell the user...

  • This is for C programming: You will be given files in the following format: n word1...

    This is for C programming: You will be given files in the following format: n word1 word2 word3 word4 The first line of the file will be a single integer value n. The integer n will denote the number of words contained within the file. Use this number to initialize an array. Each word will then appear on the next n lines. You can assume that no word is longer than 30 characters. The game will use these words as...

  • For this lab you will write a Java program that plays a simple Guess The Word...

    For this lab you will write a Java program that plays a simple Guess The Word game. The program will prompt the user to enter the name of a file containing a list of words. These words mustbe stored in an ArrayList, and the program will not know how many words are in the file before it starts putting them in the list. When all of the words have been read from the file, the program randomly chooses one word...

  • Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args)...

    Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args) { displayWelcomeMessage(); // create the Scanner object Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // generate the random number and invite user to guess it int number = getRandomNumber(); displayPleaseGuessMessage(); // continue until the user guesses the number int guessNumber = 0; int counter = 1; while (guessNumber != number) { // get a valid int from user guessNumber...

  • I have to use java programs using netbeans. this course is introduction to java programming so...

    I have to use java programs using netbeans. this course is introduction to java programming so i have to write it in a simple way using till chapter 6 (arrays) you can use (loops , methods , arrays) You will implement a menu-based system for Hangman Game. Hangman is a popular game that allows one player to choose a word and another player to guess it one letter at a time. Implement your system to perform the following tasks: Design...

  • Simple JavaScript and HTML: You'll create a simple word guessing game where the user gets infinite...

    Simple JavaScript and HTML: You'll create a simple word guessing game where the user gets infinite tries to guess the word (like Hangman without the hangman, or like Wheel of Fortune without the wheel and fortune). Create two global arrays: one to hold the letters of the word (e.g. 'F', 'O', 'X'), and one to hold the current guessed letters (e.g. it would start with '_', '_', '_' and end with 'F', 'O', 'X'). Write a function called guessLetter that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT