Question

In: Computer Science

For this assignment, you will create a command-line version of the game ​Hangman. You should work...

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.

Solutions

Expert Solution

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.");
   }

}


Related Solutions

Hangman We're going to write a game of hangman. Don't worry, this assignment is not nearly...
Hangman We're going to write a game of hangman. Don't worry, this assignment is not nearly as difficult as it may appear. The way hangman works (for this assignment - we are doing a simplified game) is as follows: the computer will choose a word. (For this version, a word is selected from a static list encoded into the program -- so the words are pretty limited). Let's say the word is "cocoa". the computer shows the user how many...
Please create a Hangman game in Java language. For this game a random word will be...
Please create a Hangman game in Java language. For this game a random word will be selected from the following list (abstract, cemetery, nurse, pharmacy, climbing). You should keep a running tab of the user’s score and display it on screen; the user begins with 100 points possible if they correctly guess the word without any mistakes. For every incorrect letter which is guessed, 10 points should be taken away from what is left of their total possible points. For...
Can you create a player vs computer Hangman game on MATLAB using nested loops, for loops,...
Can you create a player vs computer Hangman game on MATLAB using nested loops, for loops, if loops, while loops, arrays and conditional execution, as well as creating an image of the game board. The list below must be considered in the code. - Selecting a word from a dictionary (a text file) - Reading a single letter from the user - Building a character array showing the letters matched so far - Keeping count of the number of guessed...
How would I create a Hangman game that chooses  a random word and the player needs to...
How would I create a Hangman game that chooses  a random word and the player needs to guess it, letter by letter using  JavaScript and jQuery to allow user interaction. The content and the style of the page must be updated based on user’s input.
**Need to use awk command in putty (should be a ONE LINE COMMAND) Write the command...
**Need to use awk command in putty (should be a ONE LINE COMMAND) Write the command that would find all lines that have an email address and place a label email = before the line in the file longfile output will multiple lines similar to this one : using a good awk command the output would be something like this email = From: "Linder, Jann/WDC" <[email protected]> email = To: Mr Arlington Hewes <[email protected]> email = > From: Mr Arlington Hewes...
Create a Hangman game using C++ by QT creator. please separate each code that will insert...
Create a Hangman game using C++ by QT creator. please separate each code that will insert in hider files and cpp files and use an accurate screeshoots if needed. Thanks in advance.
Programmed In Java In this assignment you will create a simple game. You will put all...
Programmed In Java In this assignment you will create a simple game. You will put all of your code in a class called “Game”. You may put all of your code in the main method. An example of the “game” running is provided below. Y ou will start by welcoming the user. 1. You should print "Welcome! Your starting coordinates are (0, 0).” 2. On the next line, you will tell the user the acceptable list of commands. This should...
Systems Programming - File Operations Create your version of the tail command in Linux using C...
Systems Programming - File Operations Create your version of the tail command in Linux using C The lseek system call allows you to move the current position anywhere in a file. The call lseek(fd, 0, SEEK_END) moves the current position to the end of the file. The tail command displays the last ten liens of a file. Try it. tail has to move, not to the end of the file, but to a spot ten lines before the end of...
Here are the tasks for this assignment: 1. Write a bash command line to determine if...
Here are the tasks for this assignment: 1. Write a bash command line to determine if the server 'syccuxfs01.pcc.edu' is reachable from the syccuxas01.pcc.edu server. 2. As you have read, TCP/IP messages are passed from one device to another until the message reaches its destination. Write a bash command line that will verify that no more than two (2) network devices are used to pass messages from the syccuxas01.pcc.edu server to the www.pcc.edu server. 3. Write a bash command line...
Write the line x = 0:2:20; in the Command Window of MATLAB and then create a...
Write the line x = 0:2:20; in the Command Window of MATLAB and then create a Simulink model that first loads x from the Workspace, then creates a vector y such that y = 2.5x + ex , and finally sends the vector y back to the Workspace. You will need a From Workspace block, a To Workspace block, two Constant blocks, a Product block, and a Sum block. Note that there is a sample time associated with the From...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT