In: Computer Science
For this assignment, you will create a command-line version of the game Hangman. You should work in a group of two on the project and not view Hangman code of other students or found on-line. Submit this project-- your .java file-- here on Canvas.
For this assignment you will research on StringBuilder Class (Use this link) and use it in your code.
Functional requirements (rubric)
● Your game should have a list of at least ten phrases of your choosing (appropriate to all audiences please!) The game chooses a random phrase from the list. This is the phrase the player tries to guess. (10)
● 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: *** ********** (15)
● The user guesses a letter. All occurrences of the letter in the phrase are replaced in the partially hidden phrase. For our example, if the user guessed "o", the new partially hidden phrase would change to: * o * ** o******* 15
● Allow the phrases and guesses to include lowercase and uppercase letters and digits. If a guess is not a letter or digit (e.g., ‘%’) the user should be notified but not penalized a miss. 10
● If the guessed letter does not occur in the phrase, the user is notified of the miss and how many misses/chances are left. 10
● The user should NOT be penalized for guessing a previously correct or incorrect guess, but should be notified. 10
● If the user misses 8 times, they lose and the game is over. If all letters in the phrase are guessed, the user wins! 10
● Provide a reasonable user interface such that the players is given instructions on how to play and it is clear how to proceed at each step. Test this out by performing a usability test with at least two people! 10
Style Requirements
● Style your code using the Google Java Style Guidelines (Links to an external site.)Links to an external site.. For this first project, be sure and follow the formatting (Links to an external site.)Links to an external site. and naming (Links to an external site.)Links to an external site. guides.
Coding Specifications
● Define the ten phrases as an array of Strings.
○ String phraseList[] = {"cats and dogs", "stephen curry"};
○ then phraseList[0] will give "cats and dogs" and phraseList[1]
will
give "stephen curry".
● Define the randomly chosen phrase as a String
● Define the hidden phrase as a StringBuilder, because you will be changing it as the game is played.
● You need not define other methods other than main.
Hangman.java
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class Hangman {
public static void main(String[] args) {
String phraselist[] = {"Cats and
dogs",
"Stephen curry",
"Hangman game",
"My answers",
"Your anwers",
"Microsoft Win",
"Expert",
"Random words",
"Eclipse",
"Anaconda"};
Random rand = new Random();
int i = rand.nextInt(10);
String phrase =
phraselist[i];
//System.out.println(phrase);
int misses = 0;
ArrayList<Character>
guessedChars = new ArrayList<Character>();
StringBuilder guess = new
StringBuilder();
//Create a string builder object,
append * for alphanumeric character
//Keep punctuations and blank
spaces same as original phrase
for(int j=0; j<phrase.length();
j++) {
if(Character.isLetterOrDigit(phrase.charAt(j)))
guess.append("*");
else
guess.append(phrase.charAt(j));
}
while(misses<8) {
System.out.println("Hidden phrase: " + guess.toString());
System.out.println("Guess a character: " );
Scanner scan =
new Scanner(System.in);
char ch =
scan.nextLine().charAt(0);
//If the guessed
character is an alphabet or a digit
if(Character.isLetterOrDigit(ch)) {
//If phrase contains the guessed character
if(phrase.indexOf(ch)>=0) {
for(int j=0;
j<phrase.length(); j++) {
//Character at index j in original phrase
Character
original = phrase.charAt(j);
original =
Character.toLowerCase(original);
//Unhide
the guessed characters
if(original == ch)
guess.setCharAt(j, ch);
}
}
//It's a miss
else {
System.out.println("It's a
miss.");
System.out.println("Attempts
left: " + (7-misses));
//If this character has not
already been guessed, penalize
if(!guessedChars.contains(ch))
misses++;
}
}
else
continue;
//Check if all
characters have been guessed or not
if(guess.indexOf("*")<0) {
System.out.println("Hiden phrase: " +
guess.toString());
System.out.println("You won!");
break;
}
}
if(misses==8)
System.out.println("You lost.");
}
}