Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Java Hangman Assignment

Tags:

java

I'm stuck on a java assignment for a class where we need to make a Hangman game but a REALLY BASIC one (It's an intro to Java class). Basically I have a word entered by someone and the other person has to guess the word but they dont see the word so it displays it like this for example (if the word is aardvark)

* * * * * * * *

Then the user inputs a letter and if its part of the word it then displays those letters, example:

Enter letter: a
a a * * * a * *

Enter letter: k
a a * * * a * k

Enter letter: r
a a r * * a r k

And so one...so yeah I've been stuck on this for a while and I REALLY need help Thanks

P.S: This is an intro class so all I know so far are loops (for, while, do while etc), if, if/else, switch statements etc.

import java.util.Scanner;

public class ass_2 {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    //  public static final Comparator<secretWord> CASE_INSENSITIVE_ORDER;

    int attempts = 10;
    int wordLength;
    boolean solved;
    Scanner userInput = new Scanner(System.in);


    System.out.println("OK Guessing Player ... turn around, while your friend enters the word to guess!\n");
    System.out.println("Other Player ‐ Enter your word (letters only, no repeated letters and not case sensitive):");

    String secretWord = userInput.next();


    // 20 blank spaces WITH a for loop, we're smart!
    for(int i = 1; i <= 20; i++)
        System.out.print("\n");


        Scanner userLetter = new Scanner(System.in);
        String letter;

        System.out.print("Word to date: ");
        for (int i = 0; i < secretWord.length(); i++)
        {
            System.out.print("*");
        }

        while (attempts <= 10 && attempts > 0)
        {
            System.out.println("\nAttempts left: " + attempts);
            System.out.print("Enter letter: ");

            attempts--;
        }

        System.out.println("\n---------------------------");
        System.out.println("Sorry you didn't find the mystery word!");
        System.out.println("It was \"" + secretWord + "\"");

}

}

like image 229
Jordan Avatar asked Feb 14 '11 01:02

Jordan


People also ask

What is Hangman game in C?

The hangman game code in c is developed using C programming language. This hangman game in c programming is about guessing letters (A-Z) to form the words. A hangman game is a common word guessing game in which the player must guess one letter at a time to complete a missing word.


1 Answers

Hey Jordan, your first attempt looks very good! You only need some more logic inside the while loop to read guesses and replace "*"s with correct guesses. And I would advise you to store the obfuscated word ("*****...") in a string also instead of just printing it out, will be handy later on..

Judging by your code you don't need any help on user input, your only problem is correct replacing of stars with right guesses, lets get to it:

String secret;
//read in secret string
String displaySecret;
//generate as many "*"s as secret is long and store them in displaySecret

Now the cool thing is this:

...no repeated letters...

which will make your assignment much easier! Look at the documentation of the String class provided by Williwaw. There you'll find two methods with which will lead to the solution:

  • One method finds the first occurrence of a character inside a string and outputs its position. And since you don't accept duplicate letters, that'll also be the only occurrence.
  • The other method can replace the character at a given position inside a string with another character.

I think from that you'll find the solution easily. Feel free to ask further questions in the comments!

EDIT: Some more help

String secret = "example-text";
String displaySecret = "";
for (int i = 0; i < secret.length(); i++)
    displaySecret += "*";

char guess;
//read in a guess
int position = secret.indexOf(guess);
//now position contains the index of guess inside secret, or
//-1 if the guess was wrong

String newDisplaySecret = "";
for (int i = 0; i < secret.length(); i++)
    if (i == position)
        newDisplaySecret += secret.charAt(i); //newly guessed character
    else
        newDisplaySecret += displaySecret.charAt(i); //old state

displaySecret = new String(newDisplaySecret);

Damn I was sure there was some kind of setCharAt(int) method.. the loop does the job.

like image 115
Dave O. Avatar answered Oct 14 '22 14:10

Dave O.