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
