Results 1 to 6 of 6

Thread: Hangman Help!!!

  1. #1
    chinasome is offline Member
    Join Date
    Nov 2008
    Posts
    7
    Rep Power
    0

    Post Hangman Help!!!

    How do I modify my program so it stores players' "high scores." When a game begins, prompt the user for their name. When a game ends, display the top 5 scores and player names, displayed from highest to lowest.

    I have got the hangman game working but i dont know how to add the high scores and prompt the users name. can someone help thank you.

    Here is my code:

    import acm.graphics.*;
    import acm.program.*;
    import acm.util.*;
    import java.util.*;
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;

    // Plays the Hangman game
    public class Hangman extends GraphicsProgram
    {
    private final String[] letters = {"A","B","C","D","E","F","G","H","I","J"...
    "N","O","P","Q","R","S","T","U","V","W... String array of alphabets

    private final String[] words = {"HAPPY","CONFOUND","DESERT","APPLE","JO... String array of words

    private GLabel[] alpha = new GLabel[26]; // Array of alphabets as GLabel
    private GLabel[] word = new GLabel[4]; // Array of words as GLabel
    private GLabel[] answer = new GLabel[8]; // Array of letters as GLabel
    private GObject[] manParts = new GObject[6]; // Array of hangman body parts

    private int randomnumber = 0;
    private int counter = 0;
    private int wrongGuess = -1;
    private int guess = 0;

    // Runs the program
    public void run()
    {
    addMouseListeners();
    setSize(500,500);
    Random rnd = new Random(); // Picks a random word
    randomnumber = rnd.nextInt(4);

    alphabet();
    randomWords();
    drawLetters();
    drawLines();
    answerWord();
    drawMan();
    }

    // Enters the String array of alphabets into GLabel array
    public void alphabet()
    {
    for(int i = 0; i < letters.length; i++)
    {
    alpha[i] = new GLabel(String.valueOf(letters[i]));
    }
    }

    // Enters the String array of words into GLabel array
    public void randomWords()
    {
    for(int i = 0; i < words.length-1; i++)
    {
    word[i] = new GLabel(words[i]);
    }
    }

    // Draws the alphabets on to the screen
    public void drawLetters()
    {
    int x = 400;
    int y = 50;
    int w = 50;

    for(int i = 0; i< letters.length/2; i++)// A-M column
    {
    add(alpha[i],x,y);
    y += 25;
    }
    for(int z = letters.length/2; z<26; z++)// N-Z column
    {
    add(alpha[z],x+50,w);
    w += 25;
    }
    }

    // Draws appropriate number of lines for each letter to guess
    public void drawLines()
    {
    int x1 = 120;
    int x2 = 140;
    int y = 450;

    GLine[]line = new GLine[8];
    for(int i = 0; i< words[randomnumber].length(); i++)
    {
    line[i] = new GLine(x1,y,x2,y);
    add(line[i]);
    x1 +=25;
    x2 +=25;
    }
    }

    // Each letter of the answer word is turned to a GLabel and set invisible
    public void answerWord()
    {
    int x = 125;
    int y = 445;
    char current;

    for(int i = 0; i < words[randomnumber].length(); i++)
    {
    current = words[randomnumber].charAt(i);
    answer[i] = new GLabel(String.valueOf(current),x,y);
    answer[i].setVisible(false);
    add(answer[i]);
    x +=25;
    }
    }

    // Allows the user to interact with the applet using a mouse
    public void mouseClicked(MouseEvent e)
    {
    char current;

    GObject obj = getElementAt(e.getX(), e.getY()); // Gets info from what the user clicked
    GLabel a = (GLabel)obj; // Casts GObject to GLabel

    if(wrongGuess <4) // Allow 6 guesses
    {
    for(int i = 0; i < word[randomnumber].getLabel().length(); i++)
    {
    current = words[randomnumber].charAt(i);
    if(a.getLabel().charAt(0) == current )
    {
    answer[i].setVisible(true);
    }
    else if(a.getLabel().charAt(0) != current)
    {
    guess++;
    }
    }
    }
    else
    {
    wrongGuess++;
    youLose(); // If the guess is wrong "YOU LOSE"
    }

    if(guess == words[randomnumber].length())
    {
    wrongGuess++;
    guess = 0;
    }
    else
    {
    guess = 0;
    }
    remove(obj);
    add(manParts[wrongGuess]); // Adds body part
    }

    // Displays "YOU LOSE" on the applet
    public void youLose()
    {
    GLabel lose = new GLabel("YOU LOSE",250,250);
    add(lose);
    }

    // Combines all the bodyparts
    public void drawMan()
    {
    deathBed();
    head();
    body();
    leftArm();
    rightArm();
    leftLeg();
    rightLeg();
    }

    // Draws the stand
    public void deathBed()
    {
    GLine deathBed1 = new GLine(100,150,100,100);
    GLine deathBed2 = new GLine(100,100,200,100);
    GLine deathBed3 = new GLine(200,100,200,400);
    GLine deathBed4 = new GLine(50,400,250,400);

    add(deathBed1);
    add(deathBed2);
    add(deathBed3);
    add(deathBed4);
    }

    // Draws the head
    public void head()
    {
    GOval head = new GOval(75,150,50,50);
    manParts[0] = head;
    }

    // Draws the body
    public void body()
    {
    GLine body = new GLine(100,200,100,300);
    manParts[1] = body;
    }

    // Draws the left arm
    public void leftArm()
    {
    GLine leftArm = new GLine(100,210,50,220);
    manParts[2] = leftArm;
    }

    // Draws the right arm
    public void rightArm()
    {
    GLine rightArm = new GLine(100,210,150,220);
    manParts[3] = rightArm;
    }

    // Draws the left leg
    public void leftLeg()
    {
    GLine leftLeg = new GLine(100,300,75,375);
    manParts[4] = leftLeg;
    }

    // Draws the right leg
    public void rightLeg()
    {
    GLine rightLeg = new GLine(100,300,125,375);
    manParts[5] = rightLeg;
    }
    }

    Thank You

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    So you don't reply to Norm's response in your initial thread and then post the same darn question again? Why?

  3. #3
    chinasome is offline Member
    Join Date
    Nov 2008
    Posts
    7
    Rep Power
    0

    Post

    I am new to java sorry about that i really need to finish it could you help plz so sorry about that

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    i really need to finish it
    well double posting will only tick some folks off and will have the opposite effect that you intended.

    As to a solution, what have you tried?
    If I were doing this, I'd try to separate my logical into different classes, to divide and conquer. Then I could solve each problem in isolation of the other.

    Other suggestions:
    1) Using code tags will help make your code retain its formatting and make it much more readable. You can do this by simply placing [cod&#101;] on top of your code block and [/code] on the bottom.
    2) Try to post code samples that are compilable and runnable, that way we can run it on our boxes and be better able to help you. Your current code uses some nonstandard "acm" classes that the rest of us don't have access to. I'm assuming that these classes involve the GUI side of your program. So this is another reason to separate parts of your code into separate logical entities including a GUI class that uses these non-standard classes, a Model class that encapsulates the logic of your program and a DataStorage (don't really need a full-fledged database here) class that allows you to get users, scores, and store them. This way we can compile and alter your non-GUI components since the GUI coding isn't really the issue here, is it?

    Good luck.
    Last edited by Fubarable; 11-07-2008 at 09:43 PM.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,272
    Rep Power
    25

    Default

    See other thread.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. Need help with Hangman!!!
    By chinasome in forum New To Java
    Replies: 10
    Last Post: 11-09-2008, 04:42 AM
  2. Hangman Game
    By L23 in forum New To Java
    Replies: 8
    Last Post: 07-03-2008, 01:56 PM
  3. Create the game Hangman
    By barney in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 06:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •