Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java Hangman Game

  • 31-01-2014 3:18pm
    #1
    Registered Users, Registered Users 2 Posts: 2,369 ✭✭✭


    Hey I've been given an assignment for my college course. It doesn't count towards any grades just an assignment to get me familiar with java content before I do my project. I really want to do well in java programming so help with this program issue would help me greatly!

    Here is what I was supposed to and I've noted the tasks I've completed.

    Develop an application that allows the user to play a Hangman game. The game should store a secret word (which you can choose and hard code in a variable) and allow the user to guess the word one letter at a time. The game should begin by printing a * for every letter in the word. When the user guesses a letter correctly, that letter should replace the appropriate * and updated string be printed to the user.

    • Task 1: Add functionality to the app to allow the user to guess a letter 10 times and give appropiate output each time
    • Task 2: Add further functionality to check if the user has guessed the word completely. If they have not guessed the full word correctly, continue playing the game until they reached 10 guesses.
    • Task 3: Give the user an instruction at the start of the game to inform them that they have 5 lives. Each time they guess a letter which is not present in the word one life is lost. When all lives are gone, the game should end.
    • Task 4: Modify the app so that rather than hard coding one secret word. 10 words are stored in an array and when the program is run, one word is chosen at random as the secret word.
    • Task 5: Finally, modify the app so that when the game ends the user is asked would they like to play again? At this point a new secret word should be chosen and the lives and guesses reset to 5 & 10


    I am having difficulty with task 3 to 5 and I've declared and created my arrays correctly in the instantiable constructor correctly I think.



    Here is my code. **I use JOptionPane for Input and Output**



    Instantiable Hangman.java


    public class Hangman{

    private String word;
    private String secret;
    private String randomSecret[]={"football", "car", "dumbledore", "potter", "random", "pokemon", "rasher", "bacon", "ruby", "java"};
    private char letter;
    private StringBuffer strBuff;
    private int x;

    //constructor

    public Hangman(){
    word = " ";
    letter = ' ';
    secret = "house";
    String randomSecret[] = new String[10];
    randomSecret[0] = "football";
    randomSecret[1] = "car";
    randomSecret[2] = "dumbledore";
    randomSecret[4] = "potter";
    randomSecret[5] = "random";
    randomSecret[6] = "pokemon";
    randomSecret[7] = "rasher?";
    randomSecret[8] = "bacon";
    randomSecret[9] = "ruby";
    randomSecret[10] = "java";
    strBuff = new StringBuffer("*****");

    x = (int)((Math.random()*9)+1);

    for(int i = 0; i < secret.length()-1; i = i++){
    strBuff.append("*");
    }
    word = toString();
    }

    //set method

    public void setLetter(char letter){
    this.letter = letter;
    }

    //compute

    public void compute(){
    for(int i = 0; i < secret.length(); i++){
    if(letter == secret.charAt(i)){
    strBuff.setCharAt(i, letter);
    }

    word = strBuff.toString();

    }
    }



    //get method

    public String getWord(){
    return word;
    }

    public String getSecret(){
    return secret;
    }
    }


    HangmanApp



    import javax.swing.JOptionPane;

    public class HangmanApp{
    public static void main(String args[]){

    //variables

    String word, secret, user, randomSecret[], flag;
    char letter;

    //create & declare

    Hangman myHangman = new Hangman();
    myHangman.compute();
    word = myHangman.getWord();
    secret = myHangman.getSecret();

    //input
    for(int i = 0; i < 10; i++){
    user = JOptionPane.showInputDialog(null, "Please guess a letter");
    letter = user.charAt(0);
    myHangman.setLetter(letter);
    myHangman.compute();
    word = myHangman.getWord();
    JOptionPane.showMessageDialog(null, "The secret word is: "+word);

    if(word.equals(secret)){
    JOptionPane.showMessageDialog(null, "Congratulations you guessed it correctly!");
    i = 10;
    }

    else{
    user = JOptionPane.showInputDialog(null, "Guess a letter");
    letter = user.charAt(0);

    }

    secret = myHangman.getSecret();
    JOptionPane.showMessageDialog(null, "The word you were trying to guess is "+secret);

    }

    }


Comments

  • Registered Users, Registered Users 2 Posts: 2,369 ✭✭✭LostBoy101


    Managed to solve it today. I put the random number generator inside the randomSecret array in the constructor

    So should be something like this

    public Hangman {

    secret = randomSecret[(int) (math.random *10)];

    }


Advertisement