[source] High Scores Class
Author |
Message |
[Gandalf]
|
Posted: 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.
Description: |
An easy to use high scores class. |
|
Download |
Filename: |
HighScoreClass.t |
Filesize: |
2.71 KB |
Downloaded: |
221 Time(s) |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Delos
|
Posted: Sat Mar 18, 2006 11:54 pm Post subject: (No 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.
|
|
|
|
|
|
GlobeTrotter
|
Posted: Sun Mar 19, 2006 12:21 am Post subject: (No 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
code: |
Public Function ReplaceInString(ByVal strInput As String, ByVal strFind As String, Optional ByVal strReplace As String = "")
Dim strResult As String
Dim lWhereStringFound As Long
Dim lLengthInput As Long
lLengthInput = Len(strInput)
If lLengthInput > 0 Then
lWhereStringFound = InStr(strInput, strFind)
If lWhereStringFound = 0 Then
strResult = strInput
Else
strResult = Mid(strInput, 1, lWhereStringFound - 1)
strResult = strResult & strReplace
strResult = strResult & Mid(strInput, lWhereStringFound + Len(strFind))
strResult = ReplaceInString(strResult, strFind, strReplace) 'Some recursion
End If
End If
ReplaceInString = strResult
End Function
print ReplaceInString("test_123", "_", " ")
|
|
|
|
|
|
|
[Gandalf]
|
Posted: Sun Mar 19, 2006 1:24 am Post subject: (No subject) |
|
|
Yep, here's my version:
code: | /***
* Recursive check and replace letters in a string function using index().
*/
fcn replaceLetters (letR, letN, words : string) : string
var i : int := index (words, letR)
if i ~= 0 then
result replaceLetters (letR, letN, words (1 .. i - 1) + letN + words (i + 1 .. *))
end if
result words
end replaceLetters
/***
* Recursive check and replace letters in a string function without index().
* Takes the first index on the string as a parameter (notably, 1).
*/
fcn replaceLetters2 (letR, letN, words : string, i : int) : string
if i <= length (words) & words (i) = letR then
result replaceLetters2 (letR, letN, words (1 .. i - 1) + letN + words (i + 1 .. *), i + 1)
elsif i <= length (words) then
result replaceLetters2 (letR, letN, words, i + 1)
end if
result words
end replaceLetters2 |
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.
|
|
|
|
|
|
[Gandalf]
|
Posted: Sun Mar 19, 2006 2:33 am Post subject: (No 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.
|
|
|
|
|
|
Cervantes
|
Posted: Sun Mar 19, 2006 9:20 am Post subject: (No subject) |
|
|
[Gandalf] wrote:
code: | /***
* Recursive check and replace letters in a string function using index().
*/
fcn replaceLetters (letR, letN, words : string) : string
var i : int := index (words, letR)
if i ~= 0 then
result replaceLetters (letR, letN, words (1 .. i - 1) + letN + words (i + 1 .. *))
end if
result words
end replaceLetters
/***
* Recursive check and replace letters in a string function without index().
* Takes the first index on the string as a parameter (notably, 1).
*/
fcn replaceLetters2 (letR, letN, words : string, i : int) : string
if i <= length (words) & words (i) = letR then
result replaceLetters2 (letR, letN, words (1 .. i - 1) + letN + words (i + 1 .. *), i + 1)
elsif i <= length (words) then
result replaceLetters2 (letR, letN, words, i + 1)
end if
result words
end replaceLetters2 |
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?
code: |
fcn replaceLetters2 (letR, letN, words : string, i : int) : string
if i <= length (words) then
if words (i) = letR then
result replaceLetters2 (letR, letN, words (1 .. i - 1) + letN + words (i + 1 .. *), i + 1)
else
result replaceLetters2 (letR, letN, words, i + 1)
end if
end if
result words
end replaceLetters2
|
|
|
|
|
|
|
Delos
|
Posted: Sun Mar 19, 2006 10:19 am Post subject: (No 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:
code: |
class MyClass
export main, cat
var cat : string := "Aww, so cute!"
proc main
put cat
end main
end MyClass
var test : ^ MyClass
new MyClass, test
put test -> cat
test -> main
test -> cat := "Not a dog" %**
test -> main
|
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
|
|
|
|
|
|
|
|