
-----------------------------------
[Gandalf]
Thu Mar 16, 2006 8:39 pm

[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. ;)

-----------------------------------
Delos
Sat Mar 18, 2006 11:54 pm


-----------------------------------
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
Sun Mar 19, 2006 12:21 am


-----------------------------------
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


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]
Sun Mar 19, 2006 1:24 am


-----------------------------------
Yep, here's my version:
/***
 * 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  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
