Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

i have a dictionary file which consist inappropariate words and its named hangman.txt so i cant paste but here is the assignment instruction Let's play

i have a dictionary file which consist inappropariate words and its named hangman.txt so i cant paste but

here is the assignment instruction

Let's play Hangman!

Word:

_ _ _

Misses:

Guess a

letter: g

Word:

_ _ G

Misses:

Guess a

letter: T

Word:

_ _ G

Misses:

T

Guess a

letter: g

Already

guessed!

Guess a letter: t

Already

guessed!

Guess a letter: f

Word:

_ _ G

Misses:

T,F

Guess a

letter: d

Word:

D _ G

Misses:

T,F

Guess a

letter: m

Word: D _ G

Misses: T,F,M Guess a letter: O

The answer was: DOG You WIN!

Do you want to play again? Yess!!

Word: _ _ _ _

Misses:

Guess a letter: u

Word: _ _ _ _

Misses: U

Guess a letter: I

Word: _ _ _ _

Misses: U,I

Guess a letter: o

Word: _ _ _ _

Misses: U,I,O

Guess a letter: a

Word: _ A _ _

Misses: U,I,O

Guess a letter: R

Word: _ A _ _

Misses: U,I,O,R

Guess a letter: b

Word: _ A _ _ Misses: U,I,O,R,B Guess a letter: p The answer was: HAND You LOSE!

Do you want to play again? neIN

You played 2 game(s). Goodbye!

Program Description:

This assignment focuses on while loops, Boolean methods, and text processing. The program allows the user to play Hangman. Turn in a file named Hangman.java. Make sure you have all the required files from the course web site before you start.

Each game of Hangman consists of the program choosing a word and initially displaying each letter of the word with an underscore character (_) and a space. The user has to guess the word by guessing one letter at a time.

If the user guesses a letter that is not in the word, the program adds that letter (in uppercase) to a list of wrong guesses. If the user guesses 6 wrong letters before guessing all the letters of the word, the user loses.

If the user guesses a letter that is in the word, the program replaces the corresponding underscore(s) with that letter (in uppercase). If the user guesses all the letters of the word before making 6 wrong guesses, the user wins.

If the user guesses a letter that they have already guessed before, the program notifies them and asks them to enter another letter (see example logs of execution for exact wording).

After each game, the program asks the user if they want to play again. The program should start a new game if the user's response begins with the letter y. That is, answers such as "y", "Y", "YES", "yes", "Yes", or "yeehaw" all indicate that the user wants to play again. Otherwise assume that the user does not want to play again. For example, responses such as "n", "N", "no", "okay", and "hello" would mean that the user doesn't want to play again.

Read files Hangman.txt for selecting random words. Words must be on their own line. Only words that consist entirely of letters (no spaces or punctuation) will be used. You can use the following statement to retrieve a random word and store it in the String variable.

1 of 2

Implementation Guidelines:

We suggest that you develop this program in stages. One useful stage is to write a program that plays just one game. You can also start by only considering correct letters. If the user types a wrong letter, just ignore it for now. You can even put in code to print out the correct answer for debugging purposes, though you should delete such code before you turn in the assignment. The following might be logs of execution for two in-progress stages of the program, though you do not have to develop the program this way and should not turn in a program that prints the answer:

Log of execution from first hypothetical in-progress version

Log of execution from second hypothetical in-progress version

(only consider letters from the correct answer)

(user may guess wrong letters)

Let's play Hangman!

Let's play Hangman!

ANSWER: CAT

ANSWER: CAT

Word:

_ _ _

Word:

_ _ _

Guess a letter: T

Misses:

Guess a

letter: q

Word:

_ _ T

Guess a letter: a

Word:

_ _ _

Misses:

Q

Word:

_ A T

Guess a

letter: B

Guess a letter: C

The answer was: CAT

Word:

_ _ _

You WIN!

Misses:

Q,B

Guess a

letter:

This program involves a bit of text processing. You should store the guessed letters in a String. Whether you store all the guessed letters in one String or you separate the correct letters from the wrong letters is up to you. For example, if the correct answer is dog and the user has guesse d g, t, f, d, and m so far, you can store gtfdm as a String or separate the correct letters (gd) from the wro ng letters (tfm). Youll find the methods indexOf and charAt from the String class to be of general use.

Assume valid user input. When prompted for a guess, assume that the user will only type one letter. However, the user may enter letters in either uppercase or lowercase and your program must be able to handle that. When the user is prompted to play again, assume the user will type a one-word answer. To deal with the yes/no user response, you may want to use String methods described in the slides.

IMPORTANT:

