Select Case problem

in BASIC Stamp
Does anyone know how to use Debug/Select/Case commands using words and sentences from keyboard ? Appreciate the
help.
help.
Comments
' {$STAMP BS2} ' {$PBASIC 2.5} KW1 DATA "Einie", 0 KW2 DATA "Minie", 0 KW3 DATA "Miney", 0 KW4 DATA "Moe",0 NmbrKW CON 4 'number of keywords to look for KWPoint VAR Nib 'pointer to current keyword InStrng VAR Byte(8) 'string from debugin DBPoint VAR Word 'pointer to beginning of current trial DByte VAR Byte 'string element from the data list CByte VAR Byte 'string element from Debugin X1 VAR Nib 'pointer into current strings begin: DEBUG CLS, "Please type a keyword.", CR DEBUGIN STR InStrng \7 \CR 'input up to 7 characters, terminated with CR GOSUB LookItUp 'returns 1-NmbrKW if found, zero if not found SELECT KWPoint 'dispatch accordingly CASE 1 DEBUG "you input 'einie'", CR CASE 2 DEBUG "you input minie", CR CASE 3 DEBUG "Case Three", CR CASE 4 DEBUG "Case 4", CR CASE ELSE DEBUG "Keyword not recognized.", CR ENDSELECT PAUSE 2000 GOTO begin LookItUp: FOR KwPoint = 1 TO NmbrKW 'search one keyword at a time LOOKUP (KwPoint-1), [KW1, KW2, KW3, KW4], DBPoint 'get pointer to current trial keyword X1 = 0 'set offset to beginning LookMore: READ (DBPoint + X1), DByte 'get current comparand DByte = DByte | $20 'force lower case CByte = InStrng(X1) | $20 'get byte from in, lower case ' DEBUG CR, DEC KWPoint, " ", DEC x1, " ", DByte, " ", CByte 'diagnostic IF (DByte <> CByte) THEN NoMatch 'see if this byte of this kw matches IF (DByte = $20) THEN FullMatch 'yes, we got to terminator X1 = X1 + 1 'to next byte in each GOTO LookMore 'continue with current keyword NoMatch: NEXT 'no match, try next keyword KWPoint = 0 'no next keyword to try RETURN 'failure exit FullMatch: RETURN 'KWPoint points to keyword that matched
left 108*2+ 101*3+ 102*5+ 116*7 =1841
right 114*2+ 105*3+ 103*5+ 104*7+ 116*11 =3062
up 117*2+ 112*3 =570
down 100*2+ 111*3+ 119*5+ 110*7 =1898
By the "chinese remainder theorem", the values computed will be unique. Subsequently when you input chars from the keyboard after the prompt, sum up with the same math until you get a CR, then look for a numerical match.
LOOKUP x,[2,3,5,7,9,11,13],y
sum = Dbyte * y + sum
For a small set of key words it could work by simply adding up the ascii codes, if it turns out that the sums are unique anyway.
sum = Dbyte + sum
Edit: Tracy, I sent you a PM regarding this thread. EndEdit
' {$STAMP BS2} ' {$PBASIC 2.5} InStrng VAR Byte(10) 'string from debugin DByte VAR Byte 'string element from InStrng X1 VAR Nib 'pointer into InStrng Value VAR Word 'sum corresponding to contents of Instrng Temp VAR Byte 'generally useful begin: DEBUG "Please type a keyword.", CR DEBUGIN STR InStrng \9 \CR 'input up to 7 characters, terminated with CR GOSUB Calc 'returns code corresponding to InStrng Temp = 255 'not found indicator LOOKDOWN Value, [1841, 3062, 570, 1898, 6189, 1731, 8293], Temp 'left right up down forward back toandfro SELECT Temp 'dispatch accordingly CASE 0 DEBUG "LEFT", CR CASE 1 DEBUG "RIGHT", CR CASE 2 DEBUG "UP", CR CASE 3 DEBUG "DOWN", CR CASE 4 DEBUG "FORWARD", CR CASE 5 DEBUG "BACK", CR CASE 6 DEBUG "TOANDFRO", CR CASE 255 DEBUG "Keyword not recognized. ", DEC value, CR 'To add a keyword, you must put an additional entry in the LOOKDOWN command 'and a put an additional CASE in the SELECT/CASE command. You could also 'replace current entries in the lookdown and select/case. CASE ELSE DEBUG "This program is broken.", CR ENDSELECT PAUSE 2000 GOTO begin Calc: Value = 0 X1 = 0 Calc1: DByte = InStrng(X1) 'get first (next) character IF (DByte = 0) THEN RETURN 'termination indicator LOOKUP X1, [2,3,5,7,11,13,17,19,23], Temp 'get the prime number Value = Value + ((DByte | $20) * Temp) 'accumulate (forcing lower case) X1 = X1 + 1 'point to next GOTO Calc1
The method or one like it does works though for a small set of words, just so long as the words map to unique numerical values.
' {$STAMP BS2} ' {$PBASIC 2.5} InStrng VAR Byte(10) 'string from debugin DByte VAR Byte 'string element from InStrng X1 VAR Nib 'pointer into InStrng Value VAR Word 'sum corresponding to contents of Instrng Temp VAR Byte 'generally useful begin: DEBUG "Please type a keyword.", CR DEBUGIN STR InStrng \9 \CR 'input up to 7 characters, terminated with CR GOSUB Calc 'returns code corresponding to InStrng SELECT Value 'dispatch accordingly CASE 1841 DEBUG "LEFT", CR CASE 3062 DEBUG "RIGHT", CR CASE 570 DEBUG "UP", CR CASE 1898 DEBUG "DOWN", CR CASE 6189 DEBUG "FORWARD", CR CASE 1731 DEBUG "BACK", CR CASE 8293 DEBUG "TOANDFRO", CR CASE ELSE DEBUG "Keyword not recognized. ", DEC value, CR 'To add a keyword, you can simply add another CASE (or replace one). ENDSELECT PAUSE 2000 GOTO begin Calc: Value = 0 X1 = 0 Calc1: DByte = InStrng(X1) 'get first (next) character IF (DByte = 0) THEN RETURN 'termination indicator LOOKUP X1, [2,3,5,7,11,13,17,19,23], Temp 'get the prime number Value = Value + ((DByte | $20) * Temp) 'accumulate (forcing lower case) X1 = X1 + 1 'point to next GOTO Calc1
Sometimes you don't see the forest for the trees.
brachiosaurus: ' come here as a GOSUB with Value Temp = 255 LOOKDOWN Value, [1841, 3062, 570, 1898, 6189, 1731, 8293], Temp BRANCH Temp[left, right, up, down, forward, back, toandfro] RETURN ' not found, temp=255 left: DEBUG "LEFT", CR RETURN ' etc., separate subroutine for each action with RETURN