Q and A

Date: 198x
Type: Program
Platform(s): TS 2068
Tags: Game

This program implements a question-and-answer quiz game where the user first enters a set of clue/answer pairs, then runs through them in randomized order. The quiz awards 2 points for a correct first attempt and 1 point for a correct second attempt after a hint; the hint reveals only the first and last letters of the answer with dashes filling the middle. Arrays `c$`, `a$`, `l`, and `y` store the question text, answer strings, answer lengths, and a shuffled index respectively, with the index array shrinking as questions are consumed to avoid repeats. The program saves itself in two forms: one starting at the quiz-play loop (line 20) and one starting at the data-entry section (line 430).


Program Analysis

Program Structure

The program is divided into two logical phases. Lines 430–590 handle data entry: the user specifies how many questions to use, then types each clue and answer interactively. Lines 10–420 implement the quiz engine, which randomizes question order and scores responses. The SAVE commands at lines 580–590 persist both phases separately so the quiz can be replayed without re-entering questions.

Data Structures

VariableTypePurpose
c$(m,64)String arrayClue/question text, up to 64 characters each
a$(m,15)String arrayAnswer strings, up to 15 characters each
l(m)Numeric arrayActual length of each answer (to trim fixed-length string padding)
y(m)Numeric arrayRemaining question indices for random selection
xNumericCount of remaining questions
sNumericRunning score
tNumericPoints available for current question (2 = first try, 1 = second try)

Randomization Without Replacement

The program uses a shrinking-index technique to pick questions randomly without repetition. Array y is initialized at line 40 to hold values 1 through m. At line 70, a random position z within the remaining x slots is picked, and the corresponding question index q is retrieved. Lines 80–90 then close the gap by shifting remaining entries down one position, effectively removing the chosen question from the pool. The counter x is decremented so subsequent picks operate on the shorter list.

Hint Mechanism

When the first attempt is wrong, the program drops t from 2 to 1 and shows a hint at line 320. The hint is constructed by concatenating the first character of the answer, a substring of dashes equal to l(q)-2 characters from the 15-character dash string b$, and the last character of the answer. This reveals boundary letters only and requires l(q) >= 2 to avoid an anomaly with single-character answers.

Answer Comparison

Line 120 compares user input i$ against a$(q)(TO l(q)) — a substring slice using the stored length — rather than the full padded fixed-length string. This is necessary because BASIC string arrays pad entries with spaces to the declared dimension width, so a direct equality check would almost always fail.

Key Input Subroutine

Lines 400–420 implement a clean single-keypress reader. Line 400 waits for any previously held key to be released (busy-loop while INKEY$ is non-empty), and line 410 then waits for a new keypress. Line 420 captures the key into k$ and returns. This two-phase debounce pattern is a standard BASIC idiom to prevent accidental key repeats.

Scoring and Flow

  1. Question is displayed; player types an answer.
  2. If correct on first try, t=2 is added to s; praise is shown.
  3. If wrong and t=2, a hint is shown and t is set to 1 for a second attempt.
  4. If wrong on the second try (t=1), the full answer is revealed and no points are awarded.
  5. After all questions, the final score out of m*2 is displayed.

Notable Techniques

  • The preview loop at line 50 flashes each answer briefly using PAUSE 50 before the quiz starts, giving a quick study pass.
  • Pressing Enter (blank input) at line 230 skips the “wrong answer” message and goes straight to the hint/reveal logic at line 260.
  • The GO SUB 340 at line 50 and the GO SUB 350 calls share the same “Press ENTER to continue” subroutine body; GO SUB 340 falls through into it naturally because line 340 is just a REM.
  • The data-entry phase (line 430) lets the user confirm or delete each entry before committing, using the k$ single-key input routine.

