I need help with this. I need to create the hangman game using char arrays. I\'v
ID: 652105 • Letter: I
Question
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).
Explanation / Answer
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);
}
}