Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  storing high scores??  (Read 7508 times)
0 Members and 1 Guest are viewing this topic.
Offline RSYR

Junior Newbie





« Posted 2010-04-14 20:58:17 »

Hey i've got a pacman type game and i'm wondering if i can store the highest scores in the phones memory... i've searched around and found the RMS functions but it seems like an awful lot of code just to store say the top 5 or 10 highest scores in the phone? Is there a simpler way of doing this or will i have to study RMS?

Thanks

RSYR
Offline SimonH
« Reply #1 - Posted 2010-04-15 00:37:24 »

will i have to study RMS?
Yup!
It's not that hard though - if I recall there's a few easy examples to google for.

People make games and games make people
Offline RSYR

Junior Newbie





« Reply #2 - Posted 2010-04-15 00:56:25 »

Thanks for the quick reply!! I'll give it a go in the morning. Cheers
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline RSYR

Junior Newbie





« Reply #3 - Posted 2010-04-21 15:31:52 »

okay so i've got some code and its kind of working but i'm having trouble...
I want to retrieve my enumerated scores from the database and print them to a string...
but this only seems to be setting the latest score. It does work in the output bar as println but not when i put it to string.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
package game;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.RecordComparator;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;

public class HighScore extends MIDlet implements CommandListener
{
   TextField name, score;
   Command add, show, exit, home;
    private String HS;
    private Form f;


   protected void destroyApp(boolean arg0) throws MIDletStateChangeException
   {
      // TODO Auto-generated method stub

   }

   protected void pauseApp() {
      // TODO Auto-generated method stub

   }

   protected void startApp() throws MIDletStateChangeException
   {
      f = new Form("High scores");

      f.append(name = new TextField("Name", "", 255, TextField.ANY));
      f.append(score = new TextField("Score", "", 10, TextField.NUMERIC));

      f.addCommand(add = new Command("Add new score", Command.OK, 1));
      f.addCommand(show = new Command("Retrieve scores", Command.OK, 2));
      f.addCommand(exit = new Command("Exit", Command.EXIT, 1));

      f.setCommandListener(this);

      Display.getDisplay(this).setCurrent(f);
   }
   public void commandAction(Command c, Displayable d)
   {      
            if(c == home)
      {
      Display.getDisplay(this).setCurrent(f);
      }
      if(c == exit)
      {
         notifyDestroyed();
      }
      else if(c == add)
      {
         try
         {
            addNewScore(name.getString(), score.getString());

            showConfirm("Score correctly added");
         }
         catch(Exception e)
         {
            showError(e.toString());
         }
      }
      else if(c == show)
      {
         try
         {
            String[][] scores = retrieveHighScores();

            System.out.println("HIGH SCORES");

            for(int i = 0; i < scores.length; i++)
            {
               System.out.println("SCORE: " + scores[i][0] + " - " + scores[i][1]);
                                        HS = ("SCORE: " + scores[i][0] + " - " + scores[i][1]);
                                }
            showConfirm("Scores correctly retrieved");
         }
         catch(Exception e)
         {
            showError(e.toString());
         }
      }
   }
   void showConfirm(String text)
   {
        Display.getDisplay(this).setCurrent(new Alert("Score Added", text, null, AlertType.CONFIRMATION));
        Form highscore = new Form("Your Score");
        highscore.append(HS);

   highscore.addCommand(home = new Command("Home", Command.OK, 1));
        highscore.addCommand(exit = new Command("Exit", Command.EXIT, 1));

        highscore.setCommandListener(this);
        Display.getDisplay(this).setCurrent(highscore);
   
   }
   void showError(String error)
   {
      Display.getDisplay(this).setCurrent(new Alert("Error", error, null, AlertType.ERROR));
   }

   void addNewScore(String name, String score) throws Exception
   {
//       We open the recordstore
     RecordStore highscore = RecordStore.openRecordStore("High Score", true);

//       To add a new HiScore we use a quick string comma-separated
     String str = new String(name + "," + score);
      byte [] strb = str.getBytes();
      highscore.addRecord(strb, 0, strb.length);

      highscore.closeRecordStore();
   }
   String[][] retrieveHighScores() throws Exception
   {
      String[][] scores;

      RecordStore highscore = RecordStore.openRecordStore("High Score", true);

//       To read all hiscores saved on the RMS
     RecordEnumeration enumerator = highscore.enumerateRecords(null, new ScoresComparator(), false);
      int id, index, separator;
      byte[] record;
      String str;

      index = 0;

      scores = new String[enumerator.numRecords()][2];

      while (enumerator.hasNextElement( )) {
           id = enumerator.nextRecordId( );
           record = highscore.getRecord(id);
           str = new String(record);
           // Operate with str to extract name and points

           separator = str.indexOf(',');

           scores[index][0] = str.substring(0, separator);
           scores[index][1] = str.substring(separator + 1);

           index++;
      }
      highscore.closeRecordStore();

      return scores;
   }


   class ScoresComparator implements RecordComparator
   {
      public int compare(byte[] arg0, byte[] arg1)
      {
         int score0 = 0, score1 = 0;

         for(int i = 0; i < arg0.length; i++)
         {
            if(arg0[i] == ',')
            {
               score0 = Integer.parseInt(new String(arg0, i + 1, arg0.length - i - 1));
            }
         }
         for(int i = 0; i < arg1.length; i++)
         {
            if(arg1[i] == ',')
            {
               score1 = Integer.parseInt(new String(arg1, i + 1, arg1.length - i - 1));
            }
         }

         if(score0 < score1)
            return 1;
         else if(score0 == score1)
            return 0;
         else
            return -1;
      }
   }
}


Any Help Appreciated Cheers
Offline Karmington

Senior Member


Medals: 1
Projects: 1


Co-op Freak


« Reply #4 - Posted 2010-04-21 16:41:34 »

http://www.j2meforums.com/forum/

will almost certainly have example code and tips.
Used to be there all day when I used to make J2ME stuff. Good site.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CopyableCougar4 (24 views)
2014-08-22 19:31:30

atombrot (34 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (28 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (33 views)
2014-08-08 02:01:56

Norakomi (42 views)
2014-08-06 19:49:38
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!