Question

I need help with this. I need to create the hangman game using char arrays. I've...

I need help with this. I need to create the hangman game using char arrays. I've been provided with the three following classes to complete it. I have no idea where to start. HELP!!

1.

/**

* This class contains all of the logic of the hangman game.

* Carefully review the comments to see where you must insert

* code.

*

*/

public class HangmanGame {

private final Integer MAX_GUESSES = 8;

private static HangmanLexicon lexicon = new HangmanLexicon();

private Integer guesses;

private char [] hiddenWord; //word the user is trying to guess

private char [] userWord; // The word visible to the user(example: C--ONN---)

/**

* This constructor initialized guesses, hiddenWord and userWord.

*

* @param index This is the index that is passes to the HangmanLexicon.

* @throws Exception Will throw an exception when a index is invalid

*/

public HangmanGame(Integer index) throws Exception {

super();

//Put code here

}

// Don't change

public Integer getMaxGuesses() {

return MAX_GUESSES;

}

// Don't change

public Integer getGuesses() {

return guesses;

}

// Don't change

public char[] getUserWord() {

return userWord;

}

/**

* This method accepts a guess and determines if it is correct or not.

* It will look to see if the guess is a valid character and sees

* if the character is found in the hiddenWord attribute.

* If it is found, it updates the userWord attribute.

* @param guess The user's guess

* @return Returns true if the character guess was found. If the character was not found, return false.

*/

public Boolean guessChar(char guess)

{

// Put code here

}

/**

* Returns true if the game is lost.

* @return

*/

public Boolean isGameLost()

{

// Put code here

}

/**

* Returns true if the game is won.

* @return

*/

public Boolean isGameWon()

{

// Put code here

  

}

//Don't change

public static Integer getWordCount()

{

return lexicon.getWordCount();

}

}

2.

import java.util.Scanner;

public class HangmanConsole {

public static void main(String[] args)

{

//Put code here

//Hint: Put a loop here

}

}

3.

/*

* This class provides the words for the hangman game.

* Do not alter any of the contents of this file!

*/

public class HangmanLexicon {

/** Returns the number of words in the lexicon. */

public int getWordCount() {

return 10;

}

/**

* Returns the word at the specified index.

*

* @throws Exception

*/

public String getWord(int index) throws Exception {

switch (index) {

case 0:

return "BUOY";

case 1:

return "COMPUTER";

case 2:

return "CONNOISSEUR";

case 3:

return "DEHYDRATE";

case 4:

return "FUZZY";

case 5:

return "HUBBUB";

case 6:

return "KEYHOLE";

case 7:

return "QUAGMIRE";

case 8:

return "SLITHER";

case 9:

return "ZIRCON";

default:

throw new Exception("getWord: Illegal index");

}

};

}

Important to note:

? use the HangmanLexicon class provided
? This assignment only requires the command line section of the application. You will not need to program the hung man or read from a file.
? You will be using arrays of characters, not String objects.
? The program will only support a single run of the game (opposed to multiple games).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Random;

import java.util.Scanner;

