I saw your message yesterday but was too sick to reply. In the latest version of your code I see that you use two different files to store the scores and two different arrays. You can put both the names and the scores into one array and save/load into one file.
` Make a multi-dimensional array
dim Score$(9,1)
` Score$(x,0) = Player Name
` Score$(x,1) = Score
The above is a multi-dimensional array that stores 10 player names and 10 player scores. All arrays have a zero element too so for 10 names you only need to dimensionalize it to 9. Score$(0,0) = the first players name, Score$(0,1) = the first players score, Score$(1,0) = the second players name, Score$(1,1) = the second players score and so on till Score$(9,0) = the tenth players name and Score$(9,1) = the tenth players score.
If you're wondering how you can compare a number to a string and vice versa you use the STR$() command to convert a number to a string and the VAL() command to convert a string to a number.
a$="1000"
b$="500"
` Make c equal the number in a$ plus the number in b$
c=val(a$)+val(b$)
` Show the number
print c
wait key
a=3000
b=500
` Make c$ equal the number a minus the number b
c$=str$(a-b)
print c$
wait key
To save/load the file you do exactly what you had but just make it use one file instead. As long as you load the file exactly how you wrote it, it shouldn't be a problem.
Saving:
` Check if score.txt already exists and delete it
if file exist("Scores.txt") then delete file "Scores.txt"
` Open the file to write
open to write 1,"Scores.txt"
` Save the scores to the file
for t=0 to 9
` Write the name
write string 1,Score$(t,0)
` Write the score
write string 1,Score$(t,1)
` Make a blank line (so it looks good when reading in a text editor)
write string 1,""
next t
close file 1
Loading:
` Open the file to read
open to read 1,"Scores.txt"
` Save the scores to the file
for t=0 to 9
` Read the name
read string 1,Score$(t,0)
` Read the score
read string 1,Score$(t,1)
` Grab the blank line (just so it loads right)
read string 1,a$
next t
close file 1
There is a couple of commands you may have overlooked that can make file saving/loading easier. The SAVE ARRAY and LOAD ARRAY commands can do it all for you and save you some grief.
Saving:
save array "Scores.txt",Score$(0)
Loading:
load array "Scores.txt",Score$(0)
Also you don't have to use GOTO to leave the FOR/NEXT loop you can use EXIT to leave any kind of loop. Lets keep away from GOTO.
for t=0 to 2000
` Show t
print "T = "+str$(t)
` Leave the for/next loop when 15 is reached
if t=15 then exit
next t
print ""
print "I'm done."
wait key
In the above code snip the FOR/NEXT loop would count up to 2000 if it wasn't for a check to see if t=15 and EXIT moves it out of the FOR/NEXT loop.
Here's my version of a high score list (with both ways):
` Make random number picking more random
randomize timer()
` Make a multi-dimensional array
dim Score$(9,1)
` Score$(x,0) = Player Name
` Score$(x,1) = Score
do
` Load the scores the hard way
LoadScore()
` Or do it the easy way
`if file exist("Scores.txt") then load array "Scores.txt",Score$(0)
` Pick a random score
S=rnd(500*Count)
P$="Grog "+str$(Count)
` Make High equal the number returned by the CheckScore() function
High=CheckScore(P$,S)
cls
` Show all the scores
for t=0 to 9
print str$(t+1)+". "+Score$(t,0)+" "+Score$(t,1)
next t
wait key
` If a high score was found add to Count
if High=1 then inc Count
` Save the scores the hard way
SaveScore()
` Or do it the easy way
`save array "Scores.txt",Score$(0)
loop
end
function CheckScore(Player$,Score)
` Make a temporary switch
Found=-1
` Go through each score
for t=0 to 9
` Check if the current score is equal to or more than the scores in the array
if Score=>val(Score$(t,1))
` Make the switch equal t (the element in the array for the new score)
Found=t
` Leave the for/next loop
exit
endif
next t
` Check if not a high score and exit the function with a zero
if Found=-1 then exitfunction 0
` Move the score list down to make room for the new score
for t=8 to Found step -1
Score$(t+1,0)=Score$(t,0)
Score$(t+1,1)=Score$(t,1)
next t
` Add the new score to the array
Score$(Found,0)=Player$
Score$(Found,1)=str$(Score)
` A high score was found so exit the function with a 1
endfunction 1
function SaveScore()
` Check if score.txt already exists and delete it
if file exist("Scores.txt") then delete file "Scores.txt"
` Open the file to write
open to write 1,"Scores.txt"
` Save the scores to the file
for t=0 to 9
` Write the name
write string 1,Score$(t,0)
` Write the score
write string 1,Score$(t,1)
` Make a blank line (so it looks good when reading in a text editor)
write string 1,""
next t
close file 1
endfunction
function LoadScore()
` If the file doesn't exist exit the function
if file exist("Scores.txt")=0 then exitfunction
` Open the file to read
open to read 1,"Scores.txt"
` Save the scores to the file
for t=0 to 9
` Read the name
read string 1,Score$(t,0)
` Read the score
read string 1,Score$(t,1)
` Grab the blank line (just so it loads right)
read string 1,a$
next t
close file 1
endfunction
