Results 1 to 11 of 11
Thread: Need help with Hangman!!!
- 11-07-2008, 09:12 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 7
- Rep Power
- 0
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
- 11-07-2008, 02:29 PM #2
Not sure what "add" means. Normal you add two numbers by using the + operator.how to add the high scores and prompt the users name.
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.
-
Double posted in this same forum.
- 11-07-2008, 09:11 PM #4
Member
- Join Date
- Nov 2008
- Posts
- 7
- Rep Power
- 0
Sorry about that!
- 11-07-2008, 09:13 PM #5
Member
- Join Date
- Nov 2008
- Posts
- 7
- Rep Power
- 0
I need to finish this by tonight
could someone plz tell me how to create high scores in hangman
- 11-07-2008, 10:05 PM #6
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.how to create high scores in hangman
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?
- 11-07-2008, 11:14 PM #7
Member
- Join Date
- Nov 2008
- Posts
- 7
- Rep Power
- 0
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.
- 11-08-2008, 12:00 AM #8
How do you create any scores?how to create high 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.
- 11-09-2008, 12:45 AM #9
Member
- Join Date
- Nov 2008
- Posts
- 7
- Rep Power
- 0
How to save players scores
- 11-09-2008, 02:43 AM #10
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-09-2008, 04:42 AM #11
Member
- Join Date
- Oct 2008
- Posts
- 24
- Rep Power
- 0
Similar Threads
-
Hangman Game
By L23 in forum New To JavaReplies: 8Last Post: 07-03-2008, 01:56 PM -
Create the game Hangman
By barney in forum New To JavaReplies: 1Last Post: 08-06-2007, 06:16 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks