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

**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...
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...
Create a project plan on the game or application you are creating. The project plan should...
Create a project plan on the game or application you are creating. The project plan should include the following: A description of the game or application The IDE or game engine your plan to use to create the game or app and information on how you are going to develop the game or app If you choose to create a game, how are you going to approach the game design and game development process or if you choose an application...
Building a Command Line Interpreter (myshell) 1.Goal of this programming assignment The primary goal of this...
Building a Command Line Interpreter (myshell) 1.Goal of this programming assignment The primary goal of this assignment is to understand and gain some familiarity with the system call interface in Unix Environment. In this assignment you will be implementing your own command line interpreter like a Unix shell program. YOU ARE NOT IMPLEMENTING COMMANDS! YOUR program should just FORK off programs and EXECUTE them. 2. Requirements (1) Programming language: You have to use either C or C++ to develop your...
You are playing a version of the roulette game, where the pockets are from 0 to...
You are playing a version of the roulette game, where the pockets are from 0 to 10 and even numbers are red and odd numbers are black (0 is green). You spin 3 times and add up the values you see. What is the probability that you get a total of 15 given on the first spin you spin a 2? What about a 3? Solve by simulation and analytically.
The program should be able to do the following: In Java accepts one command line parameter....
The program should be able to do the following: In Java accepts one command line parameter. The parameter specifies the path to a text file containing the integers to be sorted. The structure of the file is as follows: There will be multiple lines in the file (number of lines unknown). Each line will contain multiple integers, separated by a single whitespace. reads the integers from the text file in part a into an array of integers. sort the integers...
Discuss the pros and cons of an IDE vs command-line interpreters. Why are command-line interpreters still...
Discuss the pros and cons of an IDE vs command-line interpreters. Why are command-line interpreters still being used? Be sure to argue both sides.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT