Java代写:CS1017 HangMan

代写一款猜单词的HangMan游戏,在指定次数内猜对单词。

Requirement

In this assignment, you may use your knowledge of the following concepts:

  1. Loops (do-while) and condition flows (if-else if-else)
  2. Testing for String and number equality
  3. StringBuilder
  4. Reading from console (using Scanner to read from System.in)
  5. Reading from files (using BufferedReader)
  6. Writing to console (using System.out (a PrintStream))
  7. Writing out to files (using BufferedWriter)
  8. Try-catch-finally
  9. Calling methods and sending arguments, methods with return types
  10. Random number generation

Instructions

Create a class called Hw3FirstLastName.java
In this assignment you will be play the Hangman game. You will read words from a given file. Let us assume this file is called word.txt and contains 3 lower case words one on each line.
This class will have four methods.
The main method should be defined as follows:

1
public static void main(String[] args) {}

  • In main(), you will call the following three methods.
1
public static String[] readWordsFromFile(String fileName) throws IOException { }
  • this method will open a file called words.txt sent to it as an argument or input to its parameter
  • read the contents of the file and store it in a String array called wordArray of length 3
  • each word read is an element of the String array
  • this method should catch the FileNotFoundException and IOException
  • make sure to use try-catch-finally
  • this method returns the String[] created
1
public static String playHangman(String[] words) {}
  • write the logic for the hangman game here
  • the user should be able to play the game multiple times until he or she enters 0, which will quit the game
  • you can do this by using a do-while loop, inside which you will write code for the game
  • the loop should only stop when a boolean variable
  • before start of game
    • i. ask for user name and save it in a variable name in each game
  • randomly choose one of the words in the words array, which is a parameter of this method (that means it receives the array as an argument from main())
    • i. Random r = new Random();
    • ii. int rNum = r.nextInt(3);
    • iii. rNum can be used as the index of the word to select from words array
    • iv. store the selected word in String wordSelected
  • then print out the empty chars separated by a space to the console
  • for example, if the words has 5 chars, print
  • _ _ _ _ _
  • Create a char array called interimGuess to keep track of correctly guessed chars in the String wordSelected
  • Then ask the user to enter an input, get it using Scanner’s methods
  • *****“Enter 1 if you want to guess the word or enter 2 to guess one char or 0 to quit : “
  • Store what the user entered in an int reply
  • If reply == 0
    • i. Set play to false
    • ii. This will exit the current game (do-while loop( and not create another game
  • else if reply ==1
    • i. ask the user to enter their word guess
    • ii. store in a String
    • iii. check to see if it equal to String wordSelected
    • iv. if yes,
      • print “Congrats! you guessed right”
      • compute float stat = number of chars guessed so far saved in an int charGuessed/ length of the wordSelected
      • create a StringBuilder object that appends name + tab + stat + newline
    • if not equal
      • print “no that is not right”
  • else if reply ==2
    • i. ask user to enter a lower case char using Scanner’s method
    • ii. store it as a String
    • iii. check length of String
    • iv. if length >1 print “ you entered too many chars”
    • v. else if length == 1
      • extract the char at index 0 of string and save it in a char called c
      • increment int charGuessed++
    • vi. then update interimGuess by using a for loop
    • vii. if the char c matches a letter in wordSelected, also update the letter in interimGuess (set it equal to c)
    • viii. then print the new partially guessed word (stored in interimGuess) using another for loop
    • ix. if the user entered letter s, and it is found in the word, output will look like this
    • x. _ _ s _ _
    • xi. Then loop back to ***** above
  • else
    • i. print a message that the input is not valid
1
public static void writeStatsToFile(String playerStats) throws IOException {}
  • this method will write the content of playerStats String to a file called gamestats.txt
  • this method should catch the IOException
  • make sure to use try-catch-finally
  • this method has void return type

Submission

Submit a zip file of your project folder Hw3FirstLastname.zip and send it directly from the Canvas assignments area (NOT as an email attachment to me). Happy gaming!

Sample output

run:

Entry from file: polymorphism
Entry from file: encapsulation
Entry from file: hierarchy

Let's play hangman. I will give you empty letters in a word. You will guess what letter goes in the word one at a time.
Please enter your name : 
dana
thanks dana

Starting New Game
_ _ _ _ _ _ _ _ _ _ _ _ 
Enter 1 if you want to guess the word or enter 2 to guess one char or 0 to quit : 
1
Please enter your guess: 
java
no that is not right
Enter 1 if you want to guess the word or enter 2 to guess one char or 0 to quit : 
7
that choice is not valid
Enter 1 if you want to guess the word or enter 2 to guess one char or 0 to quit : 
fgfg
Input mismatch: please enter 1 or 2
Enter 1 if you want to guess the word or enter 2 to guess one char or 0 to quit : 
2
please enter a letter
e
you entered e
_ _ _ _ _ _ _ _ _ _ _ _ 
Enter 1 if you want to guess the word or enter 2 to guess one char or 0 to quit : 
2
please enter a letter
p
you entered p
p _ _ _ _ _ _ p _ _ _ _ 
Enter 1 if you want to guess the word or enter 2 to guess one char or 0 to quit : 
2
please enter a letter
m
you entered m
p _ _ _ m _ _ p _ _ _ m 
Enter 1 if you want to guess the word or enter 2 to guess one char or 0 to quit : 
1
Please enter your guess: 
polymorphism
Congrats! you guessed right
charGuessed = 3
word length = 12
stat = 4.0

Starting New Game
_ _ _ _ _ _ _ _ _ 
Enter 1 if you want to guess the word or enter 2 to guess one char or 0 to quit : 
2
please enter a letter
e
you entered e
_ _ e _ _ _ _ _ _ 
Enter 1 if you want to guess the word or enter 2 to guess one char or 0 to quit : 
1
Please enter your guess: 
hierarchy
Congrats! you guessed right
charGuessed = 1
word length = 9
stat = 9.0

Starting New Game
_ _ _ _ _ _ _ _ _ _ _ _ 
Enter 1 if you want to guess the word or enter 2 to guess one char or 0 to quit : 
0

Game ends

The gamestats.txt file contains one line for each game played
Vandana    4.0
Vandana    9.0