Another variable/array question

6 posts / 0 new
Last post
Offline
Last seen: 9 years 5 months ago
Joined: Sep 3 2014 - 21:33
Posts: 21
Another variable/array question

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
Last seen: 8 years 11 months ago
Joined: Apr 10 2006 - 20:01
Posts: 1013
Re: Another variable/array question

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
Last seen: 9 years 5 months ago
Joined: Sep 3 2014 - 21:33
Posts: 21
Re: Another variable/array question

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?

resman's picture
Offline
Last seen: 7 months 2 weeks ago
Joined: Feb 9 2006 - 12:41
Posts: 217
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...

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?

BillO's picture
Offline
Last seen: 4 years 4 months ago
Joined: Jun 20 2014 - 18:03
Posts: 258
Re: Another variable/array question


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.


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.

Offline
Last seen: 8 years 11 months ago
Joined: Apr 10 2006 - 20:01
Posts: 1013
Re: Another variable/array question

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.

Log in or register to post comments