Wednesday, March 14, 2018

A Simple Quiz Game in Commodore 64 Basic a Grade Schooler Could Write


10 print "quizgame"
20 open 1,8,8,"quizfile,r,s"
30 dim answer$(9)
35 i=0
36 p=0:rem points
37 qs$=""
40 input# 1,a$
50 rem print a$
60 if a$="!" goto 900
80 ca=0:rem correct answers
100 type$=left$(a$,1)
130 if type$="q" then qs$=qs$+mid$(a$,2,255)
150 if type$="a" then gosub 200
160 if type$="c" then gosub 250
190 goto 40
200 answer$(i)=mid$(a$,2,255)
210 i=i+1
220 return
250 rem gosub from 160
260 ca=val(mid$(a$,2,1))
270 gosub 300
280 return
300 print qs$
310 for j=0 to i-1:rem gameplay
320 print str$(j+1)+":"+answer$(j)
330 next j
340 i=0
350 get an$:if an$="" goto 350
360 an=val(an$)
370 if ca=an then p=p+1
380 print "your answer was:", an
390 print "correct answer was:",ca
400 print ,answer$(ca-1)
410 return
900 close 1
910 print "number of points:"+str$(p)

So...yeah.
Basically, that's all it took. It reads in a file "quizfile" and lines that start with a q, are part of the question, a lines contain an answer, and c lines are followed by a number indicating which answer was correct.

For an example:
qWhat is your favourite color
aBlue
aRed
aBlue, i mean red!
c1


This is not quite enough, as the ability to create the file is required. A simple solution - and the one I used to write the file was this:

5 dim o$(100)
6 i=1
10 input o$(i)
20 if o$(i)="!" then 100
40 i=i+1
50 goto 10
100 print "end"
105 print i;"lines of text"
107 open 1,8,8,"@0:quizfile,w,s"
110 for j=0 to i
120 print o$(j)
140 print#1,o$(j)
150 next
160 close 1

It's not good enough, though, since you do need the ability to add new questions.
It's also not the simplest possible way, since you could do the whole thing with a goto after the open in a input-print# loop until end symbol, but I often putter around, trying different things, and sometimes the solution can be messy.

simple would be something like this:
10 open 1,8,8,"@0:quizfile,w,s"
20 input a$
30 if a$="!" then 100
40 print# 1,a$
50 goto 20
100 close 1

Well, I'm going to be compassionate towards my younger self. After all, it took me a while to figure out how to write and load from files, and an arcane syntax like "@0:" is more than a bit odd, although I think it means "start writing to the file from this location. Because, otherwise, the file doesn't save, and then, sadness. 

No comments: