Another variable/array question

5 replies [Last post]
Offline
Joined: Sep 4 2014
Posts: 21

Hi. I've been trying to work out a way to reduce code in Applesoft.

E = user's choice
GL = amount of current gold
P(1) = price of item 1
K = an error check. K is set to 1 (K=1) when an invalid choice has been determined (“you can't afford/use it”). User is GOTOed to input new choice. Elsewise, K reset to 0, continue.
S = category identifier set at items list section S=1 (shields, line 750), S=2 (armour, line 900) or S=3(weapons, line 1200).

Here's what does work:

10 IF E = 1 AND GL < P(1) THEN GOSUB [PRINT “YOU CAN'T AFFORD IT"]: GOTO [ENTER NEW CHOICE]
20 IF E = 2 AND GL < P(2) THEN GOSUB [“YOU CAN'T...”]: GOTO [NEW CHOICE]
30 IF E = 3 AND GL < P(3) THEN GOSUB [“YOU CAN'T...”]: GOTO [NEW CHOICE]
40 IF E = 4 AND GL < P(4) THEN GOSUB [“YOU CAN'T...”]: GOTO [NEW CHOICE]
50 IF E = 5 AND GL < P(5) THEN GOSUB [“YOU CAN'T...”]: GOTO [NEW CHOICE]

I tried various ways of reducing the code above. This is what I currently have.

90 FOR A = 1 TO 5: IF E = A AND GL < P(A) THEN 100: NEXT
95 RETURN (to purchase?).
100 PRINT “YOU CAN'T AFFORD IT.”: FOR T = 1 TO 3500: NEXT: K = 1: RETURN

Running my program, if I enter choice 1 where the price should be 51 although I have 153 I get the “you can't afford it message.” If I interrupt the program (Ctr-C) at that point I get these values:
E = 1, P(A) = 0, GL = 153, P(1) = 51

P(A) seems to be the problem.

The issue is that I currently have 33 price error checks as IF/THEN statements. Surely, there must be a short routine that upon the user making a selection compares a variable from an array to a set variable to determine whether the array variable is less than a set variable. I just can't figure it out. Any help?

__________________

"You can be in my dream if I can be in yours" / Bob Dylan

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Offline
Joined: Apr 11 2006
Posts: 1013
Re: Another variable/array question

RockbadgerX wrote:

90 FOR A = 1 TO 5: IF E = A AND GL < P(A) THEN 100: NEXT
95 RETURN (to purchase?).
100 PRINT “YOU CAN'T AFFORD IT.”: FOR T = 1 TO 3500: NEXT: K = 1: RETURN

Running my program, if I enter choice 1 where the price should be 51 although I have 153 I get the “you can't afford it message.” If I interrupt the program (Ctr-C) at that point I get these values:
E = 1, P(A) = 0, GL = 153, P(1) = 51

P(A) seems to be the problem.

Well, maybe - but line 90 certainly is a problem. Your NEXT is hiding behind the THEN clause. It might do more of what you want if you spread those separate items out on their respective line numbers:

90 FOR A = 1 TO 5: IF E = A AND GL < P(A) THEN 100
91 NEXT

But... I'm not sure why you're doing so much checking. All you need to know is: do they have enough gold for what they picked? E is your index into the array of prices. If they picked #3, and P(3) is 51, and you have 153 gold... then they're good to go. Why not just use this as the decision maker:

IF GL < P(E) THEN GOTO fail message and retry
REM Else test passed - money to purchase item #E exists

Offline
Joined: Sep 4 2014
Posts: 21
Re: Another variable/array question

david__schmidt wrote:

IF GL < P(E) THEN GOTO fail message and retry
REM Else test passed - money to purchase item #E exists

Hmm, painfully obvious. Thanks. Like I said in an earlier post my coding is cumbersome! May I also ask about greater/less-than statements. Statements like the following never seem to work for me (just going from memory here):

IF A = 10 AND B$ <> "R" THEN...
or
IF A = 10 AND A$<> "S" OR B = 20 AND B$ <> "R" THEN...

These never seem to work for me.

Er, at while I'm at it (again going from memory) I've noticed statements in other programs that appear to be using % instead of $. Something like:
IF A%(A$,4) = "WORD" THEN...
or
W%(4)=W%(4)+WP

What is the percentage sign doing?

__________________

"You can be in my dream if I can be in yours" / Bob Dylan

resman's picture
Offline
Joined: Feb 9 2006
Posts: 213
Re: Another variable/array question

Also, if you find yourself testing for a linear range of numbers, you could use the ON .. GOSUB ... construct: http://www.calormen.com/jsbasic/reference.htm

Something to keep in your toolbox.

Dave...

RockbadgerX wrote:

Hi. I've been trying to work out a way to reduce code in Applesoft.

E = user's choice
GL = amount of current gold
P(1) = price of item 1
K = an error check. K is set to 1 (K=1) when an invalid choice has been determined (“you can't afford/use it”). User is GOTOed to input new choice. Elsewise, K reset to 0, continue.
S = category identifier set at items list section S=1 (shields, line 750), S=2 (armour, line 900) or S=3(weapons, line 1200).

Here's what does work:

10 IF E = 1 AND GL < P(1) THEN GOSUB [PRINT “YOU CAN'T AFFORD IT"]: GOTO [ENTER NEW CHOICE]
20 IF E = 2 AND GL < P(2) THEN GOSUB [“YOU CAN'T...”]: GOTO [NEW CHOICE]
30 IF E = 3 AND GL < P(3) THEN GOSUB [“YOU CAN'T...”]: GOTO [NEW CHOICE]
40 IF E = 4 AND GL < P(4) THEN GOSUB [“YOU CAN'T...”]: GOTO [NEW CHOICE]
50 IF E = 5 AND GL < P(5) THEN GOSUB [“YOU CAN'T...”]: GOTO [NEW CHOICE]

I tried various ways of reducing the code above. This is what I currently have.

90 FOR A = 1 TO 5: IF E = A AND GL < P(A) THEN 100: NEXT
95 RETURN (to purchase?).
100 PRINT “YOU CAN'T AFFORD IT.”: FOR T = 1 TO 3500: NEXT: K = 1: RETURN

Running my program, if I enter choice 1 where the price should be 51 although I have 153 I get the “you can't afford it message.” If I interrupt the program (Ctr-C) at that point I get these values:
E = 1, P(A) = 0, GL = 153, P(1) = 51

P(A) seems to be the problem.

The issue is that I currently have 33 price error checks as IF/THEN statements. Surely, there must be a short routine that upon the user making a selection compares a variable from an array to a set variable to determine whether the array variable is less than a set variable. I just can't figure it out. Any help?

Offline
Joined: Jun 20 2014
Posts: 234
Re: Another variable/array question

RockbadgerX wrote:

IF A = 10 AND B$ <> "R" THEN...
or
IF A = 10 AND A$<> "S" OR B = 20 AND B$ <> "R" THEN...

These never seem to work for me.

That is really odd. Can you create an example we can run? The only reason they would not work is if the values being tested are not in the ranges expected.

RockbadgerX wrote:

Er, at while I'm at it (again going from memory) I've noticed statements in other programs that appear to be using % instead of $. Something like:
IF A%(A$,4) = "WORD" THEN...
or
W%(4)=W%(4)+WP

What is the percentage sign doing?

Some forms of BASIC have different numerical types. A '%' usually refers to an integer value rather than a floating point value. In Applesoft we only have one numerical type, floating point. Integer BASIC, on the other hand, only has integers.

VAX BASIC, probably the most advanced BASIC ever created, has the following 12 numeric types: BYTE, WORD, LONG, QUAD, SINGLE, DOUBLE, GFLOAT, HFLOAT, SFLOAT, TFLOAT, XFLOAT and DECIMAL. The first 4 are integer (8 bits to 64 bits), the next 7 are floating point (32 bits to 128 bits) and the last one is a definable fixed point decimal (variable size 8 to 256 bits). There is a 13th type, but it is only for use in file access and not for computation.

__________________

Bill

6502 forever!

Offline
Joined: Apr 11 2006
Posts: 1013
Re: Another variable/array question

BillO wrote:
RockbadgerX wrote:

Er, at while I'm at it (again going from memory) I've noticed statements in other programs that appear to be using % instead of $. Something like:
IF A%(A$,4) = "WORD" THEN...
or
W%(4)=W%(4)+WP

What is the percentage sign doing?

Some forms of BASIC have different numerical types. A '%' usually refers to an integer value rather than a floating point value. In Applesoft we only have one numerical type, floating point. Integer BASIC, on the other hand, only has integers.

Applesoft (Floating Point) does in fact use the '%' to indicate and store an integer value. Try it:

]A%=3.1415
]PRINT A%
3

Integer variables use quite a bit less memory than floating point variables; this is especially true in matrices, where you have a pile of values sitting around. If you really only need integers, it's smaller. Faster is debatable - there are some operations where ints are internally converted to floats and back again.