public class ProjectNum2 {

public static void main(String[] args)

{

        Scanner kbd = new Scanner(System.in);

        String letterguess;

        String wordguess;

        String hangmanwords = "";

        char letter;

        String con = "yes";

        String quit = "no";

        int computer = 0;

        int player = 0;

        boolean done = false;

        int guess = 6;

        System.out.println("Welcome to the Hangman Word game!");

        String[] hangmanWords = { "apple", "triangle", "assassin", "mercenary",

                "loop", "snake", "lion", "algorithm", "turtle", "return",

                "message", "heart", "halloween", "door", "fruit", "band",

                "married", "summer", "choice", "elephant" };

        String[] spaces = { "_ _ _ _ _", "_ _ _ _ _ _ _ _", "_ _ _ _ _ _ _",

                "_ _ _ _ _ _ _ _ _", "_ _ _ _", "_ _ _ _ _", "_ _ _ _",

                "_ _ _ _ _ _ _ _ _", "_ _ _ _ _ _", "_ _ _ _ _ _",

                "_ _ _ _ _ _ _", "_ _ _ _ _", "_ _ _ _ _ _ _ _ _", "_ _ _ _",

                "_ _ _ _ _", "_ _ _ _", "_ _ _ _ _ _ _", "_ _ _ _ _ _",

                "_ _ _ _ _ _", "_ _ _ _ _ _ _ _" };

        int index = (int) RandomWord(hangmanWords, spaces, hangmanwords);

        System.out.println();

        System.out.println("Choose a letter or enter zero to guess the word: ");

        letterguess = kbd.next();

        String guesscomplete = "0";

        String fixedkey = letterguess.toLowerCase();

        char[] charkey = new char[letterguess.length()];

        if (letterguess.equals(guesscomplete)) {

            System.out.println("Guess the word! ");

            wordguess = kbd.next();

            if (wordguess.equals(hangmanWords[index])) {

                System.out.println("That is correct!");

                player++;

                System.out.println("Would you like to play again?");

                String again = kbd.next();

                if (again.equals(quit)) {

                    System.out.println("Computer wins: " + computer

                            + " Player wins: " + player);

                    System.out.println("See you later!");

                }

                else {

                    System.out.println("That is not correct!");

                    System.out.println("Would you like to play again?");

                    again = kbd.next();

                    computer++;

                }

            }

        }

        while (hangmanWords[index].contains(letterguess)) {

            System.out.println(hangmanWords[index].replace(hangmanWords[index],

                    letterguess));

            System.out

                    .println("Choose a letter or enter zero to guess the word: ");

            letterguess = kbd.next();

        }

        if (!hangmanWords[index].contains(letterguess)) {

            guess--;

            System.out.println("The letter is not in the word. You have "

                    + guess + " more guesses.");

            System.out

                    .println("Choose a letter or enter zero to guess the word: ");

            letterguess = kbd.next();

            if (guess == 0) {

                computer++;

            }

        }

    }

    private static Object RandomWord(String[] hangmanWords, String[] spaces,

            String hangmanwords) {

        Random ranIndex = new Random();

        int index = ranIndex.nextInt(hangmanWords.length);

        hangmanwords = spaces[index];

        System.out.print(hangmanwords);

        return (index);

    }

}

