iPhone High Score Data File/Sort
Hello all.
I am working on an iPhone game which is nearly complete but I am having trouble with the following high score sort/write code. I am aiming to keep up to 10 scores, sorted from highest to lowest obviously.
The code creates a new data file with the current score added as first entry if there is no existing data file (and this works), or it creates a data file with the current score as first entry if a data file exists but is empty for some reason (and this works as well), or it adds the current score to a data file of existing scores if it is within the top ten or there are less than ten entries (this is where it gets odd).
If there is one existing score in the data file, the current score is added and sorted properly. If there are two scores in the data file, the application crashes BUT the data file shows the current score was correctly added and sorted to the existing scores. If there are three existing scores, the application crashes and the data file remains unchanged.
I have been over the logic many times and tried many different variations of the logic structure to no avail. I suspect it is something simple but I've been staring at it too long to see. Any ideas?
If there is a better way to display the code/formatting on the forum, please let me know. The code follows (score variable is brought in from another class but works properly in my tests).
Many thanks,
Jesse Widener
www.artandstructure.com
I am working on an iPhone game which is nearly complete but I am having trouble with the following high score sort/write code. I am aiming to keep up to 10 scores, sorted from highest to lowest obviously.
The code creates a new data file with the current score added as first entry if there is no existing data file (and this works), or it creates a data file with the current score as first entry if a data file exists but is empty for some reason (and this works as well), or it adds the current score to a data file of existing scores if it is within the top ten or there are less than ten entries (this is where it gets odd).
If there is one existing score in the data file, the current score is added and sorted properly. If there are two scores in the data file, the application crashes BUT the data file shows the current score was correctly added and sorted to the existing scores. If there are three existing scores, the application crashes and the data file remains unchanged.
I have been over the logic many times and tried many different variations of the logic structure to no avail. I suspect it is something simple but I've been staring at it too long to see. Any ideas?
If there is a better way to display the code/formatting on the forum, please let me know. The code follows (score variable is brought in from another class but works properly in my tests).
Many thanks,
Jesse Widener
www.artandstructure.com
Code:
int i, ii;
struct high_score_entry {
NSString *name;
int highScore;
};
struct high_score_entry structArray[10];
FILE *fin = fopen("highscore.dat", "rb");
if (fin != NULL) { //if the data file exists proceed here
for (i = 0; i < 10; i++) {
if (fscanf(fin, "%s %d\n", structArray[i].name, &structArray[i].highScore) != EOF) { //if data exists for this iteration proceed
ii = i; //ii will be the last entry of existing data
}
}
for (i = ii; i > -1; i--) { //will begin at last entry and work up the list of scores to sort
if (score > structArray[i].highScore) { //if current score is higher than recoded score, recorded score moves down 1 place
structArray[i + 1] = structArray[i];
structArray[i].name = (NSString *)"JESSE";
structArray[i].highScore = score;
if (i == ii && ii < 9) //if there are less than 10 entries we will add another for our new entry
ii = ii + 1;
}
else if (score < structArray[i].highScore && i == ii) { //if current score is less than last recorded score it becomes new last entry
structArray[i + 1].name = (NSString *)"JESSE";
structArray[i + 1].highScore = score;
if (ii < 9)
ii = ii + 1;
}
}
}
fclose(fin);
if (fin == NULL) { //if the data file does not exist prepare data for new file
ii = 0; //will be used to limit write iterations to this single new entry
structArray[0].name = (NSString *)"JESSE";
structArray[0].highScore = score;
}
FILE *fout;
fout = fopen("highscore.dat", "wb"); //should create/rewrite data file from scratch
for (i = 0; i <= ii; i++) {
fprintf(fout, "%s %d\n", structArray[i].name, structArray[i].highScore);
}
fclose(fout);
This is a bit of a stab without looking properly but since no one else has replied... is it possible you are overunning your array when you do :
since i is equal to the array length, so i + 1 would be over?
Code:
structArray[i + 1]monteboyd Wrote:This is a bit of a stab without looking properly but since no one else has replied... is it possible you are overunning your array when you do :
since i is equal to the array length, so i + 1 would be over?Code:
structArray[i + 1]
That is indeed one of the problems, though not the one stopping the show at this point. A larger issue was preventing me from even getting to enough entries to write past the array, so I hadn't gotten to that one yet.

A poster on Apple's forums has been very helpful in discovering the error(s), though I have moved on to an attempt with NSUserDefaults instead. The thread can be viewed here:
http://discussions.apple.com/thread.jspa...9
All the best,
Jesse Widener
www.artandstructure.com
Solved. See here:
http://discussions.apple.com/thread.jspa...ID=1996000
All the best,
Jesse Widener
www.artandstructure.com
http://discussions.apple.com/thread.jspa...ID=1996000
All the best,
Jesse Widener
www.artandstructure.com
Hello all.
I have consolidated and annotated the code from this method in an article on my web site here:
Working with High Scores article
When searching for a method to tackle high scores in the iPhone SDK I found no similar approach laid out, at least not in code. I think this is a unique solution, at least as far as spelling it out for people, in a compact and powerful form.
Let me know if anything ids unclear about the article.
All the best,
Jesse Widener
www.artandstructure.com
I have consolidated and annotated the code from this method in an article on my web site here:
Working with High Scores article
When searching for a method to tackle high scores in the iPhone SDK I found no similar approach laid out, at least not in code. I think this is a unique solution, at least as far as spelling it out for people, in a compact and powerful form.
Let me know if anything ids unclear about the article.
All the best,
Jesse Widener
www.artandstructure.com
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| iphone file issue | NelsonMandella | 10 | 4,602 |
Sep 9, 2009 02:53 PM Last Post: NelsonMandella |
|
| Complete High Score Solution | danielpovlsen | 0 | 1,592 |
Jun 10, 2009 01:40 AM Last Post: danielpovlsen |
|
| Any sort of filters for opengl? | kendric | 10 | 3,767 |
May 15, 2009 10:13 PM Last Post: AnotherJake |
|
| Error when trying to save data to file. | Biposh | 2 | 3,471 |
Apr 24, 2009 03:26 AM Last Post: Biposh |
|
| High Score Questions | OptimisticMonkey | 1 | 1,990 |
Apr 1, 2009 11:03 AM Last Post: Malarkey |
|

