943,666 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 2078
  • Java RSS
Feb 29th, 2008
0

How do i create a High score class using Arrays

Expand Post »
I Would like to create a High Score For a Game in jave Called Jetman. i have created a class in java called Score and Score list to display the score but i do not know how to create a highscore using an Array List

the Score Class is

package jetman;
import java.io.Serializable;


public class Score implements Comparable<Score>,
Serializable {

private String name;//int should have more than enough space - if score overflows
private int score; //the user's cheated so they can accept the concequences!!

/** Creates a new instance of Score */
public Score(String name, int score) {
this.name = name;
this.score = score;
}
public Score(){
this.name = "";
this.score = -1;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getScore() {
return score;
}

public void setScore(int score) {
this.score = score;
}

public int compareTo(Score o) {
return score - o.score;
}

int getNumRecords() {
return 0;
}

}


and the Score List is
package jetman;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;



public class scoreList {
String filePath = "./score.txt";
private ArrayList<Score> scoreArrayList = new ArrayList<Score>();



/** Creates a new instance of scoreList */
public scoreList() {
FileWriter writer = null;
try {
writer = new FileWriter("output.txt");
} catch (IOException ex) {
ex.printStackTrace();
}
PrintWriter out = new PrintWriter(writer);
out.println(this);

out.println(10.55);

//this.add(scoreArrayList);

}

public ArrayList<Score> getScoreArrayList() {
return scoreArrayList;
}

public void setScoreArrayList(ArrayList<Score> scoreArrayList) {
this.scoreArrayList = scoreArrayList;

}

public void removeScoreArrayList (Score s) {
scoreArrayList.remove(s);
this.scoreArrayList = scoreArrayList;
}

private Score add(Score s) {
scoreArrayList.add(s);
return s;
}


}

Thank You
Last edited by Helpwithjava; Feb 29th, 2008 at 5:35 pm. Reason: change in title
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Helpwithjava is offline Offline
4 posts
since Feb 2008
Feb 29th, 2008
0

Re: Help With Java

I would like to know how to create a high score system for a java game using an array List. Thankyou
You are going to have to get a lot more specific to get help.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Feb 29th, 2008
0

Re: Help With Java

O.K. Good, you've added code. So you want to "create a high score using an array list". So you intend to put all the Scores into an ArrayList and you want to pick the single highest Score FROM that ArrayList? I'm guessing that is what you mean. I would write a function that traverses an ArrayList of type Score and extracts the Score with the maximum score (note capitalization difference). Since your Score class also has "name" in it in addition to "score", you would not be able to compare two Scores directly but would instead have to extract "score" from each Score and return the highest from the Score ArrayList. Also, in code like this:
Java Syntax (Toggle Plain Text)
  1. public void setScoreArrayList(ArrayList<Score> scoreArrayList) {
  2. this.scoreArrayList = scoreArrayList;
  3.  
  4. }
it is easy to get confused when the parameter's name is the same as the data member's name. It's better to choose a slightly different name for the parameter, like:
Java Syntax (Toggle Plain Text)
  1. public void setScoreArrayList(ArrayList<Score> scoreList) {
  2. scoreArrayList = scoreList;
  3.  
  4. }
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Mar 1st, 2008
0

Re: Help With Java

Sorry about this i have writen this code is this correct and how do i add a sort so only the top 5 scores are displayed

The code

/*
* highscore.java
*
* Created on 29 February 2008, 19:54
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package jetman;

import java.util.ArrayList;
import java.util.Vector;
import java.util.Random;

public class highscore extends Vector {
private String name; // name for the Palyer
private ArrayList<Score> scorelist;


// create a new instance
public highscore(String name){
this.name = name;
this.scorelist = new ArrayList <Score>(); // this is a Array List
}


public highscore(){
this.name =" No Name";
this.scorelist = new ArrayList <Score>();
}


public void setScoreArrayList(ArrayList<Score> scoreList) {
scorelist = scorelist;

}

/** Removes the product at index i */
public void removeScore(int i){ // Removes a score from the list
scorelist.remove(i);
}


// get the name of the player
public String getName() {
return name;
}


public Score index(int i){
if(i < scorelist.size() && i >=0)return scorelist.get(i);
return null;
}


/** Returns a textual representation of the Shop object. */
public String toString() {
return "name" + name + "Scorelist" + scorelist;
}

}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Helpwithjava is offline Offline
4 posts
since Feb 2008
Mar 1st, 2008
0

Re: Help With Java

You still have some of the same problems as far as having parameters with the same names as the data members:

Java Syntax (Toggle Plain Text)
  1. public void setScoreArrayList(ArrayList<Score> scoreList) {
  2. scorelist = scorelist;
  3.  
  4. }
  5.  
  6.  
  7. public highscore(String name){
  8. this.name = name;
  9. this.scorelist = new ArrayList <Score>(); // this is a Array List
  10. }

setScoreArray looks like it's just a typo. O.K. You have an ArrayList. It looks like each player has an ArrayList of Score, so there'll be one object of type highscore for each player. I don't see anything that involves a vector, just the ArrayList. I also don't see any function that adds a single Score to the ArrayList in this class, which I think you need. I see a removeScore, but no addScore. So I am imagining that you want the Scores to reside in the ArrayList called scorelist. I assume this ArrayList called scorelist contains only one player's Scores. You have a couple of choices. Regardless, I think you need to add an addScore function to your class. You can either only keep track of the top five scores or keep track of all of them, but only later display the top five. You can sort them as you go ar sort them later. I would sort them as you go, so I'd always have a sorted ArrayList. When you insert a new Score into addScore, store it in its correctly sorted spot in the ArrrayList. To do this, I would use an InsertionSort.

Now, if you want to display the top five Scores of all the players total, so one score comes from Joe, two, from Bob, two from Sue, I think that that's a lot harder the way you have your classes set up, but certainly doable. Again, the way you have set up your highscore class leads me to believe that your highscore object comes from a group of scores of one individual player, not a mix of players.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Mar 2nd, 2008
0

Re: Help With Java

Thank You for all your help
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Helpwithjava is offline Offline
4 posts
since Feb 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: best Struts book
Next Thread in Java Forum Timeline: JUnit test case problem.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC