Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (579)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Java Game APIs & Engines / J2ME / Re: storing high scores?? on: 2010-04-21 17: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
2  Java Game APIs & Engines / J2ME / Re: storing high scores?? on: 2010-04-15 02:56:25
Thanks for the quick reply!! I'll give it a go in the morning. Cheers
3  Java Game APIs & Engines / J2ME / storing high scores?? on: 2010-04-14 22: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
4  Java Game APIs & Engines / J2ME / Re: Another Newbie Game Project on: 2010-04-12 21:24:28
Thats no big deal. As long as i can show my tutor it running in Netbeans IDE he'll be fine about it. I just thought it would be a nice thing to show friends once i had created it. Cheers for the input though. Smiley

RSYR
5  Java Game APIs & Engines / J2ME / Another Newbie Game Project on: 2010-04-12 20:53:45
Hi guys sorry to bother everyone but i need a little help. I've been delaying this for a while as Java is not one of my strong points but i have to make a small Java game (J2ME for my University project. It would be nice to have it working on my HTC Hero but thats not a neccessity =D

I was thinking of either creating something like:
  • Zelda Style RPG
  • Pacman Style Arcade Game

The games don't need to be anything special just a couple of sprites and some sort of scoring system.

I'm very new to Java and Game development in general and the learning materials i've been provided with by my tutor aren't very good for me as i prefer to actually get stuck in and play around with code rather than read copious amounts and not actually implement anything till the last minute.

I essentially just want to know what you would suggest me to take on as a project as i only have a short timespan (3 weeks) in which to complete the game. And any other resources that you personally use or would suggest for someone like me.

On a side note i've been thinking of grabbing some open source code and testing it out so any links to anything like that would be good. In the meantime i'll troll the forum and google for anything else that can help me out.

Thanks, I know i'm asking for a lot but would really appreciate it.

RSYR
Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (38 views)
2014-04-15 18:08:23

BurntPizza (34 views)
2014-04-15 03:46:01

UprightPath (50 views)
2014-04-14 17:39:50

UprightPath (32 views)
2014-04-14 17:35:47

Porlus (49 views)
2014-04-14 15:48:38

tom_mai78101 (71 views)
2014-04-10 04:04:31

BurntPizza (130 views)
2014-04-08 23:06:04

tom_mai78101 (230 views)
2014-04-05 13:34:39

trollwarrior1 (193 views)
2014-04-04 12:06:45

CJLetsGame (201 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!