Computer Science Canada

oh man.. High Scores eh?

Author:  santabruzer [ Thu Jan 08, 2004 9:37 pm ]
Post subject:  oh man.. High Scores eh?

well.. i'm almost done my ISU.. now all i need is a high scores that will write to a file.. now... i'm not sure what will be the most effiecient way to do this.. through arrays and a file is what i thought.. but the problem is finding where is the username, and where is the score.. so i kinda need a bit of help on the file aspect of it.. so retrieving, writing...

Author:  DanShadow [ Fri Jan 09, 2004 3:21 pm ]
Post subject: 

Its actually really easy, use this:
code:

%Variables
var highscore, hs_temp : array 1 .. 5 of int
var highscore_name, hsn_temp : array 1 .. 5 of string
var datafile : int := 0

%Makes the Variables equal to something
highscore_name (1) := "Dan"
highscore (1) := 9999
highscore_name (2) := "Jacob"
highscore (2) := 389
highscore_name (3) := "Jimmy"
highscore (3) := 247
highscore_name (4) := "Josh"
highscore (4) := 226
highscore_name (5) := "Joe"
highscore (5) := 54

%Saves the High Scores
open : datafile, "High Scores", write
for i : 1 .. 5
    write : datafile, highscore (i)
end for
for i : 1 .. 5
    write : datafile, highscore_name (i)
end for
close : datafile

%Loads the High Scores
open : datafile, "High Scores", read
for i : 1 .. 5
    read : datafile, highscore (i)
    hs_temp (i) := highscore (i)
end for
for i : 1 .. 5
    read : datafile, highscore (i)
    hsn_temp (i) := highscore_name (i)
end for
close : datafile
for i : 1 .. 5
    highscore (i) := hs_temp (i)
    highscore_name (i) := hsn_temp (i)
end for

%Displays High Score
for i : 1 .. 5
    put highscore_name (i), ": ", highscore (i)
end for

Author:  santabruzer [ Fri Jan 09, 2004 8:40 pm ]
Post subject: 

yea.. i know about that... or i figure that out from the textbook today at school... thanx anyways.. and dude.. how would you go upon reading the spaces as well.. let's say the text file is in the format:

Name (can be anything with spaces)
Score (here the score is)

basically i want to fill all let's say unknown number of words into the Name, and the Score to be read...

Author:  McKenzie [ Fri Jan 09, 2004 8:49 pm ]
Post subject: 

Good call on the format. That is probably the easiest way to do it. I would suggest using text files rather than binary files. Would look something like:
code:

%Load the current high scores
var names :array 1.. 10 of string
var scores :array 1.. 10 of int
var scoreFile:int

open : scoreFile, "highscore.dat",get

for i: 1..10
     get:scoreFile,names(i):*
     get:scoreFile,scores(i)
end for
close:scoreFile

I'm sure from here you can figure out how to save you data.

Author:  santabruzer [ Fri Jan 09, 2004 8:50 pm ]
Post subject: 

ah.. you can do a tolken on a get from the file.. i thought you couldn't Rolling Eyes


: