PDA

View Full Version : iPhone High Score Data File/Sort




ArtandStructure
May 3, 2009, 06:58 PM
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
Art and Structure (http://www.artandstructure.com)



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);



lee1210
May 3, 2009, 07:03 PM
Wrap in tags. As for the real problem, no ideas yet. =)

-Lee

ArtandStructure
May 3, 2009, 07:07 PM
I see there is a forum specifically for iPhone development. I misunderstood the "Mac programming" header which had a link topic for "Mac (or iPhone)" programming. If this thread could be moved to the iphone forum it would probably be more suited to it.


Many thanks,


Jesse Widener
Art and Structure (http://www.artandstructure.com)

ArtandStructure
May 3, 2009, 07:10 PM
Wrap in tags. As for the real problem, no ideas yet. =)

-Lee

Is there an easy "format code for reading" tag I could use to wrap whole thing? :) Or am I stuck with tagging each individual line?


Jesse Widener
Art and Structure (http://www.artandstructure.com)

lee1210
May 3, 2009, 07:12 PM
Is there an easy "format code for reading" tag I could use to wrap whole thing? :) Or am I stuck with tagging each individual line?


Jesse Widener
Art and Structure (http://www.artandstructure.com)

Sorry... i was trying to say the sort of tag to use, but my BB code-fu is weak. The tag looks like and .

-Lee

EDIT: Found it... [noparse]...

ArtandStructure
May 3, 2009, 08:04 PM
Sorry... i was trying to say the sort of tag to use, but my BB code-fu is weak. The tag looks like and .

-Lee

EDIT: Found it...

Thank you sir. [code] does the trick. Apparently now the code needs to be scrolled horizontally but at least the indentation is relatively correct (if not overzealous in its spacing). I'm not sure which display is preferable but I'll go with it unless I hear a good argument for or against one or the other.


Jesse Widener
Art and Structure (http://www.artandstructure.com)

ArtandStructure
May 4, 2009, 02:12 AM
As far as I can tell by commenting out different portions of the code, the problem appears to be somewhere in here:


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
}
}
}

...but it baffles me that this works with one structure in the data file, crashes with two structures in the data file but correctly processes/sorts them and writes the file properly, and crashes with three structures in the data file without doing any additional work.


Jesse Widener
Art and Structure (http://www.artandstructure.com)

ArtandStructure
May 12, 2009, 12:45 AM
Solved. See here:

http://discussions.apple.com/thread.jspa?threadID=1996000


All the best,

Jesse Widener
Art and Structure (http://www.artandstructure.com)

ArtandStructure
Jul 4, 2009, 03:03 PM
Hello all.


I have consolidated and annotated the code from this method in an article on my web site, Working with High Scores (http://www.artandstructure.com/softwaredev/hscores.php). Like the photography section of my site I hope to add articles periodically.

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 is unclear about the article.


All the best,


Jesse Widener
Art and Structure (http://www.artandstructure.com)