-
Notifications
You must be signed in to change notification settings - Fork 0
/
HIGHSCOR.BAS
executable file
·239 lines (150 loc) · 4.44 KB
/
HIGHSCOR.BAS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
DECLARE SUB demo ()
DECLARE SUB savehighscores ()
DECLARE SUB addscore (newname$, newscore!)
DECLARE SUB createfile ()
DECLARE SUB displayscores ()
DECLARE SUB loadhighscores ()
DECLARE SUB verifyfile ()
DIM SHARED name$(51)
DIM SHARED score(51)
COMMON SHARED errorflag, filename$, numscores
ON ERROR GOTO errorhandle
RANDOMIZE TIMER
CLS
' set the high score file name
filename$ = "high.scr"
' set the number of high scores in the list
numscores = 15
'make sure that there is a file with the name that is in "filename$"
verifyfile
' Load the high scores into the name$() and socre() arrays
loadhighscores
' This is the subroutine to use to add a new name
addscore "Your Name", 50
' Display the scores on the screen
displayscores
LOCATE 17, 10
PRINT "Press <space> to add a random score"
LOCATE 18, 10
PRINT "Press <n> to reset the scores and add one random score"
LOCATE 19, 10
PRINT "Press <Esc> to exit"
DO
a$ = INKEY$
IF a$ = " " THEN demo
IF a$ = "n" THEN
createfile
loadhighscores
demo
END IF
LOOP UNTIL a$ = CHR$(27)
END
' NOTE: It is critical that you have the ON ERROR code included in this
' program, since it detects the non-existence of the high score file
errorhandle:
' Set the errorflag variable to signal that there was an error
' then let the program continue if the errors are able to be handled
errorflag = ERR
IF errorflag = 53 THEN RESUME NEXT ' File Not Found
IF errorflag = 62 THEN RESUME NEXT ' Input Past End Of File
PRINT "There is an error: code "; errorflag
END
SUB addscore (newname$, newscore)
' since the scores are already in order, adding a new score
' to the list is simple. The following code checks for the
' highest place that the new score can go, moves the other
' scores down and puts the new socre in it's new place.
' score(1) is the highest, and score(numscores) is the lowest
' Check for a position to fill
' "found" is the place where the new score will go
found = 0
FOR t = numscores TO 1 STEP -1
IF newscore > score(t) THEN found = t
NEXT t
IF found = 0 THEN
' The new score is not high enough to get onto the list
' Nothing happens
ELSE
' The score is high enough to get onto the list
' Shift the scores down
FOR t = numscores TO (found + 1) STEP -1
name$(t) = name$(t - 1)
score(t) = score(t - 1)
NEXT t
' Put the new name and score into place
name$(found) = newname$
score(found) = newscore
' If the score qualified to be on the list, it is now in the
' score() array, and the name is in its respective location
' Now the score has to be saved into the file
savehighscores
END IF
END SUB
SUB createfile
' This subroutine creates a default high score file
zero = 0
OPEN filename$ FOR OUTPUT AS #2
FOR t = 1 TO numscores
PRINT #2, "Nobody"
PRINT #2, zero
NEXT t
CLOSE #2
END SUB
SUB demo
' demo: temp$ is a random character
' scr is a random score
INPUT "Name"; temp$
scr = INT(RND * 100)
addscore temp$, scr
CLS
displayscores
LOCATE 17, 10
PRINT "Press <space> to add a random score"
LOCATE 18, 10
PRINT "Press <n> to reset the scores and add one random score"
LOCATE 19, 10
PRINT "Press <Esc> to exit"
END SUB
SUB displayscores
FOR t = 1 TO numscores
' draw a line for the name and score
LOCATE t, 10
PRINT "..............................."
' print the name and score
LOCATE t, 10
PRINT name$(t)
LOCATE t, 40
PRINT score(t)
NEXT t
END SUB
SUB loadhighscores
' Now that there is for sure a high score file, open it and retrieve
' all "numscores" of the names and scores in it
OPEN filename$ FOR INPUT AS #1
FOR t = 1 TO numscores
INPUT #1, name$(t)
INPUT #1, score(t)
NEXT t
CLOSE #1
END SUB
SUB savehighscores
' Open the high score file and save the new scoreboard to it
OPEN filename$ FOR OUTPUT AS #1
FOR t = 1 TO numscores
PRINT #1, name$(t)
PRINT #1, score(t)
NEXT t
CLOSE #1
END SUB
SUB verifyfile
' Try to open the high score file to see if it exists
' If it does not exist, it will cause an error. The
' error handling code sets a flag to signal the error
OPEN "high.scr" FOR INPUT AS #1
' If there is no high score file, errorflag will equal 1
' so create the high score file and fill it with default
' information. Then reset the errorflag
IF errorflag = 53 THEN createfile
errorflag = 0
CLOSE
END SUB