START:
  CURSOR OFF
  C = 219
  P = 0
GOTO MAIN

MAIN:
  IF P < 0 THEN P = 3
  IF P = 0 THEN GOSUB RCSTART
  IF P = 1 THEN GOSUB HLSTART
  IF P = 2 THEN GOSUB RNSTART
  IF P = 3 THEN GOSUB CBSTART
  IF P > 3 THEN P = 0
GOTO MAIN

RCSTART:
  INK 7
  CLS
GOTO RANDCHAR

RANDCHAR:
  RAND A 0 79
  RAND B 0 24
  MOVE A B
  IF C = 220 THEN C = 219
  PRINT CHR C ;
  GOSUB KEYBOARD
  IF P > Q THEN RETURN
  IF P < Q THEN RETURN
GOTO RANDCHAR

HLSTART:
  INK 7
  CLS
  C = 219
  FOR A = 0 TO 79
    FOR B = 0 TO 24
      MOVE A B
      PRINT CHR C ;
    NEXT B 
  NEXT A
  A = 0
GOTO HORLINES

HORLINES:  
  FOR B = 0 TO 24
    MOVE A B
    PRINT CHR C ;
  NEXT B
  A = A + 1
  IF A = 80 THEN A = 0
  FOR B = 0 TO 24
    MOVE A B
    PRINT " " ;  
  NEXT B
  GOSUB KEYBOARD
  IF P > Q THEN RETURN
  IF P < Q THEN RETURN
  PAUSE 1
GOTO HORLINES

RNSTART:
  INK 7
  CLS
RANDNUM:
  IF D = 0 THEN RAND A 48 57
  IF D = 1 THEN RAND A 0 255
  IF D = 2 THEN RAND A 65 90
  IF D = 3 THEN RAND A 97 122
  IF D = 4 THEN RAND A 128 165
  IF D = 5 THEN RAND A 179 218
  IF D = 6 THEN RAND A 1 2
  IF D = 7 THEN RAND A 169 170
  IF D = 8 THEN RAND A 176 178
  REM IF D = 8 THEN RAND A 219 223
  IF D = 9 THEN RAND A 23 31
  IF A = 7 THEN A = 0
  RAND X 0 79
  RAND Y 0 24
  MOVE X Y
  PRINT CHR A ;
  E = D
  GOSUB KEYBOARD
  IF E > D THEN CLS
  IF E < D THEN CLS
  IF P > Q THEN RETURN
  IF P < Q THEN RETURN
GOTO RANDNUM

CBSTART:
  CLS
  C = 219
COLOURBOX:
  RAND A 0 255
  RAND X 0 79
  RAND Y 0 24
  MOVE X Y
  IF C = 219 THEN C = 220
  INK A
  PRINT CHR C ;
  GOSUB KEYBOARD
  IF P > Q THEN RETURN
  IF P < Q THEN RETURN
GOTO COLOURBOX
  
KEYBOARD:
  GETKEY K
  IF K > 47 THEN IF K < 58 THEN D = K - 48
  IF K = 27 THEN GOTO EXIT
  IF K = 'C' THEN CLS
  IF K = 'c' THEN CLS
  IF K = '0' THEN C = 32
  IF K = '1' THEN C = 176
  IF K = '2' THEN C = 177
  IF K = '3' THEN C = 178
  IF K = '4' THEN C = 219
  IF K = '5' THEN C = 197
  IF K = '6' THEN C = 206
  IF K = '7' THEN C = 254
  IF K = '8' THEN C = 233
  IF K = '9' THEN C = 1
  Q = P
  IF K = '+' THEN P = P + 1
  IF K = '=' THEN P = P + 1
  IF K = '-' THEN P = P - 1
  IF K = '_' THEN P = P - 1
  IF S = 1 AND K = ' ' THEN S = 2
  IF S = 0 AND K = ' ' THEN S = 1
  IF S = 2 THEN S = 0
  IF S = 1 THEN GOTO KEYBOARD
RETURN

EXIT:
  INK 7
  CLS
  CURSOR ON
  END

