To install StudyMoose App tap and then “Add to Home Screen”
Save to my list
Remove from my list
The objective of this lab assignment is to create a Hangman game in Java. The game will randomly select a word from a predefined array of words, allow the user to guess one letter at a time, display the progress with asterisks, and track the number of misses. When the user either correctly guesses the word or exceeds the maximum allowed misses, the game will display the result and ask if the user wants to play again.
This Hangman game will be implemented in Java and will consist of the following components:
The main program handles the overall flow of the Hangman game.
It randomly selects a word from the predefined array of words, initializes variables to keep track of the user's progress and missed guesses, and controls the game loop.
The game loop continues until the user decides not to play anymore. Within the loop, the following actions occur:
hangman
function is called to start the game with the selected word.
import java.util.*;
public class Lab3_1 {
public static void main(String[] args) {
String[] words = { "apple", "oranges", "write", "that", "instructor" };
Scanner stdin = new Scanner(System.in);
boolean play = true;
while (play) {
int index = (int)(Math.random() * words.length);
String word = words[index];
hangman(word);
System.out.print("Do you want to guess another word? Enter y or n: ");
play = stdin.next().charAt(0) == 'y';
}
}
}
hangman
FunctionThe hangman
function is responsible for executing an individual game of Hangman with a given word.
It initializes variables for missed guesses, tracks the user's progress in guessing the word, and handles the game loop.
Within the game loop, the following steps occur:
public static void hangman(String word) {
Scanner stdin = new Scanner(System.in);
int missedGuesses = 0;
char[] userWord = new char[word.length()];
char userLetter;
while (true) {
boolean userWon = true;
for (int i = 0; i < userWord.length; i++) { if (userWord[i] != word.charAt(i)) { userWon = false; break; } } if (missedGuesses > word.length()) {
printResult(word, missedGuesses);
System.out.println("You lost!");
System.exit(0);
}
System.out.print("Guess a letter in the word ");
for (char c : userWord) {
if (c >= 'A' && c <= 'z') {
System.out.print(c);
} else {
System.out.print("*");
}
}
System.out.print(" : ");
userLetter = stdin.next().charAt(0);
for (char c : userWord) {
if (userLetter == c) {
System.out.println(userLetter + " is already in the word");
break;
}
}
int[] letterIndexes = new int[word.length()];
Arrays.fill(letterIndexes, -1);
boolean found = false;
for (int i = 0; i < word.length(); i++) { if (userLetter == word.charAt(i)) { letterIndexes[i] = i; found = true; } } if (found) { for (int letterIndex : letterIndexes) { if (letterIndex >= 0) {
userWord[letterIndex] = userLetter;
}
}
} else {
missedGuesses++;
System.out.println(userLetter + " is not in the word");
}
if (userWon) {
printResult(word, missedGuesses);
break;
}
}
}
printResult
FunctionThe printResult
function is responsible for displaying the result of an individual game. It shows the correct word and the number of missed guesses to the user.
public static void printResult(String word, int missedGuesses) {
System.out.println("The word is " + word + ". You missed " + missedGuesses + " times.");
}
Let's consider an example run of the Hangman game to illustrate its functionality:
(Guess) Enter a letter in word ****** > p
(Guess) Enter a letter in word p****** > r
(Guess) Enter a letter in word prr > p
p is already in the word
(Guess) Enter a letter in word prr > o
(Guess) Enter a letter in word pro*r** > g
(Guess) Enter a letter in word progr** > n
n is not in the word
(Guess) Enter a letter in word progr** > m
(Guess) Enter a letter in word progr"m > a
The word is program. You missed 1 time
Do you want to guess another word? Enter y or n: n
In conclusion, this lab assignment has successfully implemented a Hangman game in Java. The game randomly selects words from an array, allows the user to guess letters, displays the progress, and keeps track of missed guesses. It provides an entertaining way to improve Java programming skills while also showcasing the use of arrays, loops, conditionals, and functions.
1. Java Arrays Documentation: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Arrays.html
Lab Assignment Report - Hangman Game. (2023, Aug 04). Retrieved from https://studymoose.com/document/comp-182-lab-assignment-3-1-hangman-extra-credit
👋 Hi! I’m your smart assistant Amy!
Don’t know where to start? Type your requirements and I’ll connect you to an academic expert within 3 minutes.
get help with your assignment