O.K. Jeff - here goes:Jeff-20 wrote:Feel free to add your programs here. I'd love to have a selection of contemporary games to share with the community.
After I bought my VIC, I began to realize that in order to control the outside world, I would need to learn how to program.
Getting something to work on the screen would be good as that afforded me feedback rather quickly. I found that the screen -
the working area of the screen at least - was made up of 506 blocks (22 columns across by 23 rows down) and that each
block had it's own memory address. This was good. I could run a string of asterisks (*) across the screen, dividing it in to two
playing areas for a simple game of Black Jack - on area to play the cards and the other area for communications between me
and the computer. All four suits were already on the keys and so were the different colors. Just to get the game going, I
decided I didn't need any fancy looking cards like some of the commercial programs had. Everything at this point was looking
good. Then I went looking for a gaming program. Oh Boy ! ! ! THAT was not looking good at all!! Everyone of the programs
I looked at had the same problem - Duplicate Cards!!! This can *definitely* ruin your day in the middle of a game. So there
was my challenge - shuffle a deck and have no duplicate cards. O.K. - suppose I assign a number to each card, put the cards
in memory, and let the computer scramble the numbers - a job the computer is built for! What I came up with is the following:
10 print"[shift][clear/home]": dim A(52): for B=1 to 52
20 C=int(rnd(1) * 52) + 1
30 for D=1 to B: if A(D)=C then D=B: goto 20
40 if A(D)=0 then A(D)=C
50 next: next
This worked!! Everything's scrambled and no duplicate numbers!! Yea!! It just took about a minute and a half (about 90
seconds!!!!) to do it! After massaging this program a little bit, I discovered that most of the time was used in the last
two numbers. So, I tried the following: adding the numbers 1 through 52 gave the result of 1378. If I subtracted the total of
the first 51 numbers from 1378, I always wound up with the missing number. So, I modified the program as follows:
10 print"[shift][clr/home]": dim A(52): for B=1 to 51
20 C=int(rnd(1) * 52)+ 1
30 for D=1 to B: if A(D)=C then D=B: goto 20
40 if A(D)=0 then A(D)=C: E=E + C
50 next: next: F= 1378 - E: A(52)=F: E=0
This worked!! Time is now down to about 45 seconds - I could live with that.
Well (I know - deep subject!), I gotta go now. I have dinner date, so, I'll be back in a little while to explain things.
I'm back. Now the explanation. Line 10 clears the screen so that I can set up my playing field; sets apart 52 places in
memory (this is called an 'array') by way of the 'dim' (dimension) statement (because I've exceeded 11 places
which the VIC handles automatically) and labels it 'A'. 'B' is a counter used to check each position in 'A' for a duplicate
number or empty space. 'C' is a randomly generated number (the '1' in 'rnd(1)' is the 'seed' that produces the least
amount of predictable sequences. "52" gives the range of numbers from 0 to 51 and the '+ 1' adjusts the range to 1 to 52.
(By the way, a good tutorial on random numbers can be found right here at 'denial' - see "home page", "links", "related
resources", "compute magazine archive", "compute #8 - Jan. 81: random numbers part #1"; "compute #9 - Feb 81: random
numbers part #2"; "compute - Mar 81: part #3"; and "compute - Apr 81: part #4"; and also VIC-20 Programer's Guide).
'D' is a loop that sets the limits for checking 'A'. The 'if-then' statements do the actual checking for duplicate numbers or
empty spaces. If a duplicate number is found then 'D' is max'd out and another number is generated and the checking
starts again. If no duplicate number is found by the time the last space is addressed (per each iteration of loop 'D'), then
the new number is placed in the empty space and the face value of the number is added to counter 'E'. Then 'D' is up dated
(1st 'next') followed by 'B' being up dated (2nd 'next'). Every time 'B' is up dated by one, 'D''s limits are increased by one
- so, the search continues. 'F' is the 52nd number which is found by subtracting 'E' from 1378. 'F' is then placed into
space #52 of 'A'. Since that completes the shuffle and both 'B' and 'D' are max'd out, 'E' is cleared (0) and the program is
thus ready for another shuffle command.
I hope this makes sense to somebody out there. Maybe you can improve on this. Hope to hear from you.
Phil Potter.