Add a comment
Know the answer?
Add Answer to:
I need help with this. I need to create the hangman game using char arrays. I've...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For a C program hangman game: Create the function int play_game [play_game ( Game *g )]...

    For a C program hangman game: Create the function int play_game [play_game ( Game *g )] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) (Also the link to program files (hangman.h and library file) is below the existing code section. You can use that to check if the code works) What int play_game needs to do mostly involves calling other functions you've already...

  • Here is the indexOf method that I wrote: public static int indexOf(char[] arr, char ch) {...

    Here is the indexOf method that I wrote: public static int indexOf(char[] arr, char ch) {            if(arr == null || arr.length == 0) {                return -1;            }            for (int i = 0; i < arr.length; i++) {                if(arr[i] == ch) {                    return i;                }            }        return -1;       ...

  • JAVA Hangman Your game should have a list of at least ten phrases of your choosing.The...

    JAVA Hangman Your game should have a list of at least ten phrases of your choosing.The game chooses a random phrase from the list. This is the phrase the player tries to guess. 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: *** ********** The user guesses a letter. All occurrences of the letter in the phrase are replaced in...

  • Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file...

    Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file with the appropriate name. public class BasicJava4 { Add four methods to the class that return a default value. public static boolean isAlphabetic(char aChar) public static int round(double num) public static boolean useSameChars(String str1, String str2) public static int reverse(int num) Implement the methods. public static boolean isAlphabetic(char aChar): Returns true if the argument is an alphabetic character, return false otherwise. Do NOT use...

  • I've previously completed a Java assignment where I wrote a program that reads a given text...

    I've previously completed a Java assignment where I wrote a program that reads a given text file and creates an index that stores the line numbers for where individual words occur. I've been given a new assignment where I need to modify some of my old code. I need to replace the indexer in my Index class with a NavigableMap<String, Word> and update my Word class with NavigableSet<Integer> lines. The instantiated objects should be TreeMap() and TreeSet(). I have below...

  • Why am I getting compile errors. rowPuzzle.java:9: error: class, interface, or enum expected public static boolean...

    Why am I getting compile errors. rowPuzzle.java:9: error: class, interface, or enum expected public static boolean rowPuzzle(ArrayList<Integer> squares, int index, ArrayList<Boolean> visited) ^ rowPuzzle.java:16: error: class, interface, or enum expected } //Java Program import java.util.*; // rowPuzzle helper function implementation // File: rowPuzzle.cpp // Header files section // function prototypes public static boolean rowPuzzle(ArrayList<Integer> squares, int index, ArrayList<Boolean> visited) { // base case // return true if the puzzle is solvable if (index == squares.size() - 1) {    return...

  • hey dear i just need help with update my code i have the hangman program i...

    hey dear i just need help with update my code i have the hangman program i just want to draw the body of hang man when the player lose every attempt program is going to draw the body of hang man until the player lose all his/her all attempts and hangman be hanged and show in the display you can write the program in java language: this is the code bellow: import java.util.Random; import java.util.Scanner; public class Hangmann { //...

  • Need help with a number guessing game in java 1) You should store prior guessses in...

    Need help with a number guessing game in java 1) You should store prior guessses in a linked list, and the nodes in the list must follow the order of prior guesses. For example, if the prior guesses are 1000, 2111, 3222 in that order, the nodes must follow the same order 2) You should store the candidate numbers also in a linked list, and the nodes must follow the order of numbers (i.e. from the smallest to the largest)....

  • Return a method as an expression tree Hi guys. I need to return a method as...

    Return a method as an expression tree Hi guys. I need to return a method as an expression tree, it's currently returning null. public static ExpressionTree getExpressionTree(String expression) throws Exception {       char[] charArray = expression.toCharArray(); Node root = et.constructTree(charArray); System.out.println("infix expression is"); et.inorder(root); return et; } In the above method, et needs to have a value. -- Take a look at the complete class below. Kindly assist. ; public class ExpressionTree extends BinaryTree { private static final String DELIMITERS...

  • Need help with writing a Client test in JAVA

    public class TutorialSpace {                  // slots — the number of available slots in a tutorial class         private int slots;         // activated — true if the tutorial class has been activated         private boolean activated;                  /**         * TutorialSpace(n) — a constructor for a tutorial class with n slots.         *          * @param n         */         public TutorialSpace(int n) {                 this.slots = n;                 this.activated = false;         }                  /**         * activate() — activates the tutorial class. Throws an exception if the         * tutorial class has already been activated.         *          * @throws NotActivatedException         */                  public void activate() throws NotActivatedException {                 if (activated) {                         throw new NotActivatedException("Already Activated");                 } else {                         activated = true;                 }         }                  /**         * reserveSlot()—enrol a student into the tutorial class by decreasing the         * number of slots left for enrolling in the class. Throws an exception if slot         * is either completely used or the tutorial class is not active.         *          * @throws EmptyException         */         public void reserveSlot() throws EmptyException {                                  if (!activated || slots == 0) {                         throw new EmptyException("Tutorial space is empty");                 } else {                         slots--;                 }         }                  /**         * slotsRemaining()—returns the number of slots remaining in the tutorial class.         */                  public int slotsRemaining() {                 return slots;         }          } public class NotActivatedException extends Exception {                  /**         *          */         private static final long serialVersionUID = 1L;                  public NotActivatedException(String msg) {                 super(msg);         }          } public class EmptyException extends Exception {                  /**         *          */         private static final long serialVersionUID = 1L;         public EmptyException(String msg) {                 super(msg);         }                  ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT