Score Board


runescape=crap

Still Fresh
Joined
May 11, 2005
Messages
61
I was planning on trying to put a score board in my game and was wondering if anyone can suggest the best way to do it.
 
If you want to have a savable highscore table, use arrays as variables, which you can save easily to a file.
You can sort the array with bubble sort, or any other sorting algorithm you like :)

Shall I post an example for sorting and saving?
 
And you don't even need bubble sorting in this case, you can just find the right place for a new high score, place it and shift the lower values one place down kicking the lowest out of the table.
 
I`ll post the bubble sort anyways, easier for me :D
This should work to save a highscore. Which vars you have to change for your game, you have to find out yourself :D also how to display the highscore ingame.

Code:
 global
  array[10]; //an array, the number tells how big the array is.
  pointscounter; //the score you achieve during gameplay

...

process highscore();
 private  
  n; //variables you need for the sorting
  i; // ~
  j; // ~
  t; // ~

begin
   load("config.cfg",array); //load values from the file config.cfg into the array (one value for each number)
   n=10; //How big your highscore is (10 entries)
   i=1; //leave that as it is
   j=i+1; //leave that as it is
  
  if(array[10]<pointscounter)  //if score is higher then number 10 of the highscore
    array[10]=pointscounter; // then your score=place ten in highscore, and start sorting   
    while(i<=n); //here starts the highscore sorting
      while(j<=n); 
        if(array[i]<array[j]) 
          t=array[j];                  
          array[j]=array[i];           
          array[i]=t;             
         end;    
        j++;                            
       end;  
       i++;                               
       j=i+1;
     end; //here does the sorting end  
   end;

  save("config.cfg",array); //save the highscore in config.cfg afterwards
 end;

But try to make WhiteFalcons idea working, it is much faster, BubbleSort is one of the slowest sorting algorithms (it compares every entry in the array with every other, 10 entries = 100 comparisons, 100 entries = 10000 comparisons, etc.).
But if you have just a highscore up to 10 entries, you won`t have to wait that long...
 
If you try it like that, yes, because the variable pointscounter needs to have a value greater zero.

The initial value of array[10] is zero, so is pointscounter and that makes "if(array[10]<pointscounter)" false, and the whole program is directly going to "save...".
 
no i linked changed points counter to my original counter plyscore so should have a value higher that zero.
 
For a highscore table sorting is kind of awkward. You'd have to expand the table by one, add the entry in the new position, sort the table and then remove the last entry again. Doable, but if you keep the table sorted from the start there is no need to sort. If you'd do this(all typing in the quickreply):

Code:
Global
  // 10 entries(Fenix declares both 0 and 9 for some reason)
  scoreTable[9] = 1000,900,800,700,600,500,400,300,200,100; 

...

/**
 * Loads highscore table from file into the array where it belongs
 */
process loadScores()

begin
  if(file_exists("scores.dat"))
    load("scores.dat",scoreTable);
  end
end

/**
 * Saves highscore table to a file
 */
process saveScores()

begin
  save("scores.dat",scoreTable);
end

/**
 * Tries to add a score to the table, returns the new scores' position in the list(starting with 1) if succeeded, -1 if not added.
 */
process addScore(myScore)

private
i,j;

begin
  //Goes through the table until it finds a score below the score to be added
  for(i = 0; i < 10; i++)
    if(scoreTable[i] < myScore)
      break;
    end
  end
  
  //Moves all scores from the detected one downwards one step down
  for(j = 8; j >= i; j--)
    scoreTable[j+1] = scoreTable[j];
  end
  
  //And adds the new score on the open position.
  if(i <= 9)
    scoreTable[i] = myScore;
    return(i+1);
  else
    return(-1);
  end

end

This gives somewhat less 'guarantees', if it gets out of order this won't fix it where sorting would, but if only these functions are going to touch the array all is fine.
 
Look at my example Goity, you see the array?

That is a thing that can story certain values.
array[1] = 10
array[2] = 23 and so on.

Now use the load and save commands like in my example, and there you saved your variables.
 
Back
Top