Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ' {$STAMP BS2}
- ' {$PBASIC 2.5}
- Speaker PIN 4
- MAX_INPUT CON 10
- sInputBuff VAR Byte(MAX_INPUT) 'input buffer
- byKeyIn VAR Byte 'Key read from terminal
- byCurPos VAR Byte 'cursor position/input length
- byLinePos VAR Byte 'position in line we're printing
- byCharIn VAR byKeyIn 'Reuse KeyIn for reading strings to output
- wStrPtr VAR Word 'pointer to string
- bOutType VAR Bit 'what we'll print, 0 = room description, 1 = other
- nDescription VAR Nib 'Description to type
- nCommand VAR nDescription 'comand to compare against
- nRoom VAR Nib 'Stores room we're in
- '---- Room Descriptions ----'
- Room1 DATA "You find yourself in a dimly lit dungeon, exits are NORTH, SOUTH and WEST.", 0
- Room2 DATA "North room"
- '---- Other Descriptions ----'
- UnknownCommand DATA "I don't know what you're saying...", 0
- DSC_NOT_COMMAND CON 0
- DO
- byCurPos = 0 'reset input length to 0
- GOSUB ReadInput
- GOSUB CheckInput
- LOOP
- END
- 'Command strings
- CmdLook DATA "LOOK", 0
- 'and lookup constants
- CMD_LOOK CON 0
- CMD_NONE CON 1 'always must be last
- bFoundMatch VAR Bit
- CheckInput:
- nCommand = CMD_LOOK
- GOSUB CompareStrings
- SELECT nCommand
- CASE CMD_LOOK
- bOutType = 0 'print room
- CASE CMD_NONE
- nDescription = DSC_NOT_COMMAND
- bOutType = 1
- ENDSELECT
- GOSUB TypeDescription
- RETURN
- CompareStrings:
- DO
- bFoundMatch = 1 'all are match until otherwise proven
- LOOKUP nCommand, [CmdLook], wStrPtr
- FOR byLinePos = 0 TO byCurPos - 1
- READ wStrPtr + byLinePos, byCharIn
- IF byCharIn <> sInputBuff(byLinePos + 1) THEN
- bFoundMatch = 0 'not a match
- nCommand = nCommand + 1 'move to next one
- EXIT
- ENDIF
- NEXT
- IF bFoundMatch THEN RETURN
- LOOP UNTIL nCommand = CMD_NONE
- RETURN
- TypeDescription:
- byLinePos = 0 'start at begin of line
- IF bOutType = 0 THEN 'Room description
- LOOKUP (nRoom), [Room1, Room2], wStrPtr
- ELSE 'Describe something else
- SELECT nDescription
- CASE DSC_NOT_COMMAND
- wStrPtr = UnknownCommand
- ENDSELECT
- ENDIF
- DO
- READ wStrPtr, byCharIn
- IF byCharIn <> 0 THEN
- DEBUG byCharIn
- ENDIF
- wStrPtr = wStrPtr + 1
- LOOP UNTIL byCharIn = 0
- DEBUG 13, ">"
- RETURN
- ReadInput:
- DEBUGIN byKeyIn 'read key in
- IF byKeyIn = 8 THEN 'backspace
- sInputBuff(byCurPos) = 0
- IF byCurPos > 0 THEN
- byCurPos = byCurPos - 1
- ELSE
- DEBUG ">"
- ENDIF
- GOTO ReadInput
- ELSEIF byKeyIn = 13 THEN 'return
- RETURN
- ENDIF
- 'make sure input doesn't overflow
- IF byCurPos < MAX_INPUT THEN
- byCurPos = byCurPos + 1
- ELSE 'hit end of input buffer!
- DEBUG 8 'backspace
- FREQOUT Speaker, 125, 3000 'beep
- GOTO ReadInput 'Go back to input
- ENDIF
- 'Do stuff based on char read
- SELECT byKeyIn
- CASE "a" TO "z"
- sInputBuff(byCurPos) = (byKeyIn - 32) 'convert to upper case
- CASE "A" TO "Z"
- sInputBuff(byCurPos) = byKeyIn
- CASE ELSE
- DEBUG 8 'don't accept non-text characters
- FREQOUT Speaker, 125, 3000 'beep
- ENDSELECT
- GOTO ReadInput 'loop until return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement