'===========================================================================
' Subject: How Random Are You?                Date: 06-16-02 (  :  )       
'  Author: David Williams                     Code: Qbasic,QB,PDS          
'  Origin: david.williams@ablelink.org      Packet: ALGOR.ABC
'===========================================================================
' KEYPRED.BAS 
' Keypress predictor, David Williams, 2002 Jun 05 
' david.williams@ablelink.org 
  
' Program invites user to press keys at random, and spots 
' patterns in his guesses so as to make predictions. 
' Useless, but fun! 
  
DECLARE FUNCTION Key$ (U$) 
DECLARE SUB PutOut (U$) 
  
DEFLNG A-Z 
  
WIDTH 80, 50 
  
CLS 
  
DIM SHARED P% ' printer flag 
  
PRINT "Look back how many strokes (1 - 5) (Try 3 first!)?"; 
  
K% = VAL(Key$("12345")) 
  
PRINT K% 
PRINT 
  
PRINT "Output to printer (y/n)? "; 
  
P% = (Key$("YN") = "Y") 
  
IF P% THEN 
  PRINT "Y" 
  PRINT 
  PRINT "Ensure printer is ready, then press <Enter>" 
  X$ = Key$(CHR$(13)) 
END IF 
  
S% = 2 ^ (K% - 1) 
T% = S% + S% - 1 
DIM A!(0 TO T%)' array holds results from like sequences 
  
FOR X% = 0 TO T% 
  A!(X%) = 0 
NEXT 
  
Q% = 0   ' summary of previous K% keypresses 
F! = .7  ' old-keys decline factor 
G! = 1 - F! 
CP = 0   ' prediction counter 
CC = 0   ' correct prediction counter 
CK = 0   ' keypress counter 
  
CLS 
PRINT "Press 'Z' and '/' keys repeatedly, in what feels like a" 
PRINT "random sequence. The program will spot your patterns, and" 
PRINT "make predictions. If you wish, look away from the screen so" 
PRINT "your keypresses will not be influenced by the predictions." 
PRINT 
PRINT "When you wish to finish, press 'Q'. A summary will be printed." 
PRINT "The random expectation of correct predictions is 50%." 
PRINT "The program will do a lot better than that!" 
PRINT 
PRINT "In the screen/printer display, each pair of characters shows a" 
PRINT "prediction and the key that is actually pressed. (A dash in" 
PRINT "the first position shows that no prediction has been made.)" 
PRINT 
PRINT "The final 'Q' keypress, and any prediction that may have been" 
PRINT "made immediately before it, are not included in totals." 
PRINT 
PRINT "Start now." 
PRINT 
  
DO 
  
  IF ABS(A!(Q%)) > .8 THEN 
    IF A!(Q%) > 0 THEN 
      PutOut "Z" 
    ELSE 
      PutOut "/" 
    END IF 
    FF% = 1 
  ELSE 
    PutOut "-" 
    FF% = 0 
  END IF 
  
  K$ = Key$("Z/Q") 
  
  PutOut K$ + " " 
  IF POS(0) > 75 THEN PutOut "" 
  
  SELECT CASE K$ 
    CASE "Z" 
      N! = G! 
      Z% = S% 
    CASE "/" 
      N! = -G! 
      Z% = 0 
    CASE ELSE 
      EXIT DO 
  END SELECT 
  
  CK = CK + 1 
  
  IF FF% THEN 
    CP = CP + 1 
    IF SGN(N!) = SGN(A!(Q%)) THEN CC = CC + 1 
  END IF 
  
  IF CK > K% THEN A!(Q%) = F! * A!(Q%) + N! 
  
  Q% = Z% OR Q% \ 2 
  
LOOP 
  
IF POS(0) > 1 THEN PutOut "" 
  
PutOut "" 
  
PutOut "Keypresses:" + STR$(CK) 
PutOut ". Predictions:" + STR$(CP) 
PutOut ". Correct:" + STR$(CC) 
IF CP THEN PutOut ". Percent correct:" + STR$(CINT(CC / CP * 100)) 
PutOut "" 
  
IF P% THEN LPRINT CHR$(12); ' form feed to eject paper 
  
END 
  
FUNCTION Key$ (U$) 
  DO WHILE INKEY$ <> "" 
  LOOP 
  DO 
    K$ = UCASE$(INPUT$(1)) 
  LOOP UNTIL INSTR(U$, K$) 
  Key$ = K$ 
END FUNCTION 
  
SUB PutOut (S$) 
  IF LEN(S$) THEN 
    PRINT S$; 
    IF P% THEN LPRINT S$; 
  ELSE 
    PRINT 
    IF P% THEN LPRINT 
  END IF 
END SUB 
