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

    Red face Need help with Hangman!!!

    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
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default

    how to add the high scores and prompt the users name.
    Not sure what "add" means. Normal you add two numbers by using the + operator.
    If you mean you want to save the scores, you need some kind of container(like an array or ArrayList or Hashmap) to save the scores in. Then if you want to save the scores between executions of the program, you will need to write the scores to a file. Depends on how you want to do it.
    To prompt for the user's name you can use the JOptionPane class. There are many examples of code here if you use Search.

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

    Default

    Double posted in this same forum.

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

    Default

    Sorry about that!

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

    Default I need to finish this by tonight

    could someone plz tell me how to create high scores in hangman

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default

    how to create high scores in hangman
    I've forgotten the rules. It seems like the object was to guess the word within as few a guesses as possible. I don't know how to set a score depending on the number of guesses or what.
    Seems the best score would be to guess the word the first time.
    How much would that be worth? vs not finding the word before the number of guesses were taken.

    So how do you score it?

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

    Default

    I still dont know how to create high scores. I am suppose to create a seperate file with name and scores of the people in it. I know that i have to create an array for names and scores, but after that i m confused.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default

    how to create high scores
    How do you create any scores?
    Are you asking how to save a player's score?
    Or how to add together a player's scores from different games?
    Please explain.

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

    Default

    How to save players scores

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,607
    Rep Power
    23

    Default

    Use an ArrayList to save the scores for a player. Then use another ArrayList to save the players' ArrayList of scores. Sort of like a 2 dim array.

  11. #11
    J-Live is offline Member
    Join Date
    Oct 2008
    Posts
    24
    Rep Power
    0

Similar Threads

  1. Hangman Game
    By L23 in forum New To Java
    Replies: 8
    Last Post: 07-03-2008, 01:56 PM
  2. 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
  •