Potential Anomalies

  • The hint formula a$(q,1)+b$(1 TO l(q)-2)+a$(q,l(q)) will produce an error if any answer is exactly one character long, since l(q)-2 would be negative, giving an invalid slice of b$.
  • AT 9,15,, at line 200 uses a double comma which, in this dialect, repeats the last attribute argument; this is likely intended to set ink/paper or move the cursor but may behave unexpectedly if no prior color attributes have been set in the same PRINT statement.
  • Line 420 reads INKEY$ a second time after the loop at line 410 exits, which could theoretically miss a very briefly pressed key if it was released between the loop exit and the LET assignment, though this is unlikely in practice.

Content

Appears On

Library tape of the Indiana Sinclair Timex User’s Group.

Related Products

Related Articles

Related Content

Image Gallery

Source Code

   10 REM  INITIALIZATION 
   20 REM     RUN 430
   30 LET s=0: LET x=m: LET b$="---------------"
   40 FOR n=1 TO m: LET y(n)=n: NEXT n
   50 GO SUB 340: CLS : FOR n=1 TO m: PRINT AT 10,8;a$(n): PAUSE 50: CLS : NEXT n
   60 REM  MAIN PROGRAM 
   70 LET z=INT (RND*x+1): LET q=y(z)
   80 LET x=x-1
   90 FOR n=z TO x: LET y(n)=y(n+1): NEXT n
  100 CLS : LET t=2
  110 PRINT ''''c$(q): INPUT i$
  120 IF i$<>a$(q)( TO l(q)) THEN GO TO 230
  130 LET s=s+t: GO TO 200
  140 GO SUB 350: CLS 
  150 IF x>0 THEN GO TO 70
  160 REM  FINAL SCORE 
  170 PRINT AT 5,0;"Your score was ";s;" out of ";m*2
  180 STOP 
  190 REM  PRAISE 
  200 PRINT AT 7,0;"Well done, ";i$;" was right.";AT 9,15,,
  210 GO TO 140
  220 REM  WRONG ANSWER 
  230 IF i$="" THEN GO TO 260
  240 PRINT AT 7,0;i$;" was wrong.",,AT 9,0,,
  250 GO SUB 350
  260 IF t=2 THEN GO TO 300
  270 PRINT AT 7,0;"The right answer was:",,AT 9,15;a$(q)
  280 GO TO 140
  290 REM  HINT/SECOND TRY 
  300 LET t=1
  310 PRINT AT 7,0;"Here is a hint:",,AT 21,0,,
  320 PRINT AT 9,15;a$(q,1)+b$(1 TO l(q)-2)+a$(q,l(q))
  330 GO TO 110
  340 REM  ENTER TO CONTINUE 
  350 PRINT AT 21,0;"Press ENTER to continue."
  360 GO SUB 400
  370 IF CODE k$=13 THEN RETURN 
  380 GO TO 360
  390 REM  SINGLE KEY INPUT  
  400 IF INKEY$<>"" THEN GO TO 400
  410 IF INKEY$="" THEN GO TO 410
  420 LET k$=INKEY$: RETURN 
  430 REM ENTER QUESTIONS/ANSWERS
  440 CLS : PRINT "How many questions?": INPUT m
  450 DIM c$(m,64): DIM a$(m,15): DIM l(m): DIM y(m)
  460 FOR q=1 TO m
  470 CLS : PRINT "Question ";q''
  480 PRINT "Type in the question or clue.": INPUT c$(q)
  490 PRINT 'c$(q)''"Type in the answer.": INPUT i$
  500 PRINT 'i$;AT 20,0;"If satisfactory, type s.","  To delete, type d."
  510 GO SUB 400
  520 IF k$="d" THEN GO TO 470
  530 IF k$<>"s" THEN GO TO 510
  540 LET l(q)=LEN i$: LET a$(q)=i$
  550 NEXT q
  560 REM SAVE AND RUN 
  570 CLS : INPUT "What is the program name?";p$
  580 SAVE p$ LINE 20
  590 SAVE "QUEST&ANSW" LINE 430

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.

People

No people associated with this content.

Scroll to Top