This tutorial is guaranteed to be 100% Worble free! Bickley is now out of his cast and wheelchair, and has joined Phyllie, Pablo, Ophelia, Mike and the rest, for a season of surfing and sailing in Perth, Australia. After arriving at LAX aboard Albatross Airlines, they quickly boarded a taxi to Long Beach and purchased tickets aboard Sea Horse Cruise Lines, and they're off for the remainder of our winter and the Australian summer.
For those with a mind for details, how many Worlbes can sail aboard a sea horse? SOLUTION: Determine the available deckspace aboard a sea horse, (H). Calculate the area required for a Worble and lounge chair, (W). Divide W into H.
Okay, now that you have finished your Golly-Gee adventure game, you want to post the hi-scores every time your program runs. Let's assume your game is text based and uses the MAINWIN for players. The techniques given here work just as well for a GUI based game, but don't require me to create a GUI for demonstration.
If your game is Galactic Invaders, you will probably want to record the name and score of the players who destroy the most enemy space craft. If your game is Galactic Defenders, you may want to record the name and score of the players who suffer the fewest "hits" upon their own space craft. Your choice will determine how you write the code. In this example, I am using the five highest scores.
First, you need a plan... don't we always? We're going to keep the name and score of the top 5 players in a text file. Why? Have you tried keeping them in DATA statements in your main program? While this works well, it does not allow you to add or remove players and scores without editing the BAS program. So far we know we need a DATA file for 5 players and scores. The DATA file will be updated ONLY if a player achieves a score higher than fifth highest score currently in the file. For simplicity, let's call our DATA file "highScores.txt".
When loading and sorting the name and score of the top 5 players, a two-dimensional array sounds like an excellent choice. Because name$ consists of letters, Just Basic requires this array to be a string array, hiScore$(5,2), and the numeric score will be entered as a string. The array will have 5 elements and 2 values for each element.
The first time we run the program, we'll receive an error when trying to open "highScores.txt" if the file does not exist. To avoid this, I copied a routine directly from the help file to check for file existance and create one with the necessary DATA if it does not exist. Who knew these help files could be so useful? Perhaps that's why they are called "help?"
'Using a File to Display Current High Scores
'Written by Welo, 11/20/05
DIM hiScore$(5,2)
'=====Check for existance of DATA file, create if needed
DIM info$(10, 10)
IF fileExists(DefaultDir$, "highScores.txt")=0 THEN
OPEN "highScores.txt" FOR OUTPUT AS #1
FOR k=1 TO 5
PRINT #1, "No records.";", ";" "
NEXT k
CLOSE #1
END IF
[begin] 'Branch to here if running again
'===== Load the array from file and print to screen
OPEN "highScores.txt" FOR INPUT AS #1
FOR k=1 TO 5
INPUT #1, hiScore$(k,1), hiScore$(k,2)
PRINT hiScore$(k,2); " "; hiScore$(k,1)
NEXT k
CLOSE #1
'===== The array has been loaded from the DATA file
PRINT
'===== Get user input if desired
INPUT "Add a player and score to the high scores? (Y/N) "; resp$
resp$=UPPER$(resp$)
IF LEFT$(resp$, 1)= "Y" THEN
PRINT
INPUT "Player name? "; name$
INPUT "Player score? "; score$
'If score is acceptable, enter into hiScore$(array)
IF VAL(score$)>VAL(hiScore$(5,2)) THEN
hiScore$(5,1)=name$
hiScore$(5,2)=score$
ELSE
'If the new score is lower than the top 5, reject the input
PRINT "Sorry, that score is not high enough for the record book."
END IF
END IF
'===== End of input routine
'===== Sort the array by high scores
FOR k=1 TO 5
FOR i=1 TO 4
IF VAL(hiScore$(i,2))
You can copy
and paste this routine directly into your Golly-Gee adventure game.
Check to be sure no variables in the above code are also used in your
program code. Check also to avoid line breaks when copying from the web
page.
After checking
to see if our DATA file exists and creating one if needed,
hiScore$(array) is loaded from the file. The user is asked if he/she
wishes to enter new DATA for the array. In your Golly-Gee game, this
could be a simple IF/THEN to add the data if the new score ranks among
the top five.
Just Basic does
not have a native sort function, so I wrote a simple bubble sort for
this array. Since we won't be recording the top 1,000 scores and
players, the bubble sort will be more than fast enough. This code is
written to have the highest scores at the top of the list. If you want
to have the lowest scores at the top (Galactic Defenders) simply
reverse the "<" sign used for comparison.
After the array
has been sorted, we OPEN highScores.txt again, this time FOR OUTPUT,
and print the five highest scores to the DATA file for use next time we
run the program. NOTE: When printing more than one value per line to a
DATA file, you cannot use the comma and expect to have the values
separated as you would for display in the MAINWIN. Instead, you must
concatenate them, placing the comma and any spaces either as string
values, or ASCII values.
Now that we
have a DATA file for highScores.txt, the next time the program is run,
FUNCTION fileExists will not be executed. By the way, if you simply
can't abide NOT using your coding skills to cheat, you can open
highScores.txt with Wordpad and enter your own name and outrageous
scores as many times as you like.