Computer Science Canada [source] High Scores Class |
Author: | [Gandalf] [ Thu Mar 16, 2006 8:39 pm ] |
Post subject: | [source] High Scores Class |
Every good game needs high scores, so here is a quick and easy way to implement them. The class is set up so that there is always only one I/O stream open. It is a bit messy, but there should be no bugs. If there is a space in the player's name, it is replaced with an underscore, if the score file does not exist, a new one is created, etc. Also, if you tie a previous high score, yours will go just below it. The source file comes with the class and an example of how it could be used. Feel free to use and modify the code as you please, just don't claim it as your own. |
Author: | Delos [ Sat Mar 18, 2006 11:54 pm ] |
Post subject: | |
For consistency, why not reconvert underscores to spaces when translating back. Also, you need a function to return the contents of the high scores array, since not everyone would want to use put to output their data. Otherwise not too bad. I'm interested to see if you can recreate your spaces to underscores fcn using recursion instead. I'm almost positive it can be done. |
Author: | GlobeTrotter [ Sun Mar 19, 2006 12:21 am ] | ||
Post subject: | |||
It can be done with recursion, definately. Here's a function that I cooincidently wrote today for another project. Although it is in VB6, the code is still relevant
|
Author: | [Gandalf] [ Sun Mar 19, 2006 1:24 am ] | ||
Post subject: | |||
Yep, here's my version:
Delos, I will add the rest of your suggestions tomorrow, except for a function to return the high scores array since that would require me to create a PlayerData type for it to be meaningful. If you don't wish to use put to output the high scores, you can simple change the procedure directly (add Font.Draws and whatever you wish). *edit* Fixed code to work with any combination of letters to replace. |
Author: | [Gandalf] [ Sun Mar 19, 2006 2:33 am ] |
Post subject: | |
Updated. Some of the improvements made include: -You can now specify the amount of elements in the high scores in the initScoreFile procedure. -Underscores are gone, replaced with spaces, as per your original input. -Quick comments added for all functions. -Recursive replaceLetters function. |
Author: | Cervantes [ Sun Mar 19, 2006 9:20 am ] | ||||
Post subject: | |||||
[Gandalf] wrote:
I certainly hope your using the second one. The first one is far less efficient than a standard iteration through each character. For the second one, why must we run and compare the length function twice?
|
Author: | Delos [ Sun Mar 19, 2006 10:19 am ] | ||
Post subject: | |||
So you don't want to return an array eh? Then how about you return elements of said array? You have several options: - a couple of seperate fcns that will each return one of player_name and player_score (for a specified index) - a single fcn that returns PlayerData (again for the specified index) - a single fcn that returns a single string of player_name, delimiter, player_score (for a specified index) This will require a secondary fcn to return the upper-bound of the array in question. Why a seperate fcn when upper() could do this just as well? We want to keep the high scores array private, and Turing is absolutely attrocious and exporting variables in a 'read-only' fashion...(I forget the keyword right now, someone fill me in?) Edit: Did a quick background check, and found out what I as referring to:
Run that, and you'll throw an error at **. Turing tells you that 'cat' is not qualified to be changed, being in a read-only state. Sadly though, it's not! BTW: I know you know all of this, the comments were just for those who'll be reading through this thread trying to understand a little more about classes |