'===========================================================================
' Subject: SMALL HANGMAN GAME                 Date: 11-29-95 (12:15)       
'  Author: Clif Penn                          Code: QB, QBasic, PDS        
'  Origin: comp.lang.basic.misc             Packet: GAMES.ABC
'===========================================================================
' HANGMAN.BAS    Written in QBASIC by Clif Penn   11/29/95

' This test program allows MaxMiss misses (10 here) while guessing at
' letters in a mystery word. Only alphabetic keys and Escape are
' allowed, which means no " - " or " ' ". Scoring is primitive to
' test the arithmetic and logic. Each unguessed letter has a value of
' -1. Each correct letter scores +2. If a letter is guessed that has
' already been tried, this counts as a miss without altering the scoring.
' The game pauses after each word is spelled or MaxMiss misses. After
' the MaxWords (5) words have been used, the program would normally
' load a new batch of MaxWords words. Here the same words are used.
' For convenience of testing, <Esc> exits the program.

DATA horse, river, galaxy, terrain, maximum

MaxMisses% = 10:   MaxWords = 5     ' Change as needed
Esc$ = CHR$(27)

' ******In actual game, MaxWords new words would be retrieved.
' ******Here the same DATA words are RESTOREd
NextBatch:
   RESTORE
   CLS
   PRINT "This would be the beginning of a new game."
   PRINT "When asked for a letter, press <Esc> to quit."
   PRINT "<<<<< Press any key to continue. >>>>>"
   DO
   LOOP UNTIL INKEY$ <> ""

Score% = 0:      WordCount% = 0
WHILE WordCount% < MaxWords
   GOSUB GetWord
  
   WHILE (Misses% < MaxMisses%) AND (Record$ <> Mys$)
      GOSUB GetLetter
      IF Ltr$ = Esc$ THEN GOTO finis
      GOSUB RecordHitsMisses
   WEND
     
   Score% = ThisScore% + Score%
   PRINT "Accumulated Score = "; Score%
   LOCATE 1, 15:   PRINT "INSPECT. PRESS A KEY TO CONTINUE"
   DO
   LOOP WHILE INKEY$ = ""

WEND
GOTO NextBatch

finis:
CLS
END

' ************* SUBROUTINES FOLLOW:

GetWord:
   LtrsUsed$ = "":   Misses% = 0
   CLS
   READ Mys$
   LOCATE 18, 1:   PRINT "Mystery = "; Mys$     ' For test only!
   WordCount% = WordCount% + 1
   Ln% = LEN(Mys$)
   ThisScore% = -1 * Ln%
   Record$ = STRING$(Ln%, "_")
   LOCATE 1, 1:    PRINT Record$
RETURN

GetLetter:
   LOCATE 10, 10:   PRINT ("Press a letter: ");
   DO
      Ltr$ = INKEY$
      Ltr$ = LCASE$(Ltr$)
   LOOP UNTIL (Ltr$ = Esc$) OR ((Ltr$ >= "a") AND (Ltr$ <= "z"))
   PRINT Ltr$
   LtrsUsed$ = LtrsUsed$ + Ltr$
   LOCATE 13, 10: PRINT "Letters used: "; LtrsUsed$
RETURN

RecordHitsMisses:
   Miss% = 1
   FOR n% = 1 TO Ln%
      MLtr$ = MID$(Mys$, n%, 1)
     
      IF (Ltr$ = MLtr$) AND (MID$(Record$, n%, 1) = "_") THEN
         Miss% = 0
         ThisScore% = ThisScore% + 2
         MID$(Record$, n%) = Ltr$
         LOCATE 1, 1:     PRINT Record$
      END IF
   NEXT

   Misses% = Misses% + Miss%
   LOCATE 11, 10: PRINT "Misses ="; Misses%
   LOCATE 6, 1:  PRINT "This word score ="; ThisScore%
RETURN

