Re: C64 BASIC question



On 31 Mar 2006 04:22:08 -0800, arcanica@xxxxxxxxxxxxx wrote:

I am writing a small game to get more aquainted with c64 basic. I'm not
completely savvy with the inner workings of the interpreter but there
is something that boggles me.

In part of my program i need to randomize a list of numbers without
duplicates...

It's been pointed out that your NEXT statement at line 110 of the
second program sample will never be executed; it's blocked by the THEN
(GOTO) just ahead of it. In effect, your have a FOR at line 101 that
has no corresponding NEXT.

Couple of quick general comments:

--With the C64, you have lots of memory to allow you to put in the
spaces. Makes your code more readable, and helps you escape some
fairly subtle errors. Sometimes, I tell a group that the line:

IF J=T AND Y<10 THEN SAY "YES"
... will work fine, but the line:
IFJ=TANDY<10THENSAY"YES"
....bombs out with a ?SYNTAX error. Same lines, the only difference
is the spaces. (And I add the gag that no Commodore machine will
accept a line containing the word TANDY). :-)

--No matter how many characters you use for a variable name, C64 Basic
will use only the first two. So USED% is really seen as US%. No
problem if you feel it helps readability, but understand that the
computer will see USED% and USHER% as the same variable.

--Integer varaiables save space but cost speed. That makes sense for
arrays, where you might want to save quite a bit of memory. But
consder replacing such one-time variables as N% and E% with just plain
N and E.

--As it happens, you're in no dange of getting an out-of-memory
warning here (too many active loops could cause an out-of-stack) ...
any time you open a loop with the same variable name as a previous
one, the stack is cleaned of the old data. So line 140's FOR_C would
close both the C loop at 101, and the inner loop at 110, if they
hadn't been properly closed by going through the whole NEXT range.
It's received knowledge that you should not jump out of a loop, but
you can get away with it if you understand about cleaning up the stack
this way.

--Trivial, but all variables and arrays contain zero before they are
used. It doesn't hurt to zero them explicity, as you do, but they do
start that way in any case.

--As I see it, you're putting random values ranging from 1 to 15 into
an array of 15 slots. That's a little like shuffling a deck of cards,
where you want each number to appear once and once only. Your method
will work, but it's a little slow as it gets to the fifteenth number
and keeps trying for random values until it finally stumbles on the
remaing one that fits. There are several ways of a doing a shuffle
more efficiently. My favourite is this: fill the array with the
number from 1 to 15. Now: for each item in the array, randomly pick
another spot in the array, and then swap the two values.

I'd be happy to put in code for this, but I suspect that you want to
take a shot at it yourself. Have fun ...

--Jim Butterfield


----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
.


Quantcast