Results 1 to 9 of 9
  1. #1
    Implode is offline Member
    Join Date
    Aug 2009
    Posts
    50
    Rep Power
    0

    Default Storing high score and sorting the array

    A guessing game where the user must guess a number between 1 and 1000.

    The program must contain a class called Game, which has only one public method. The method must
    be called start(), and, when run it starts the game. The game continues until the user chooses to
    quit, either at the end of a game by answering no to the question or by typing 'quit' instead of a
    guess. After each game has been played, the program is to ask the user for a name and insert this
    together with the number of guesses into a high score list. When a game is started the program
    should print the entire high score list, which must be sorted with the least number of guesses first
    and the most last. Note, the list must be kept as long as the game-object is alive!

  2. #2
    Implode is offline Member
    Join Date
    Aug 2009
    Posts
    50
    Rep Power
    0

    Default

    Would I create a game object in the main class whenever the game is run, store the objects in an array, the sort the array and print it out?

    The thing I need help with is that I am not sure how the program creates a new object on its own when the game is run.

    Maybe there is another way, please help.

    Thanks.

  3. #3
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    Your teacher will probably create the Game object and call the start() method himself (whether automated or not), unless he asked you to create a driver program for your Game class.

  4. #4
    Implode is offline Member
    Join Date
    Aug 2009
    Posts
    50
    Rep Power
    0

    Default

    I thought I could write the Game class with a public method called start, then in the RunGame class, I would put Game.start in the main method. How would I store high scores? They must be sorted in terms of lowest amount of guesses and lowest game time. Would I use array to store the data, and sort it . what does it mean by "as long as the game object is alive"? It must only store the scores as long as the game is run, So I wouldnt need to store the results in a txt file??

    Please help.

    Thanks in advance

  5. #5
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    I think he's saying that you don't have to store the high scores in a file on the hdd, and use that to obtain the high scores from the last time the game was played. Just make sure that the object you use to hold the high scores doesn't go out of scope at the end of start() (or some other method). To do this you really just need to make sure that the object which holds the high score is a private member of your Game class.

    Mr. Beans

  6. #6
    Implode is offline Member
    Join Date
    Aug 2009
    Posts
    50
    Rep Power
    0

    Default

    Thanks.

    I thought I could create the game constructor

    Game(String playerName,int guesses,double timePlayed){

    }
    but how can I create set methods, e.g. setPlayerName(name inputed from scanner)

    as private, would I ask for player name in the start method, calculate guesses and time in the start method which is public, then in the runGame class I code Game.start .

    Is this a possible way to do it or am I going down the wrong path.

    Also, how can an Array be sorted depending on 2 values, time and guesses?

    Thanks a lot.

  7. #7
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    Why would you create a constructor which takes the amount of guesses as an argument? The amount of guesses would be determined by the Game class, and thus could not be accurately given to the game class before it's created.

    I suggest you create a private method to get the high score, then call it from the start() method when you need to get the high score.

    Here's some psuedo code
    Java Code:
    while (!quit)
    
    numberGuesses = guessNumber()
    
    // Map<Integer, String> map; is a private member variable
    map.put( numberGuesses, getHighScoreName() )
    That would be the basic layout of the start method I woudl create. Of course you would have to check for if the user quit, exceptions, etc.

  8. #8
    Implode is offline Member
    Join Date
    Aug 2009
    Posts
    50
    Rep Power
    0

    Default

    I having trouble thinking how I would reset the variables, once the players info is stored , and where or how would I store it?

    Also what exactly would the Game class hold then?

    The start method can be the only public method in the game class- and the game must start when it is run? shouldn't the main class do that?

    Sorry for all the questions, but you are really helping?

  9. #9
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    The main class will create the Game class then call the start() method? Just think of the start() method as your 'main' method.


    Why don't you try creating a sample program and see how far you can get.

Similar Threads

  1. Basic high score program
    By Implode in forum New To Java
    Replies: 5
    Last Post: 09-03-2009, 06:21 PM
  2. storing a string in an array
    By tiyani in forum New To Java
    Replies: 3
    Last Post: 08-12-2009, 08:25 PM
  3. storing strings into an array
    By anthonym2121 in forum New To Java
    Replies: 2
    Last Post: 04-04-2009, 08:32 AM
  4. Storing Array from HTTP Post
    By kskinner in forum New To Java
    Replies: 1
    Last Post: 09-08-2008, 07:00 AM
  5. show a high score from tetris
    By stessie in forum Java Applets
    Replies: 0
    Last Post: 03-19-2008, 04:00 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
  •