States And Capitals

This file is part of and SINCUS Exchange Tape 103 - Potpourri. Download the collection to get this file.
Date: 198x
Type: Program
Platform(s): TS 2068
Tags: Education

This program is an educational quiz covering all 50 U.S. state capitals, offering two game modes: guessing the capital from a state name, or guessing the state from its capital. It loads a companion SCREEN$ file (“usa”) as a title graphic, then uses a 50-element array `l()` to track which states have already been asked, preventing repeats within a ten-question round. When a player answers incorrectly, a hint subroutine at line 2000 reveals the first letter of the correct answer and prompts a second attempt, though only first-attempt correct answers increment the score counter `c`. A short BEEP melody at line 3010 rewards correct first-attempt answers, and the final score screen at line 4000 displays both the raw count and a percentage.


Program Analysis

Program Structure

The program divides into several logical blocks:

  1. Lines 5–20: Initialization — sets colors, dimensions the tracking array, loads a title screen, and waits for a keypress.
  2. Lines 40–95: Main menu — lets the player choose between guessing capitals (mode 1) or guessing states (mode 2).
  3. Lines 100–390: Capital-guessing loop — ten questions, each drawn randomly from the DATA, with duplicate prevention.
  4. Lines 400–590: State-guessing loop — structurally mirrors lines 100–390 but reads DATA fields in a different order.
  5. Lines 720–960: DATA block — all 50 state/capital pairs, each with a numeric index (1–50).
  6. Lines 2000–2050: Incorrect-answer subroutine — prints a first-letter hint and re-prompts.
  7. Lines 3000–3020: Correct-answer subroutine — plays a short BEEP melody and increments score.
  8. Lines 4000–4020: Score display — shows raw count and percentage, then loops back to the menu.

Random Selection and Duplicate Prevention

Random state selection is handled by an unconventional DATA-walking technique. Lines 110–130 (and 410–430) use a FOR d=1 TO (RND*50)+1 loop that READs through the DATA list a random number of times to land on a pseudo-random entry. Because RND returns a value in [0,1), the loop count ranges from 1 to 50, covering all entries.

After selecting an entry, RESTORE resets the DATA pointer so the same walk can happen next question. The numeric index x read from each DATA triplet is used to check and set l(x) in the 50-element array declared at line 8. If l(x)=1, the entry was already used and the inner loop reruns (lines 150 and 450). This approach guarantees no state is repeated within a round but does not guarantee termination in bounded time if RND repeatedly lands on used indices — a minor probabilistic inefficiency.

Dual Game Modes and DATA Field Order

The two quiz modes reuse the same DATA block but swap the read order. In capital-guessing mode (line 120), READ a$: READ b$: READ x assigns the state name to a$ and the capital to b$. In state-guessing mode (line 420), READ b$: READ a$: READ x swaps them, so a$ becomes the capital and b$ becomes the state. The question-printing and answer-checking logic (lines 330/530, 352/552, 353/553) is otherwise identical in structure.

Scoring Logic

The score counter c is only incremented inside the correct-answer subroutine at line 3015. Because the hint subroutine (line 2000) takes a second INPUT but does not call the correct-answer subroutine, a player who answers correctly only on the second attempt does not earn a point. The final display at line 4005 computes c/f*(100) for the percentage; since f is always 10 at the end of either loop (checked at lines 380 and 580 before branching to 4000), this correctly yields c*10 percent.

Key BASIC Idioms and Techniques

  • PAUSE 0 at lines 20, 377, 577, and 4010 halts execution until any key is pressed — a standard Sinclair BASIC keypress-wait idiom.
  • TAB (32-LEN b$)/2 at lines 370 and 570 centers the correct answer string across the 32-column display.
  • AT 7,(32-(LEN a$+3))/2 at line 530 attempts to center “of [capital]?” accounting for the three extra characters in the prefix “of “.
  • FLASH 1 is used on key UI prompts (lines 15, 70, 377, 577, 2000, 4010) to draw attention without color changes.
  • INVERSE 1 on the “Press any key” prompt at lines 377 and 577 provides visual emphasis in a different style from FLASH.
  • The hint subroutine references b$(1) to extract the first character of the correct answer string — straightforward Sinclair string slicing.

