#97114 - chatterbug89 - Tue Aug 08, 2006 1:38 am
I've been at this for a couple days and am sort of stumped. I have a system set up where the high scores are saved in a text file, and when read, the high scores are loaded into 2 arrays, AdventureScores and AdventureNames (there are actually 4...but, that doesn't really matter for my question). The scores are stored as text int he AdventureScores arrays, 6 digits for every score. So every 6 new digits, there is a new score. For the AdventureNames array, there are 3 letters to a name, so every 3 spaces, there is a new high score name.
Creating this file, reading it, etc. I have all working great. But, shifting the arrays to add new scores is causing me problems. One more thing before I show some code. 0 is the higest score. The higher the number, the lower the score place. At the momment, I have it working so that if you get a new higest score (a score greater than what is at 0), everything shifts down fine, clearing the lowest score, and making room for writing the new high score in the particlar place.
The following code is actually just part of a function of mine, but, I should have it so everything that concerns this question is included with it and enough to actually run it is included.
EDIT2: By the way, there are 10 high scores. That's why the names array is 30 (3 letters * 10 entries) and the score array is 60 ( 6 digits * 10 entries). ;-)
I left out the variable declartions (remember, this is a part of a game i'm working on)...but, I added extra comments for this example than in the orginal so everyone knew what I was doing. Anyways, as I said before, it works if the new high score is higher than AdventureScores[0]...but, if it's lower or higher than any of the other scores....weird things happen. The whole score array is messed up, and, some other score arrays for a differnt mode of game play are affected when they are never intentially modified or called (somehow, something like array[20] is being edited where array only goes up to 10...so, it affects the next array declared after it).
Any help on how to get this working would be greatly appreciated. Also, feel free to use my ideas for score shifting :).
Finally, this was the preliminary code I made to test array shifting...and it works. The difffernce between this and the above, is each element is a differnt number/score..it shoudlen't be any differnt than above in the general aspect, but ...for soem reason it just wont work. In this example, place is the place of 0-9 in the array that you want to stick your new score in. Everything is shifted from the place to make room for a new varaible, dumping the variable at the end of the array.
Anyways, thanks a lot :)[/code]
EDIT: By the way, I have a feeling i'm just doing somethign realy realy stupid :s The problem lies with taking the place variable and figuring out where to start shifting from.
Creating this file, reading it, etc. I have all working great. But, shifting the arrays to add new scores is causing me problems. One more thing before I show some code. 0 is the higest score. The higher the number, the lower the score place. At the momment, I have it working so that if you get a new higest score (a score greater than what is at 0), everything shifts down fine, clearing the lowest score, and making room for writing the new high score in the particlar place.
The following code is actually just part of a function of mine, but, I should have it so everything that concerns this question is included with it and enough to actually run it is included.
EDIT2: By the way, there are 10 high scores. That's why the names array is 30 (3 letters * 10 entries) and the score array is 60 ( 6 digits * 10 entries). ;-)
Code: |
/*****Check if we have made a high score in adventure mode. If so, save the new high scores******/ //This following code checks if we made a new high score and records the place. //Cscores was converted to an array of intigers from AdventureScores. //This code *should* be fine. //This first conditional statement see's if our current score is higher than the lowest high score in a list of 10 high scores. if ( score > Cscores[9] ){ // Our score is bigger than our highest score, so we have a high score! if (score > Cscores[0] ){ //We have a new TOP score! Cscores[0] = score; scoreplace = 0; } else{ //Begin the loop to determine where we are. scoreplace = -1; // Initially we wont want to kill the loop. for ( i = 9; (i >= 0) && (scoreplace == -1); ++i ){ if (score < Cscores[i]){ //We have found a high score higher than our new high score, so our new high score must go below it. Cscores[i+1] = score; scoreplace = (i+1); // Kill the Loop & set the place we scored. } } } //Move all the scores down one level from the new high level place //This is the part where i'm having the problems. //First we begin a for loop that goes 3 times. Since there are 3 spaces for a name, we have to do a shift tot he write three times. for (i = 0; i < 3; ++i){ // If it isn't our first time, we need to copy the temp array to the orignal array, since we are shifting it again. if ( i != 0 ){ for ( p = 0; p < 30; ++p) AdventureNames[p] = AdventureNamesTemp[p]; } //This is suspose to determine where we should start shifting from....this part is where i'm unsure. actualscoreplace = scoreplace * 3; if (actualscoreplace == 0) actualscoreplace = 1; //Right now it is a loop which shifts everything to the right. // It doesn't matter what's in AdventureNamesTemp[0] (this varies depending ont he place, but if we had a higest top score it woudl be 0), since it will just be ovewritten //with the new high score later on. for ( q = actualscoreplace; q < 30; ++q) AdventureNamesTemp[q] = AdventureNames[q-1]; // If a shift to the right diden't occur at the very beggining, we need to copy the old values to the unchangign parts of the arrays so they arn't filled with junk. for ( q = 0; q < actualscoreplace; ++q) AdventureNamesTemp[q] = AdventureNames[q]; } //This part is identical to the first part, except it is for the scores, which are 6 spaces instead of 3. for (i = 0; i < 6; ++i){ if ( i != 0 ){ for ( p = 0; p < 60; ++p) AdventureScores[p] = AdventureScoresTemp[p]; } actualscoreplace = scoreplace * 6; if (actualscoreplace == 0) actualscoreplace = 1; for ( q = 1; q < 60; ++q) AdventureScoresTemp[q] = AdventureScores[q-1]; for ( q = 0; q < 1; ++q ) AdventureScoresTemp[q] = AdventureScores[q]; } //This stuff doesn't realy pertain to my question..you can safely ignore it. But, you can look at it to kind of see what's happenign witht he data after it's parsed. convertscoresstring(AdventureScoresTemp, scoreplace); //Write out new score array. writeintials(AdventureNamesTemp, scoreplace); // Get the user intials and write it to the array. //Write the New High Scores to the High Scores File ResetHighScores(AdventureScoresTemp, ClassicScores, AdventureNamesTemp, ClassicNames); } |
I left out the variable declartions (remember, this is a part of a game i'm working on)...but, I added extra comments for this example than in the orginal so everyone knew what I was doing. Anyways, as I said before, it works if the new high score is higher than AdventureScores[0]...but, if it's lower or higher than any of the other scores....weird things happen. The whole score array is messed up, and, some other score arrays for a differnt mode of game play are affected when they are never intentially modified or called (somehow, something like array[20] is being edited where array only goes up to 10...so, it affects the next array declared after it).
Any help on how to get this working would be greatly appreciated. Also, feel free to use my ideas for score shifting :).
Finally, this was the preliminary code I made to test array shifting...and it works. The difffernce between this and the above, is each element is a differnt number/score..it shoudlen't be any differnt than above in the general aspect, but ...for soem reason it just wont work. In this example, place is the place of 0-9 in the array that you want to stick your new score in. Everything is shifted from the place to make room for a new varaible, dumping the variable at the end of the array.
Code: |
#include <stdio.h> main() { int i; int place = 9; int teststring[10], transferstring[10]; for ( i = 0; i < 10; ++i) teststring[i] = i; for ( i = 0; i < 10; ++i) printf("%d ",teststring[i]); for (i = place+1; i < 10; ++i) transferstring[i] = teststring[i-1]; for ( i = 0; i < place+1; ++i) transferstring[i] = teststring[i]; transferstring[place] = 9; printf("\n"); for ( i = 0; i < 10; ++i) printf("%d ",transferstring[i]); while(1) ; return 0; } |
Anyways, thanks a lot :)[/code]
EDIT: By the way, I have a feeling i'm just doing somethign realy realy stupid :s The problem lies with taking the place variable and figuring out where to start shifting from.