Shop OBEX P1 Docs P2 Docs Learn Events
Trouble filling an array with LOOKUP for Hangman game — Parallax Forums

Trouble filling an array with LOOKUP for Hangman game

RLmiller777RLmiller777 Posts: 9
edited 2012-10-20 09:25 in BASIC Stamp
Im having trouble filling an array, then displaying it, for a Hangman game.
LOOKUP and filling an Array.....
Its the simple sub at the bottom. Not sure what to study in the book. I tried to use an example from the helpmenu but my program doesnt display anything, in fact it stops and wont run the rest. The last portion is not shown, its just simple subs that were working before the fillletters sub
Thanks in advance for advice.


' {$STAMP BS2}
' {$PBASIC 2.5}
PAUSE 1000
Letters VAR Bit(25) ' remember zero and there are 26
GuessedLetters VAR Bit(25)
counter VAR Nib
iidx VAR Byte
result VAR Byte
'get_a_guess
'displayUsedLetters
'displayLettersLeft
'GoodOrBadGuess
'CheckWinOrLose
Begin: 'Main Program
GOSUB fillletters
GOSUB displayHeader
GOSUB displayhead
GOSUB displayNeck
GOSUB displayLeftarm
GOSUB displayRightarm
GOSUB displayTorso
GOSUB displayLeftleg
GOSUB displayRightleg
GOSUB displayHung
GOSUB displayWinner
END ' Main Program
'*********************************************************
fillletters:
FOR counter = 0 TO 25
LOOKUP counter, ["abcdefghijklmnopqrstuvwxyz"],result
GuessedLetters(counter)=result
NEXT
FOR counter = 0 TO 25
DEBUG ASC? GuessedLetters(counter), CR
NEXT

RETURN
'*********************************************************

Comments

  • SapphireSapphire Posts: 496
    edited 2012-10-19 16:25
    LOOKUP returns a byte in result, representing the ASCII code of the letters a-z. GuessedLetters() is an array of bits, which can only hold 0 or 1. So when you assign result to GuessedLetters(counter) you are only saving the lowest bit, not the letter. The technique to use an array of bits for GuessedLetters() is clever, but you'll need a different routine to set and clear the bits as the letters are entered.

    As for the DEBUG, GuessedLetters() is a bit, 0 or 1, and there is no ASC value for them. The lowest displayable ASC value is 32. Here too you will need to translate the bits, 0 or 1, into the letters, but that is pretty easy. Use capital letters, which start at 65 for A and add it to counter as you loop through the GuessedLetters() array.
  • ZootZoot Posts: 2,227
    edited 2012-10-19 16:44
    e.g.
    FOR counter = 0 TO 25
      LOOKUP counter, ["abcdefghijklmnopqrstuvwxyz"],result
      DEBUG "Guessed ", result, "? "
      LOOKUP GuessedLetters(counter), [ "N", "Y" ], result
      DEBUG result, CR
    NEXT
    

    For the input, you could "translate" the letter input to a bit position:
    DEBUG "Guess a letter: "
    DEBUGIN result
    LOOKDOWN result, [ "AaBbCcDdEe" ], counter ' you need to do all the letters through Z; I only did A-e
    counter = counter >> 1 ' / 2 because of case
    IF GuessedLetter(counter) = 1 THEN
       DEBUG "You already guessed ", result, CR
    ELSE 
       GuessedLetter(counter) = 1 ' set it as guessed!
       DEBUG "Good guess!", CR
       ' do some subroutine(s) to draw next part of hangman
    ENDIF
    
  • RLmiller777RLmiller777 Posts: 9
    edited 2012-10-20 09:25
    wow. thank you very much Saphire and Zoot. i will be studiing that for a while.
Sign In or Register to comment.