Post by parzival on Jan 14, 2021 23:58:05 GMT
Hello, all. New to the boards and to programming for the C64 (“maxi’) and already I have a question.
I’m working on a Text Adventure game based on designs I did long ago, and I’ve run into a wrinkle.
As far as I can tell, there’s no straight way to compare string variables in C64 BASIC— for example IF A$ = B$ THEN ? A$ will return a type mismatch error. Yes, one can compare a string variable directly to a text string— IF A$ = “Yes” THEN ? A$. But if one has a long list of string variables, such as a dimensioned array like A$(x), there’s no easy way to compare other string variables to determine if one is a match.
For example, let’s say you have a simple list of items for an inventory. The user is given an input prompt to search for an item in the inventory— for example “baseball”. Normally, I’d write the following code:
10 FOR X = 1 TO Y: REM Y is the total number of items in inventory; let’s assume it’s been established, and that the INPUT line has been used as well.
20 IF SEARCH$ = A$(X) THEN PRINT “Yes, we have a “; A$(X): END: REM Or this can branch to another line or subroutine...
30 NEXT X
40 ? “No, we do not have a “;SEARCH$: REM Yes, a C64 really only allows for two-letter variables, but SEARCH$ is clearer here.
50 END
Not a great bit of code, but it gets the point across. The user enters a word and the program searches to see if that word matches an element of the array. Fairly common code, I would think. Apparently, that’s not possible in C64 BASIC? Am I missing something, or is there a better work around that painstakingly coding each item as straight text, as in:
IF SEARCH$=“bat” THEN ? “Yes, we have a bat.”
IF SEARCH$=“baseball THEN ? “Yes, we have a baseball.”
And so on...
(I note that as an inventory system that would really suck, as you’d have to enter a new line of code every time you added an item to inventory...)
I appreciate any help!
I’m working on a Text Adventure game based on designs I did long ago, and I’ve run into a wrinkle.
As far as I can tell, there’s no straight way to compare string variables in C64 BASIC— for example IF A$ = B$ THEN ? A$ will return a type mismatch error. Yes, one can compare a string variable directly to a text string— IF A$ = “Yes” THEN ? A$. But if one has a long list of string variables, such as a dimensioned array like A$(x), there’s no easy way to compare other string variables to determine if one is a match.
For example, let’s say you have a simple list of items for an inventory. The user is given an input prompt to search for an item in the inventory— for example “baseball”. Normally, I’d write the following code:
10 FOR X = 1 TO Y: REM Y is the total number of items in inventory; let’s assume it’s been established, and that the INPUT line has been used as well.
20 IF SEARCH$ = A$(X) THEN PRINT “Yes, we have a “; A$(X): END: REM Or this can branch to another line or subroutine...
30 NEXT X
40 ? “No, we do not have a “;SEARCH$: REM Yes, a C64 really only allows for two-letter variables, but SEARCH$ is clearer here.
50 END
Not a great bit of code, but it gets the point across. The user enters a word and the program searches to see if that word matches an element of the array. Fairly common code, I would think. Apparently, that’s not possible in C64 BASIC? Am I missing something, or is there a better work around that painstakingly coding each item as straight text, as in:
IF SEARCH$=“bat” THEN ? “Yes, we have a bat.”
IF SEARCH$=“baseball THEN ? “Yes, we have a baseball.”
And so on...
(I note that as an inventory system that would really suck, as you’d have to enter a new line of code every time you added an item to inventory...)
I appreciate any help!