Results 1 to 6 of 6
Thread: Basic high score table
- 05-18-2010, 03:29 PM #1
Member
- Join Date
- May 2010
- Posts
- 3
- Rep Power
- 0
Basic high score table
Hey guys, I'm trying to recreate the classic game Minesweeper in Java for an assignment. At the moment I'm not planning on making a GUI, I just want to output into the command prompt. The game plays through perfectly so far; at the moment I'm up to creating a high score table, displaying a list of the five best completion times along with the names of the players who achieved them. I was able to get the times to display correctly using a basic array sort but I couldn't figure out how to get the names to change positions along with the scores.
Thus I am attempting a different method of sorting the high scores but so far, it isn't outputting correctly. Here is the method so far:
completionTime is a float which holds the player's time taken from the most recent game.Java Code:public void addEasyHighScore(String playerName) { for (int i = 4; i >= 0; i--) { if (scoresTableEasy[i] > completionTime) { if (i > 0) { scoresTableEasy[i] = completionTime; scoresTableEasy[i] = scoresTableEasy[(i - 1)]; } if (i == 0) { scoresTableEasy[i] = completionTime; } } } }
playerName is input just before calling the Method.
scoresTableEasy is a float array which holds the score times.
I also have an String array namesTableEasy instantiated to hold the scorers' names.
It puts the first two scores in correctly but it fails to correctly insert scores that are in between two existing ones.
Sooooo can anyone give me some hints as to where I'm going wrong or what alternative methods I could use to complete this task? I'm not after full solutions, just a nudge in the right direction.
- 05-18-2010, 03:54 PM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
I'd go with a object oriented aproach here:
Then you can do the sorting of HighScore objects using the compareTo method, and not having to worry about names while switching values.Java Code:public class HighScore implements Comparable<HighScore> { private String name; private float completionTime; public HighScore(String n, float c) { name = n; completionTime = c; } public int compareTo(HighScore h) { if(completionTime < h.completionTime) return -1; if(completionTime > h.completionTime) return 1; return 0; } public String toString() { return name+" time taken: "+completionTime; } }Ever seen a dog chase its tail? Now that's an infinite loop.
- 05-18-2010, 05:04 PM #3
The first line saves a value at [i] and then the second line replaces it with the value from [i-1] ???scoresTableEasy[i] = completionTime;
scoresTableEasy[i] = scoresTableEasy[(i - 1)];
- 05-19-2010, 10:38 AM #4
Member
- Join Date
- May 2010
- Posts
- 3
- Rep Power
- 0
I realise that was kind of dopey now that I look at it.
@m00nchile: That looks like it might be a good approach. So I should create an array of High Score objects and then compareTo each one with a for loop whenever a new high score is added?
EDIT: I forgot to mention that I need to do this all within the one class, can I just use those methods without making a new class?Last edited by Thurizdan; 05-19-2010 at 12:46 PM.
- 05-19-2010, 07:03 PM #5
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
You can always make HighScore an inner class, if you must have your whole program in one class:
Java Code:public class Game { private class HighScore { //constructors, methods, etc. } //constructors, methods, etc. }Ever seen a dog chase its tail? Now that's an infinite loop.
- 05-19-2010, 07:10 PM #6
Member
- Join Date
- May 2010
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Storing high score and sorting the array
By Implode in forum New To JavaReplies: 8Last Post: 09-28-2009, 12:43 AM -
Basic high score program
By Implode in forum New To JavaReplies: 5Last Post: 09-03-2009, 05:21 PM -
Test score average
By ryn21 in forum New To JavaReplies: 11Last Post: 10-17-2008, 05:49 AM -
show a high score from tetris
By stessie in forum Java AppletsReplies: 0Last Post: 03-19-2008, 03:00 AM -
Klaverjas Score 0.2
By levent in forum Java SoftwareReplies: 0Last Post: 05-23-2007, 07:40 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks