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