Notable Bugs and Anomalies

  • Mode 2 entry point conflict: The state-guessing loop begins at line 400, which is also the target of GO TO 400 from the menu (line 90). However, the capital-guessing loop (lines 100–390) falls through to line 400 via NEXT f at line 390 whenever f does not equal 10 — but since the loop runs f=1 TO 10 and only exits early when f=10, the NEXT f at line 390 would normally complete all 10 iterations before reaching line 400. In practice the IF f=10 THEN GO TO 4000 at line 380 exits after the tenth question, so NEXT f at line 390 is never reached. The code is structurally unusual but functionally correct.
  • Array not reset between rounds: l() is initialized by DIM l(50) only once at line 8. When the program loops back to line 40 via GO TO 40 at line 4020, RESTORE and LET c=0 are called but the array is not re-dimensioned or cleared, meaning previously used state indices remain flagged. This causes increasing difficulty finding unused states in subsequent rounds and could lead to an infinite loop once all 50 states are marked.
  • Double-check idiom: Lines 352–353 use both IF c$=b$ THEN GO SUB 3000 and IF c$<>b$ THEN GO SUB 2000 rather than IF ... THEN ... ELSE .... This is a common Sinclair BASIC pattern since the language lacks a native ELSE keyword, and it is functionally correct here.

Content

Appears On

A little of everything — play Battleship with adjustable AI, solve Klondike Solitaire with custom card graphics, guide Santa's reindeer into their pen, or track your stock portfolio on tape. The potpourri tape lives up to its name.

Related Products

Related Articles

Related Content

Image Gallery

States And Capitals

