Question

00 Sprint LTE 8:02 PM o 40% Create a game of Hangman that will use arrays, loops, user-defined methods, recursion, inheritance, objects and classes, input/output, memory management. All has to be compiled as one java code
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Arrays;
import java.util.Scanner;

public class Hangman{
public static void main(String[] args) {
String[] words = {"writer", "that", "program"};
// Pick random index of words array
int randomWordNumber = (int) (Math.random() * words.length);
// Create an array to store already entered letters
char[] enteredLetters = new char[words[randomWordNumber].length()];
int triesCount = 0;
boolean wordIsGuessed = false;
do {
// infinitely iterate through cycle as long as enterLetter returns true
// if enterLetter returns false that means user guessed all the letters
// in the word e. g. no asterisks were printed by printWord
switch (enterLetter(words[randomWordNumber], enteredLetters)) {
case 0:
triesCount++;
break;
case 1:
triesCount++;
break;
case 2:
break;
case 3:
wordIsGuessed = true;
break;
}
} while (! wordIsGuessed);
System.out.println("\nThe word is " + words[randomWordNumber] +
" You missed " + (triesCount -findEmptyPosition(enteredLetters)) +
" time(s)");
}

/* Hint user to enter a guess letter,
returns 0 if letter entered is not in the word (counts as try),
returns 1 if letter were entered 1st time (counts as try),
returns 2 if already guessed letter was REentered,
returns 3 if all letters were guessed */
public static int enterLetter(String word, char[] enteredLetters) {
System.out.print("(Guess) Enter a letter in word ");
// If-clause is true if no asterisks were printed so
// word is successfully guessed
if (! printWord(word, enteredLetters))
return 3;
System.out.print(" > ");
Scanner input = new Scanner(System.in);
int emptyPosition = findEmptyPosition(enteredLetters);
char userInput = input.nextLine().charAt(0);
if (inEnteredLetters(userInput, enteredLetters)) {
System.out.println(userInput + " is already in the word");
return 2;
}
else if (word.contains(String.valueOf(userInput))) {
enteredLetters[emptyPosition] = userInput;
return 1;
}
else {
System.out.println(userInput + " is not in the word");
return 0;
}
}

/* Print word with asterisks for hidden letters, returns true if
asterisks were printed, otherwise return false */
public static boolean printWord(String word, char[] enteredLetters) {
// Iterate through all letters in word
boolean asteriskPrinted = false;
for (int i = 0; i < word.length(); i++) {
char letter = word.charAt(i);
// Check if letter already have been entered bu user before
if (inEnteredLetters(letter, enteredLetters))
System.out.print(letter); // If yes - print it
else {
System.out.print('*');
asteriskPrinted = true;
}
}
return asteriskPrinted;
}

/* Check if letter is in enteredLetters array */
public static boolean inEnteredLetters(char letter, char[] enteredLetters) {
return new String(enteredLetters).contains(String.valueOf(letter));
}

/* Find first empty position in array of entered letters (one with code \u0000) */
public static int findEmptyPosition(char[] enteredLetters) {
int i = 0;
while (enteredLetters[i] != '\u0000') i++;
return i;
}
}

Add a comment
Know the answer?
Add Answer to:
Create a game of Hangman that will use arrays, loops, user-defined methods, recursion, inheritance, objects and...
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
  • Create a modification to the Hangman game to provide enhancements for the user experience. Share the...

    Create a modification to the Hangman game to provide enhancements for the user experience. Share the code in text format in this thread. Could someone please assist me coming up with a modification in Python? 4. EXERCISE 4 The following code implements a very simple Hangman game: word = 'underutilise guesses = [] user_input = " while user_input == '0': user_input = input ("Enter a letter, or 0 to give up:') guesses.append(user_input) output = '' for letter in range(1, len...

  • 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();...

  • Create a connect four board game in java. Use multiple methods and 2d arrays. The purpose...

    Create a connect four board game in java. Use multiple methods and 2d arrays. The purpose of the game is to get four red "R" or yellow "Y" pucks in a row(vertically,horizontally, or diagonally)

  • Loops | Methods | Arrays(programming language java) in software (ATOM) Write a toolkit that includes some...

    Loops | Methods | Arrays(programming language java) in software (ATOM) Write a toolkit that includes some common functions a user might want or need An encryption/ decryption method. Both methods should take in a String (the String that needs to be changed), and an encryption value. The methods need to change the String by the value Write a method that takes an integer array as an argument. The method should return the sum of all of the elements Write a...

  • Q1. Write a program in Java         a. Create 2 separate arrays, of user defined values,...

    Q1. Write a program in Java         a. Create 2 separate arrays, of user defined values, of same size         b. Add these 2 arrays. ........................................................................................................................... Q2. Write a program in java (using loop) input any10 numbers from user and store in an array. Check whether each of array element is positive, negative, zero, odd or even number. ............................................................................................................................ Q3. Write a program in Java to display first 10 natural numbers. Find the sum of only odd numbers. .............................................................................................................................. Q4....

  • 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...

  • Create the game hangman using c code: Program description In the game of hangman, one player...

    Create the game hangman using c code: Program description In the game of hangman, one player picks a word, and the other player has to try to guess what the word is by selecting one letter at a time. If they select a correct letter, all occurrences of the letter are shown. If no letter shows up, they use up one of their turns. The player is allowed to use no more than 10 incorrect turns to guess what the...

  • Simple JavaScript and HTML: You'll create a simple word guessing game where the user gets infinite...

    Simple JavaScript and HTML: You'll create a simple word guessing game where the user gets infinite tries to guess the word (like Hangman without the hangman, or like Wheel of Fortune without the wheel and fortune). Create two global arrays: one to hold the letters of the word (e.g. 'F', 'O', 'X'), and one to hold the current guessed letters (e.g. it would start with '_', '_', '_' and end with 'F', 'O', 'X'). Write a function called guessLetter that...

  • Unrolling Recursion The objective of this problem is to simulate recursion using stacks and loops...

    Unrolling Recursion The objective of this problem is to simulate recursion using stacks and loops. A synthetic linear recursive procedure for this problem is provided in Code Fragment 1. A recursive function such as the one described is intuitive and easily understandable. On calling myRecursion(input), the execution first checks for the stopping condition. If the stopping condition is not met, then operations inside the recursive call are performed. This can include operations on local variables. The operations inside the function...

  • MEDIA INHERITANCE - C++ You will create an inheritance set of classes. Use proper rules for...

    MEDIA INHERITANCE - C++ You will create an inheritance set of classes. Use proper rules for data types, class definitions, and inheritance (check input/output of data below). You will create a super media class that will define a general form of media. A media properties will consist of the name and price. The media should be able to construct itself out of the arguments sent to it, virtually print both data fields labeled (price to two decimal places) and overload...

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