Results 1 to 6 of 6
  1. #1
    Thurizdan is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default 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:

    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;
    			}
    		}
    		
    	}
    }
    completionTime is a float which holds the player's time taken from the most recent game.

    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.

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    I'd go with a object oriented aproach here:
    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;
      }
    }
    Then you can do the sorting of HighScore objects using the compareTo method, and not having to worry about names while switching values.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default

    scoresTableEasy[i] = completionTime;
    scoresTableEasy[i] = scoresTableEasy[(i - 1)];
    The first line saves a value at [i] and then the second line replaces it with the value from [i-1] ???

  4. #4
    Thurizdan is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    The first line saves a value at [i] and then the second line replaces it with the value from [i-1] ???
    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.

  5. #5
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    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.

  6. #6
    Thurizdan is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default

    Thanks but I've managed to solve the problem now. I appreciate your help.

Similar Threads

  1. Storing high score and sorting the array
    By Implode in forum New To Java
    Replies: 8
    Last Post: 09-28-2009, 12:43 AM
  2. Basic high score program
    By Implode in forum New To Java
    Replies: 5
    Last Post: 09-03-2009, 05:21 PM
  3. Test score average
    By ryn21 in forum New To Java
    Replies: 11
    Last Post: 10-17-2008, 05:49 AM
  4. show a high score from tetris
    By stessie in forum Java Applets
    Replies: 0
    Last Post: 03-19-2008, 03:00 AM
  5. Klaverjas Score 0.2
    By levent in forum Java Software
    Replies: 0
    Last Post: 05-23-2007, 07:40 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
  •