Source Code

    5 REM capitals 2
    6 PAPER 1: BORDER 1: INK 7: CLS 
    8 DIM l(50)
    9 LOAD "usa"SCREEN$ 
   10 PRINT AT 10,6;"STATES AND CAPITALS"
   15 PRINT AT 13,9; FLASH 1;"Use Caps Lock."
   20 PRINT AT 20,3;"Press any key to continue": PAUSE 0
   40 RESTORE : LET c=0: CLS 
   50 PRINT AT 5,9;"DO YOU WISH TO"
   55 PRINT AT 9,5;"(1) GUESS THE CAPITALS"
   60 PRINT AT 11,5;"(2) OR GUESS THE STATES"
   70 PRINT AT 16,10; FLASH 1;"PRESS 1 OR 2"
   80 IF INKEY$="1" THEN GO TO 100
   90 IF INKEY$="2" THEN GO TO 400
   95 GO TO 80
  100 FOR f=1 TO 10
  110 FOR d=1 TO (RND*50)+1
  120 READ a$: READ b$: READ x
  130 NEXT d
  140 RESTORE : CLS 
  150 IF l(x)=1 THEN GO TO 110
  160 LET l(x)=1
  330 PRINT AT 5,3;"(";f;") ";"What is the capital of ";AT 7,(32-LEN a$)/2;a$;"?"
  350 INPUT c$
  352 IF c$=b$ THEN GO SUB 3000
  353 IF c$<>b$ THEN GO SUB 2000
  355 PRINT 
  360 PRINT "Your answer was  ";c$
  365 PRINT 
  370 PRINT "The correct answer is ";''TAB (32-LEN b$)/2;b$
  375 PRINT : PRINT 
  377 PRINT TAB 3; INVERSE 1;"Press any key to continue": PAUSE 0
  380 IF f=10 THEN GO TO 4000
  390 NEXT f
  400 FOR f=1 TO 10
  410 FOR d=1 TO (RND*50)+1
  420 READ b$: READ a$: READ x
  430 NEXT d
  440 RESTORE : CLS 
  450 IF l(x)=1 THEN GO TO 410
  460 LET l(x)=1
  530 PRINT AT 5,1;"(";f;")";" What state has the capital";AT 7,(32-(LEN a$+3))/2;"of ";a$;"?"
  550 INPUT c$
  552 IF c$=b$ THEN GO SUB 3000
  553 IF c$<>b$ THEN GO SUB 2000
  555 PRINT 
  560 PRINT "Your answer was  ";c$
  565 PRINT 
  570 PRINT "The correct answer is ";''TAB (32-LEN b$)/2;b$
  575 PRINT : PRINT 
  577 PRINT TAB 3; INVERSE 1;"Press any key to continue": PAUSE 0
  580 IF f=10 THEN GO TO 4000
  590 NEXT f
  720 DATA "ALABAMA","MONTGOMERY",1,"ALASKA","JUNEAU",2
  730 DATA "ARIZONA","PHOENIX",3,"ARKANSAS","LITTLE ROCK",4
  740 DATA "CALIFORNIA","SACRAMENTO",5,"COLORADO","DENVER",6
  750 DATA "CONNECTICUT","HARTFORD",7,"DELAWARE","DOVER",8
  760 DATA "FLORIDA","TALLAHASSEE",9,"GEORGIA","ATLANTA",10
  770 DATA "HAWAII","HONOLULU",11,"IDAHO","BOISE",12
  780 DATA "ILLINOIS","SPRINGFIELD",13,"INDIANA","INDIANAPOLIS",14
  790 DATA "IOWA","DES MOINES",15,"KANSAS","TOPEKA",16
  800 DATA "KENTUCKY","FRANKFORT",17,"LOUISIANA","BATON ROUGE",18
  810 DATA "MAINE","AUGUSTA",19,"MARYLAND","ANNAPOLIS",20
  820 DATA "MASSACHUSETTS","BOSTON",21,"MICHIGAN","LANSING",22
  830 DATA "MINNESOTA","ST. PAUL",23,"MISSISSIPPI","JACKSON",24
  840 DATA "MISSOURI","JEFFERSON CITY",25,"MONTANA","HELENA",26
  850 DATA "NEBRASKA","LINCOLN",27,"NEVADA","CARSON CITY",28
  860 DATA "NEW HAMPSHIRE","CONCORD",29,"NEW JERSEY","TRENTON",30
  870 DATA "NEW MEXICO","SANTA FE",31,"NEW YORK","ALBANY",32
  880 DATA "NORTH CAROLINA","RALEIGH",33,"NORTH DAKOTA","BISMARCK",34
  890 DATA "OHIO","COLUMBUS",35,"OKLAHOMA","OKLAHOMA CITY",36
  900 DATA "OREGON","SALEM",37,"PENNSYLVANIA","HARRISBURG",38
  910 DATA "RHODE ISLAND","PROVIDENCE",39,"SOUTH CAROLINA","COLUMBIA",40
  920 DATA "SOUTH DAKOTA","PIERRE",41,"TENNESSEE","NASHVILLE",42
  930 DATA "TEXAS","AUSTIN",43,"UTAH","SALT LAKE CITY",44
  940 DATA "VERMONT","MONTPELIER",45,"VIRGINIA","RICHMOND",46
  950 DATA "WASHINGTON","OLYMPIA",47,"WEST VIRGINIA","CHARLESTON",48
  960 DATA "WISCONSIN","MADISON",49,"WYOMING","CHEYENNE",50
 2000 PRINT : PRINT FLASH 1;"HINT";: PRINT " - It begins with the letter - ";b$(1);"  ";''TAB 12;"TRY AGAIN"
 2020 INPUT c$
 2050 RETURN 
 3000 REM  music
 3010 BEEP .5,10: BEEP .2,8: BEEP .2,8: BEEP .2,6: BEEP .2,5: BEEP .2,6: BEEP .5,8: BEEP .4,3
 3015 LET c=c+1
 3020 RETURN 
 4000 REM number correct
 4005 CLS : PRINT AT 5,5;"On the first response";AT 8,7;"You got ";c;" or ";c/f*(100);"%";AT 12,6;" out of 10 correct."
 4010 PRINT AT 18,3; FLASH 1;"Press any key to continue.": PAUSE 0
 4020 GO TO 40

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

People

No people associated with this content.

Scroll to Top