While testing your code, you can set the word the user has to guess to anything you want; however, when you submit your program, the word to be guessed must come from the Hangman.txt file.

This is a pair assignment you are allowed to make a pair. Only two persons are allowed to work together no more than two. If you need a partner contact your instructor.

Stylistic Guidelines:

Structure your solution using static methods that accept parameters and return values where appropriate. For full credit, you must have at least 3 non-trivial methods other than main in your program. Two of these must be the following:

a method to play a single game with the user

a method to construct the formatted word the user sees

You may define other methods if they are useful for structure or to eliminate redundancy. Unlike in past programs, it is okay to have some println statements in main, as long as your program has good structure and main is still a concise summary of the program. For example, you can place the loop that plays multiple games and the prompt to play again in main. As a reference, our solution has 4 methods other than main

For this assignment you are limited to the language features in Chapters 1-7 of the textbook. Use whitespace and indentation properly. Limit lines to 100 characters. Use meaningful identifier names and proper capitalization. Include a comment header with basic description information and a comment at the start of each method. Since this program has longer methods than past programs, also put brief comments inside the methods explaining relevant sections of your code.

2 of 2

my code is supposed to read and pick rundomly any word from the dictionary file

here is what i got with comments so far but it is not working

import java.util.Scanner;

import java.io.*;

//Define a class lhangman

public class lhangman

{

//Define a main method

public static void main(String[] args) throws IOException

{

//Define a scanner method

Scanner lkb = new Scanner(System.in);

//Declare variable

char lagain = 'n';

//Declare variable

String secret;

//Declare variable

StringBuffer dashes;

//Declare variable

final int lMAXPARTS = 6;

//Declare variable

int bodyparts;

//Declare variable

boolean done;

//Declare variable

String guess;

//Declare variable

String guesses;

//Declare variable

char letter;

//Open file

Scanner linfile = new Scanner(new FileReader("hangWords.txt"));

//Do while loop

do

{

// play a game

secret = linfile.next();

//Guess

guesses = "";

//Declare variable

done = false;

//Update value

bodyparts = lMAXPARTS;

// Make dashes

dashes = makeDashes(secret);

//Loop until done

while (! done)

{

//Display message

System.out.println("Here is your word: " + dashes);

//Display message

System.out.println("Guesses so far: " + guesses);

//Display message

System.out.print("enter a guess (letter or word): ");

//Move to next

guess = lkb.next();

// process guess

if (guess.length() > 1)

{

//If guess matches secret

if (guess.equals(secret))

//Display message

System.out.println("you win!");

//If guess not equals secret

else

//Display message

System.out.println("you lose");

//Set value

done=true;

}

//Process single letter guess

else

{

//Store letter

letter = guess.charAt(0);

//Update guess

guesses += letter;

//If index is less than 0

if (secret.indexOf(letter) < 0)

{

//Decrement

--bodyparts;

//Display message

System.out.print("bad guess - ");

}

//If index is not less than 0

else

{

/* Put it in dashes where it belongs */

matchLetter(secret, dashes, letter);

}

//Display message

System.out.println(bodyparts + " bodyparts are left");

//If count is 0

if (bodyparts == 0)

{

//Display message

System.out.println("you lose");

//Set value

done = true;

}

//If secret equals completely

if (secret.equals(dashes.toString()))

{

//Display message

System.out.println("you win!");

//Set value

done = true;

}

}

}

//To next input

if (linfile.hasNext())

{

//Display message

System.out.print("play lagain (y/n)?: ");

//Store value

lagain = lkb.next().charAt(0);

}

//Otherwise

else

//Display message

System.out.println("thanks for playing (no more words)");

}

//While loop

while (linfile.hasNext() && (lagain == 'Y' || lagain == 'y'));

}

//Define a method "matchLetter()"

public static void matchLetter(String secret, StringBuffer dashes, char letter)

{

//Loop until length

for (int index = 0; index < secret.length(); index++)

//If secret equals letter

if (secret.charAt(index) == letter)

//Set character

dashes.setCharAt(index, letter);

//Display message

System.out.print("good guess - ");

}

//Define a method "makeDashes()"

public static StringBuffer makeDashes(String s)

{

//Define buffer

StringBuffer dashes = new StringBuffer(s.length());

//Loop until length

for (int count=0; count < s.length(); count++)

//Append dashes

dashes.append('-');

//Return dashes

return dashes;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Find the value of each determinant. 6 4 -1 3

Answered: 1 week ago

Question

LOQ 16-8: Does psychotherapy work? How can we know?

Answered: 1 week ago

Question

Language in Context?

Answered: 1 week ago