The purpose of this assignment is to have you solve a somewhat larger problem wh
ID: 3865567 • Letter: T
Question
The purpose of this assignment is to have you solve a somewhat larger problem whose solution incorporates the programming concepts that you have been studying: Variables, Data Types, Control Structures (if-then, if-then-else, loops, switch, etc.), Method Definitions, and Arrays. Your problem is to create a Java program to implement the game “Hang Man.” Hang Man is a relatively simple game that requires the user to guess the letters of a word before running out of incorrect guesses. Each incorrect guess will “build” another part of the Man who is hanging from the gallows. When the game starts, the user is presented with an “unoccupied” gallows, all the letters in the alphabet, and a set of blanks representing the letters in the word:
I need a program for hangman on javascript with the following requirements:
1. Your program must have an array of words (Strings) from which one of the words is randomly chosen to play a round of the game. Please don’t include more that 5 words so that I can easily test your program: String [ ] words = { “javascript”, “declaration”, “object”, “class”, “failing” };
2. Your program must contain at least one character array such as: char [ ] alphabet;
3. Your program must contain at least 3 method definitions not including the main method definition.
4. One of the method definitions must be a method called: explode The explode method is sent a string such as: “javascript” and returns an array of characters containing the letters that appear in the words: ‘j’ ‘a’ ‘v’ ‘a’ ‘s’ ‘c’ ‘r’ ‘I’ ‘p’ ‘t’
When the user is asked to make a letter guess, you can assume that the user will type a valid character and you do not need to check this.
Explanation / Answer
import java.util.Random;
import java.util.Scanner;
public class Hangman
{
//Will contain the guesses that the user has.
private static char[] wordToGuess;
// This will check the random words length to be set to the wordToGuess[]
private static int wordLength;
// temporarily store the random word for the user to guess.
private static String guessWord;
private static char[] dash;
private static char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();
private static char usersGuess;
private static boolean gameOver = false;
private static String [ ] words = { "javascript", "declaration", "object", "class", "failing" };
private static String testWord;
private static int count = 0;
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
Random rand = new Random();
//random nextInt(5) Will return a number 0-4.
int num = rand.nextInt(5);
//guessWord is being assigned a random String from the [] words
guessWord = words[num];
wordLength = guessWord.length();
dash = new char[wordLength];
dash = guessWord.toCharArray();
dashArray(guessWord);
while ((7 - count) != 0 && gameOver != true)
{
int numberOfGuesses = 0;
printGallows();
System.out.println(" ");
printAlphabet(dash);
wordToGuess = explode(guessWord);
System.out.println(" Please enter a letter for the game");
usersGuess = input.nextLine().charAt(0);
updateAlphabet(usersGuess);
wordToGuess = guessWord.toCharArray();
for (int i = 0; i < wordToGuess.length; i++)
{
if (wordToGuess[i] == usersGuess)
{
dash[i] = usersGuess;
testWord = new String(dash);
}
}
for (int i = 0; i < guessWord.length(); i++)
{
if (usersGuess != wordToGuess[i])
{
numberOfGuesses++;
}
}
if (numberOfGuesses == wordLength)
{
count++;
}
System.out.println("You have " + (7 - count) + " guesses left");
endGame();
}
System.out.println((gameOver != true) ? "You lost the game!" : "You won the game!");
if (count == 7)
{
System.out.println(" -------");
System.out.println("| | ");
System.out.println("| O");
System.out.println("| /|\");
System.out.println("| |");
System.out.println("| / \");
System.out.println("The correct word is: " + guessWord);
}
}
// Takes in a String uses the method toCharArray(); to convert to a char[]
public static char[] explode(String s)
{
wordLength = s.length();
wordToGuess = new char[wordLength - 1];
wordToGuess = s.toCharArray();
return wordToGuess;
}
// Takes in a character array and prints each character with a space.
public static void printAlphabet(char[] c)
{
for (int i = 0; i < c.length; i++)
{
System.out.print(c[i] + " ");
}
System.out.println(" ");
}
//
public static char[] dashArray(String s1)
{
dash = new char[wordLength];
for (int i = 0; i < dash.length; i++)
{
dash[i] = '_';
}
return dash;
}
// This method will remove letters in the alphabet and replace with a dash.
public static char[] updateAlphabet(char guess)
{
for (int i = 0; i < alphabet.length; i++)
{
if (alphabet[i] == (guess))
{
alphabet[i] = '_';
}
System.out.print("" + alphabet[i] + " ");
}
System.out.println("");
return alphabet;
}
// This method prints the gallows for the user.
// if the users input is incorrect it will display HANG MAN.
public static void printGallows()
{
System.out.println(" -------");
System.out.println("| | ");
if (count == 1)
{
System.out.println("| O");
}
if (count == 2)
{
System.out.println("| O");
System.out.println("| |");
}
if (count == 3)
{
System.out.println("| O");
System.out.println("| /|");
}
if (count == 4)
{
System.out.println("| O");
System.out.println("| /|\");
}
if (count == 5)
{
System.out.println("| O");
System.out.println("| /|\");
System.out.println("| |");
}
if (count == 6)
{
System.out.println("| O");
System.out.println("| /|\");
System.out.println("| |");
System.out.println("| /");
}
System.out.println("|");
System.out.println("|");
System.out.println("|");
printAlphabet(alphabet);
}
public static boolean endGame()
{
if (guessWord.equals(testWord))
{
gameOver = true;
return gameOver;
}
return gameOver;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.