Lab Assignment Report - Hangman Game

Categories: Technology

Objective

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.

Procedure

This Hangman game will be implemented in Java and will consist of the following components:

Main Program

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:

  1. A random word is selected from the array of words.
  2. The hangman function is called to start the game with the selected word.
  3. After the game is completed, the user is prompted to play again, and the loop continues if the user chooses to play.

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 Function

The hangman function is responsible for executing an individual game of Hangman with a given word.

Get to Know The Price Estimate For Your Paper
Topic
Number of pages
Email Invalid email

By clicking “Check Writers’ Offers”, you agree to our terms of service and privacy policy. We’ll occasionally send you promo and account related email

"You must agree to out terms of services and privacy policy"
Write my paper

You won’t be charged yet!

Get quality help now
Sweet V
Sweet V
checked Verified writer

Proficient in: Technology

star star star star 4.9 (984)

“ Ok, let me say I’m extremely satisfy with the result while it was a last minute thing. I really enjoy the effort put in. ”

avatar avatar avatar
+84 relevant experts are online
Hire writer

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:

  1. The function checks if the user has already guessed the entire word. If so, it displays the word and the number of missed guesses, and the game ends.
  2. If the number of missed guesses exceeds the length of the word, the function informs the user that they have lost, displays the word, and exits the program.
  3. Otherwise, the function prompts the user to guess a letter and displays the current progress with asterisks and already guessed letters.
  4. The user's letter guess is checked against the word, and if it's correct, the user's progress is updated accordingly. If not, the number of missed guesses is incremented.

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 Function

The 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.");
}

Example Run

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

Conclusion

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.

References

1. Java Arrays Documentation: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Arrays.html

Updated: Jan 03, 2024
Cite this page

Lab Assignment Report - Hangman Game. (2023, Aug 04). Retrieved from https://studymoose.com/document/comp-182-lab-assignment-3-1-hangman-extra-credit

Lab Assignment Report - Hangman Game essay
Live chat  with support 24/7

👋 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