
-----------------------------------
chipanpriest
Mon Dec 05, 2011 7:43 pm

Saving highscore onto hard drive (I've looked at other &quot;help&quot; attemts but im still really confused)
-----------------------------------
What is it you are trying to achieve?
Hey guys. I'm making a game that involves a score and a highscore. I have the score and highscore when you play the game. I'm still confused about how to save your highscore to the user's hard drive and be able to recover it again when the user starts the program again.


What is the problem you are having?
I'm having troubles understanding all this "open :, get :, put :, etc"


Describe what you have tried to solve this problem
I've searched the web and seen people try to explain this but I'm a little bit new at Turing and I don't understand all the advanced things in Turing so if anyone could explain this simply I would appreciate it.

Please specify what version of Turing you are using


-----------------------------------
Dreadnought
Mon Dec 05, 2011 7:54 pm

Re: Saving highscore onto hard drive (I've looked at other &quot;help&quot; attemts but im still really confused)
-----------------------------------
Perhaps this tutorial will help with your problem. 

http://compsci.ca/v3/viewtopic.php?t=12972

Hope it helps.

-----------------------------------
mirhagk
Mon Dec 05, 2011 7:54 pm

RE:Saving highscore onto hard drive (I\'ve looked at other &quot;help&quot; attemts but im still really confused)
-----------------------------------
So the first thing you need to do is open up the file, you use open to do that such as the following


var stream:int
open: stream, "highscores.txt", get, put


That will give you an integer value that corresponds to an internal value that Turing uses to know which file you're talking about. You use this whenever you do anything with this file. The get and put just go on the end, and you can just use one or the other if your just getting or putting, It;s just to tell the file how you want to use it. So basically all you need to do know is put or get from  the file pretty much just like you would window only you use : stream, after the put or get like the following


var msg:string
put: stream, "Hello there"
get: stream, msg


-----------------------------------
programmer1337
Mon Dec 05, 2011 8:09 pm

Re: Saving highscore onto hard drive (I've looked at other &quot;help&quot; attemts but im still really confused)
-----------------------------------
ill give you an example of a top 3 highscore thingy i made[code]var hScore1, hScore2, hScore3 : int
var enterYN : string
put "Please enter y / n if you want to view highscores"
get enterYN
if enterYN = "y" then
  
open : scoresFile, "High Scores.txt", get, mod
get : scoresFile, hScore1
    get : scoresFile, hScore2
    get : scoresFile, hScore3
    close : scoresFile
if accumulator < hScore1 then
    open : scoresFile, "High Scores.txt", put, mod
    put : scoresFile, accumulator
    put : scoresFile, hScore1
    put : scoresFile, hScore2
elsif accumulator < hScore2 and accumulator > hScore1 then
    open : scoresFile, "High Scores.txt", put, mod
    put : scoresFile, hScore1
    put : scoresFile, accumulator
    put : scoresFile, hScore2
elsif accumulator < hScore3 and accumulator > hScore2 and accumulator > hScore1 then
    open : scoresFile, "High Scores.txt", put, mod
    put : scoresFile, hScore1
    put : scoresFile, hScore2
    put : scoresFile, accumulator
end if

end if
put "1. ", hScore1
put "2. ",hScore2
put "3. ",hScore3[/code]

-----------------------------------
ttm
Mon Dec 05, 2011 8:10 pm

Re: Saving highscore onto hard drive (I've looked at other &quot;help&quot; attemts but im still really confused)
-----------------------------------
Hi there;

I wrote a super handy highscore module which I use in all my games.
You might want to use it as an example or try to figure out how it works.

[code]
/*
 * highscore
 *
 * A module that keeps track of your highscores
 *     even after you close your program
 *
 * -It automatically keeps only the top 10
 * -To add an entry, do highscore.add ("name", 999)
 * -To get the entry at location n, (from 1 to 10), do
 *     highscore.getname (n)
 * -To get the score at location n, (from 1 to 10), do
 *     highscore.getscore (n) 
 * -The highscores are recorded in file "highscores.dat"
 * 
  Copyright (C) 2011 by Tony Zou

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 */

module highscore
    export add, getname, getscore
    type _hsentry :
        record
            name : string
            score : int
        end record
    var _highscores : array 1 .. 10 of _hsentry
    var _highscorefs : int
    open : _highscorefs, "highscores.dat", read, write, mod, seek
    if (eof (_highscorefs)) then
        for i : 1 .. 10
            _highscores (i).name := "-- Empty --"
            _highscores (i).score := 0
        end for
        write : _highscorefs, _highscores
    else
        read : _highscorefs, _highscores
    end if
    proc _rebalance
        var curscore : _hsentry
        var k : int
        var tmp : _hsentry
        for i : 2 .. 10
            curscore := _highscores (i)
            k := i
            loop
                k -= 1
                exit when k = 0 | _highscores (k).score > curscore.score
                _highscores (k + 1) := _highscores (k)
            end loop
            _highscores (k + 1) := curscore
        end for
    end _rebalance
    proc add (name : string, score : int)
        if (_highscores(10).score > score) then
            return
        end if
        _highscores (10).name := name
        _highscores (10).score := score
        _rebalance
        seek :_highscorefs, 0
        write : _highscorefs, _highscores
    end add
    fcn getname (ind : int) : string
        result _highscores (ind).name
    end getname
    fcn getscore (ind : int) : int
        result _highscores (ind).score
    end getscore
end highscore
[/code]

An example of how to use it would be

[code]
loop
    var name : string
    var score : int

    put "What is your name? " ..
    get name : *
    put "What is your score? " ..
    get score


    highscore.add (name, score)


    put ""
    put "---- Highscores ----"

    for i : 1 .. 10
        put highscore.getname (i), "           ", highscore.getscore (i)
    end for
    
    put ""
end loop
[/code]

This uses the file input and output methods mentioned previously, but the only thing that might be a bit complicated is the _rebalance procedure, which sorts the highscore list using insertation sort. You can read about that here: http://en.wikipedia.org/wiki/Insertion_sort.

-----------------------------------
ttm
Mon Dec 05, 2011 8:11 pm

Re: Saving highscore onto hard drive (I've looked at other &quot;help&quot; attemts but im still really confused)
-----------------------------------
Oops just noticed programmer1337's post. Sorry mybad.

-----------------------------------
programmer1337
Mon Dec 05, 2011 8:12 pm

Re: Saving highscore onto hard drive (I've looked at other &quot;help&quot; attemts but im still really confused)
-----------------------------------
this is much simpler then ^ and you would prolly get it
[code]var hScore1, hScore2, hScore3 : int 
var enterYN : string 
put "Please enter y / n if you want to view highscores" 
get enterYN 
if enterYN = "y" then 
  
open : scoresFile, "High Scores.txt", get, mod 
get : scoresFile, hScore1 
    get : scoresFile, hScore2 
    get : scoresFile, hScore3 
    close : scoresFile 
if accumulator < hScore1 then 
    open : scoresFile, "High Scores.txt", put, mod 
    put : scoresFile, accumulator 
    put : scoresFile, hScore1 
    put : scoresFile, hScore2 
elsif accumulator < hScore2 and accumulator > hScore1 then 
    open : scoresFile, "High Scores.txt", put, mod 
    put : scoresFile, hScore1 
    put : scoresFile, accumulator 
    put : scoresFile, hScore2 
elsif accumulator < hScore3 and accumulator > hScore2 and accumulator > hScore1 then 
    open : scoresFile, "High Scores.txt", put, mod 
    put : scoresFile, hScore1 
    put : scoresFile, hScore2 
    put : scoresFile, accumulator 
end if 

end if 
put "1. ", hScore1 
put "2. ",hScore2 
put "3. ",hScore3[/code]

-----------------------------------
chipanpriest
Mon Dec 05, 2011 8:48 pm

RE:Saving highscore onto hard drive (I\'ve looked at other &quot;help&quot; attemts but im still really confused)
-----------------------------------
haha thanks guys i got it to wrk after the mirhagk person replied but thanks anyways ;D
