Category Archives: Color BASIC

UnderColor’s spiral challenge from 1984 – part 3

ALERT! ALERT! We are doing this wrong. It has been pointed out in a comment to an earlier installment that we missed an important part about what this contest was supposed to produce!

More on that in a moment… But first, let’s look at a faster version of the challenge, created by Dillon Teagan:

10 TIMER=0
20 PMODE4,1:PCLS1:SCREEN1,1
30 L=&HFF:I=-3:DIMR,L,U,D
40 D$="D=D;R=R;U=U;L=L;
50 DRAW"BM0,191C0R=L;U191L=L;
60 FORD=188TO3STEP-6:R=L+I:U=D+I:L=R+I:DRAWD$:NEXT
70 PRINTTIMER/60

This one clocks in at 2.7 seconds, and does some wonderful optimizations!

First, Dillon clearly understands how the BASIC interpreter works. He is doing things like using hex &HFF instead of decimal 255 which makes that bit parse a tad faster. Next, you see him declare a variable, followed by a DIM which pre-declared R, L, U and D. In this example, that DIM probably does not help, but in a real program, you can declare your variables up front and do them in the order of “most accessed” to least. This sets their order in the variable table, so when you try to access one, the one you access the most can be at the top of the list. I’ve posted about this before, but consider this:

10 DIM A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z
20 PRINT Z
30 Z=Z+1
40 IF Z<100 THEN 20

If we truly did have 26 variables in use, and declared them like this, Z would be at the end of the variable table. EVERY time Z is needed, BASIC has to scan through all 26 variables trying to match Z so it can be used. This would be MUCH slower than, if you knew Z was being used the most often, you did this:

10 DIM Z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y
20 PRINT Z
30 Z=Z+1
40 IF Z<100 THEN 20

With that one minor change (declaring Z first), Z is now the first variable in the table and will be found much quicker. Try it sometime.

But I digress…

The next cool optimization Dillon does is by drawing the initial bottom line (bottom left to bottom right, 256 pixels wide) and right side (bottom right to top right, 192 pixels tall) in line 50, along with the first left (top right to top left, 256 pixels wide) before entering the loop.

The loop itself is using a FOR/NEXT loop which is faster than using GOTO since no line scanning has to be done. BASIC stores where the FOR is, and when NEXT is encountered it pops back to that location rather than scanning forward to find the target line, or starting at the first line and scanning forward from there. Nice.

With the first three “full width/height” lines out of the way, the loop is just doing the “minus 3” size of all four lines. That’s clever. The entire draw string is in a string (D$) and I am unsure if this speeds things up versus having it directly in the line itself (line 60).

Impressive. I wish I’d thought of that!

However… we have been doing it wrong, so far, it seems.

We’ve been doing it wrong so far, it seems.

In a comment left on part 1 of this series, Paul Fiscarelli pointed out something that I completely got wrong:

Hello Allen – I sent you a DM on FB, but I don’t think you’ve seen it yet. What you have posted above is not exactly an accurate reproduction of what is being asked in the challenge question. You are using an offset decrease of 3 in each of your iterations, which produces a gap of only 2-pixels in both height and width. The challenge is indicating a gap of 3-pixels between lines, which requires an offset decrease of 4 in each iteration. This is further evident in the challenge’s diagram of the spiral, which indicates a line-length of 188-pixels for the line on the far left-hand side of the screen. If you perform a screen grab in XRoar (pixel perfect geometry of 640×480 window and 512×384 screen resolution), you will find your code generates a line length of 189 pixels (scale the 512×384 in half).

If you change the offset decrease in your code to 4 instead of 3, you will achieve a render time of roughly 2.43 seconds. This is due to the fact that you are rendering about 23% fewer lines with the DRAW statement.

You can reduce this time even further if you were to use only a single offset variable for drawing both the horizontal and vertical lines, and by adding a separate width to the horizontal lines with a value of 64 = (256w – 192v). This method will shave roughly 0.10 seconds off your render time, down to approximately 2.33 seconds.

10 TIMER=0
20 PMODE4,1:PCLS:SCREEN1,1
30 OF=191:DRAW”BM0,191″
40 DRAW”R=OF;R64″
50 DRAW”U=OF;L=OF;L64;”
60 OF=OF-4
70 DRAW”D=OF;R=OF;R64;”
80 OF=OF-4
90 IF OF>0 THEN 50
100 TM=TIMER
110 IF INKEY$=”” THEN 110
120 PRINT TM/60

As an additional optimization step, you can replace the IF/THEN boundary check with a FOR/NEXT loop, to shave another 0.05 seconds from your render time – getting it down to roughly 2.28 seconds.

10 TIMER=0
20 PMODE4,1:PCLS:SCREEN1,1
30 OF=191:DRAW”BM0,191″
40 DRAW”R=OF;R64″
50 FOR N=0 TO 23
60 DRAW”U=OF;L=OF;L64;”
70 OF=OF-4
80 DRAW”D=OF;R=OF;R64;”
90 OF=OF-4
100 NEXT
110 TM=TIMER
120 IF INKEY$=”” THEN 120
130 PRINT TM/60

There are probably some other optimizations that can be done here – but it’s a start. Oh, I also tested these examples with VCC 2.1.9.2-pre2. I’m sure there will be slight variations in timing with the different emulators, and probably even with different versions of VCC.

Paul Fiscarelli

So, uh … oops. It seems obvious now:

Let’s regroup, and take a look at Paul’s versions in the next installment.

Until then… RTFM!

UnderColor’s spiral challenge from 1984 – part 2

See Also: part 1, part 2, with part 3 and part 4 coming (and maybe more).

These types of posts are probably my favorite. Someone posts something with a coding challenge, and folks start submitting their ideas, and I get to play collector and just repost without doing any work… (Well, except I am submitting ideas as well, this time.)

This all kicked off when Michael Pittsley shared a contest he found in a 1984 issue of UnderColor magazine that challenged BASIC programmers to draw a specific spiral pattern:

Michael tackled it with this code:

10 ' SPIRAL THING
20 '
30 XL=3:YT=0:XR=252:YB=188:PX=3
40 PMODE 4,1
50 PCLS
60 SCREEN 1,1
65 LINE (XL,YB) - (XR-PX,YB),PSET
200 ' RIGHT
210 XR=XR-PX
220 LINE - (XR,YB),PSET
300 ' UP
306 YT=YT+PX
307 IF YT=96 THEN 600
310 LINE - (XR,YT), PSET
400 ' LEFT
410 XL=XL+PX
420 LINE - (XL,YT),PSET
500 ' DOWN
510 YB=YB-PX
520 LINE - (XL,YB),PSET
550 GOTO 200
600 GOTO 600

A nicely formatted and documented (with REMmarks) example of the power BASIC. He was even aware that LINE does not need both a start and end point each time you use it. If you just do “LINE-(x,y),PSET” it will draw from the last point to the new coordinates. This greatly reduces the amount of parsing BASIC has to do versus if you wrote the program always having a pair of start/end points.

Let’s modify it with some timing stuff.

10 ' SPIRAL THING
20 '
25 TIMER=0
30 XL=3:YT=0:XR=252:YB=188:PX=3
40 PMODE 4,1
50 PCLS
60 SCREEN 1,1
65 LINE (XL,YB) - (XR-PX,YB),PSET
200 ' RIGHT
210 XR=XR-PX
220 LINE - (XR,YB),PSET
300 ' UP
306 YT=YT+PX
307 IF YT=96 THEN 600
310 LINE - (XR,YT), PSET
400 ' LEFT
410 XL=XL+PX
420 LINE - (XL,YT),PSET
500 ' DOWN
510 YB=YB-PX
520 LINE - (XL,YB),PSET
550 GOTO 200
600 PRINT TIMER/60

Running it reports 3.16666667 seconds at the end, so really close to the time my version using DRAW got. AND, that is with REMs and spaces and multiple lines to parse. Let’s see what tricks we can do to speed it up by removing spaces, REMs, and packing lines together:

25 TIMER=0:XL=3:YT=0:XR=252:YB=188:PX=3::PMODE4,1:PCLS:SCREEN1,1:LINE(XL,YB)-(XR-PX,YB),PSET
200 XR=XR-PX:LINE-(XR,YB),PSET:YT=YT+PX:IFYT=96THEN600
310 LINE-(XR,YT),PSET:XL=XL+PX:LINE-(XL,YT),PSET:YB=YB-PX:LINE-(XL,YB),PSET:GOTO200
600 PRINT TIMER/60

This drops it down to 3.03333334! Parsing LINE may be faster than parsing a DRAW string. Also, longer line numbers take longer to parse, so we could RENUM0,0,1:

0 TIMER=0:XL=3:YT=0:XR=252:YB=188:PX=3:PMODE4,1:PCLS:SCREEN1,1:LINE(XL,YB)-(XR-PX,YB),PSET
1 XR=XR-PX:LINE-(XR,YB),PSET:YT=YT+PX:IFYT=96THEN3
2 LINE-(XR,YT),PSET:XL=XL+PX:LINE-(XL,YT),PSET:YB=YB-PX:LINE-(XL,YB),PSET:GOTO1
3 PRINTTIMER/60

However, this did not seem to make any consistent measurable difference.

A further optimization could be done by changing the 2-letter variables to 1-letter. But, beyond that, it would take adjusting the logic to find more improvements. Parsing integers is slow, such as “IF YT=96” as is parsing line numbers (thus, lines 1, 2 and 3 should be faster to “GOTO 1” than 100, 200, 300 and “GOTO 100”).

And, one question:: Michael started in from the corner of the screen. Here is a screen shot done in the Xroar emulator with artifact colors turned off so we can see the actual lines better:

This means this version is drawing a few lines less than the version I made. I need to go back and re-read the article and see if I got the details wrong, or if Michael is just keeping the 3-pixel border more consistent. I like the 3-pixel padding, but perhaps the first line to the right on the bottom should have gone further (3 pixel padding) and then at the top left a bit more (3 pixel padding).

Meanwhile, in response to Michael Pittsley‘s post. Andrew Ayers took the version I did and modifies it to work on the CoCo 3. The original contest was a few years before the CoCo 3 even existed, but one would imagine if the article had appeared later it might have had a contest for fastest CoCo 1/2 version, and fastest CoCo 3 version:

A couple more slightly improved CoCo 3 versions of Allen’s code above:

Both of these take longer to run than the original Allen posted, even with the high-speed poke…at least, running on XRoar Online (no idea about real hardware).

– Andres Ayers

CoCo 3 RGB b/w 320×192:

5 POKE65497,0
10 TIMER=0
20 HSCREEN2:PALETTE0,0:PALETTE1,63:HCOLOR1
30 W=320:H=191:HDRAW"BM0,191"
40 HDRAW"R=W;"
50 HDRAW"U=H;L=W;"
60 H=H-3:W=W-3
70 HDRAW"D=H;R=W;"
80 H=H-3:W=W-3
90 IF H>0 THEN 50
100 TM=TIMER
110 IF INKEY$="" THEN 110
120 PRINT TM/60
130 POKE65496,0

CoCo 3 RGB b/w 640×192:

5 POKE65497,0
10 TIMER=0
20 HSCREEN4:PALETTE0,0:PALETTE1,63:HCOLOR1
30 W=640:H=191:HDRAW"BM0,191"
40 HDRAW"R=W;"
50 HDRAW"U=H;L=W;"
60 H=H-3:W=W-3
70 HDRAW"D=H;R=W;"
80 H=H-3:W=W-3
90 IF H>0 THEN 50
100 TM=TIMER
110 IF INKEY$="" THEN 110
120 PRINT TM/60
130 POKE65496,0

It has been so long since I worked in BASIC on the CoCo 3 that I had forgotten about HDRAW and such being there. And, as expected, it takes longer to draw 320 or 640 pixel lines than it would to draw the 256 pixel lines on the CoCo 1/2 PMODE 4 display.

This does make me wonder how it would compare if limited to the same 256×192 resolution of PMODE 4. I expect the overhead of banking in and out the CoCo 3 high resolution graphics screens will add some extra time, and likely working with a screen with more than just 1-bit color (on/off pixels) is more memory to plot through.

Anyone want to work on that experiment?

Make it faster!

Revisiting my version from part 1, I changed the screen to PCLS1 (to make it white) and set COLOR 0 (to make the drawing black) so it would look like the printout in the magazine. This looks nice, but now we cannot tell how close to the original border of the image my results are:

I also see that because of the 256×191, the very last line does not appear to have a 3 pixel padding. Maybe we can look at the math later.

I simply took my version and combined lines and removed any spaces:

0 'SPIRAL2.BAS
5 FORA=1TO1000:NEXT
10 TIMER=0:PMODE4,1:PCLS1:SCREEN1,1:COLOR0:W=255:H=191:DRAW"BM0,191R=W;"
50 DRAW"U=H;L=W;":H=H-3:W=W-3:DRAW"D=H;R=W;":H=H-3:W=W-3:IFH>0THEN50
100 TM=TIMER
110 IF INKEY$="" THEN 110
120 PRINT TM/60

I could also do the “RENUM0,0,1” trick, but all of this only gets me to 3.016666667 seconds.

NOTE: I put a FOR/NEXT at the start so when I type “RUN” I have a moment to release the ENTER key before the timing starts. If you hit a key during the drawing, BASIC tries to handle that key and it will slow down the program. Run it and pound on the keyboard while it is drawing and you can see it slow down quite a bit (I got 3.7 seconds doing that).

But I digress…

Let’s collect some more examples, and see what other methods folks come up with. Now I want to get the same logic, just DRAW versus LINE to draw the lines, and see which one is faster.

To be continued…

UnderColor’s spiral challenge from 1984 – part 1

See Also: part 1, part 2, with part 3 and part 4 coming (and maybe more).

And now back to CoCo …

– Michael Pittsley posted in the TRS-80 Color Computer (CoCo) group on Facebook:

Many of us have our CoCos and have memories or how good we once were writing basic programs on it. Including myself. I found this article in the first UnderColor magazine. It was a contest to see who could write an ECB program that created a spiral. — Write an Extended Basic program that draws a spiral figure on graphics screen 0 on PMODE 4. The figure, when done should look like the picture. Use any combination of Basic commands, but no assembly language. The winner will be the person whose program executes in the shortest possible time. (Entries that simply list a series of LINE commands will be disqualified). I took a stab at it and realized how much I had forgotten about basic, so this was fun for me. I put my results as the first comment. Feel free to try your hand at it, post a screen shot and the time it took to complete.

– Michael Pittsley

This caught my attention.

UnderColor magazine (1984-1985) was one I never saw, though the name sounds familiar so I may have at least read a reference to it, or seen an ad for it somewhere. You can find the issues preserved here:

Search – TRS-80 Color Computer Archive

The article in question appeared in the first issue, which you can read here:

UNDERCOLOR Vol1 No 1 Dec 10, 1984

The article, by Bill Barden, presented a contest to see who could write a program in BASIC (no assembly allowed) that would generate a spiral as demonstrated by this graphic:

The winner would be the program that could do this in the least amount of time.

I have discussed drawing spirals on the CoCo in the past, but not like this. I also wrote about spirals for a text mode attract screen, but not like this.

So now let’s spiral like this.

LINE versus DRAW

The most obvious approach would be to use the LINE command. It takes a set of X and Y coordinates and draws a line between them, like this:

LINE (0,0)-(255,191),PSET

However, with what I know about BASIC these days (and wish I knew back then), that is alot of parsing of numbers and characters and such. That makes it slower than it might need to be.

One shortcut is that LINE remembers where it left off, so you can start a new line just by specifying the destination:

LINE-(127,0),PSET

Doing this trick should speed up a spiral program, since you only need to give the starting point once, then you can just “draw to the next spot” from then on out.

But I did not attempt this. Instead, I thought about DRAW.

The DRAW command is very powerful, and does allow you to draw to specific coordinates. You can do a “blank” draw just to move the starting point, like this:

DRAW"BM0,191"

That will do a Blank Move to (0,191), which is the lower left corner of the screen and the location where the spiral is supposed to start.

You can then do things like…

DRAW"R10"

…and that will draw a line 10 pixels to the right. (Well, the coordinates are scaled, I think, so it is 10 pixels on a PMODE 4 screen, but at other lower resolutions, that number of pixels will be scaled down.)

How can we spiral like that? One way would be to build a string and append it:

X=100
X$=STR$(X)
DRAW"R"+X$

That works, but all that parsing and creating strings and such would certainly be slower than using a built-in feature of DRAW which lets you use a variable inside the quotes! You just put “=” before the variable name, and a “;” after it.

X=100
DRAW"R=X;"

That will draw to the right however many pixels X is set to!

Could this be faster than line?

Here is what I came up with:

0 'SPIRAL1.BAS
10 TIMER=0
20 PMODE4,1:PCLS:SCREEN1,1
30 W=255:H=191:DRAW"BM0,191"
40 DRAW"R=W;"
50 DRAW"U=H;L=W;"
60 H=H-3:W=W-3
70 DRAW"D=H;R=W;"
80 H=H-3:W=W-3
90 IF H>0 THEN 50
100 TM=TIMER
110 IF INKEY$="" THEN 110
120 PRINT TM/60

This sets a TIMER variable at the start, draws the spiral, then reads the value of the TIMER again. When you press any key, the program exits and prints the time (TIMER/60) it took to run the main code.

Here is what I get:

And pressing a key shows me:

3.03333334

Three seconds.

I expect I can optimize my program to speed it up. In the meantime, what can you come up with? Is there a faster way?

Let’s play!

Ciaran “Xroar” Anscomb’s PCLEAR 0 without assembly!

On the CoCo mailing list, Ciaran (author of the Xroar emulator), said this:

FWIW this is the bodge I have saved out for doing PCLEAR0 without such
ROM patching:

POKE183,PEEK(183)-6:POKE188,PEEK(188)-6:PCLEAR1:POKE183,PEEK(183)+6:POKE188,PEEK(188)+6

Annoyingly verbose, but should account for DOS, and works on the
Dragon too.

..ciaran

https://pairlist5.pair.net/mailman/listinfo/coco

This topic came up because of Juan Castro‘s experiments with updating HDB-DOS to add new functionality on a CoCo 1 and 2 (but that is a discussion for a dedicated blog post sometime). Juan had recently “fixed” Extended Color BASIC to allow using “PCLEAR 0” to remove all graphics memory and give more RAM to BASIC. I have discussed PCLEAR 0 in the past

This mysterious line performs a PCLEAR 0 without needing to load and run program of assembly code!

POKE183,PEEK(183)-6:POKE188,PEEK(188)-6:PCLEAR1:POKE183,PEEK(183)+6:POKE188,PEEK(188)+6

And it works!

But … how does it work!?!

Ciaran, you’ve got some ‘splainin’ to do…

Until then…

A BASIC coin flip…

Us humans (this is not an A.I. post, bleep bloop) have a tendency to try to find patterns in randomness. For example, when asked to pick a number between 1 and 10, a magician/mentalist will know that statistically humans are more likely to choose certain numbers. There is alot of “human nature” that makes us somewhat predictable.

In a deck of 52 playing cards, if you were asked to predict what card is on the top of a shuffled deck, you probably wouldn’t say Ace of Diamonds, but that card is just as likely to be there as any other. No matter which card you guess, you have a 1 in 52 chance of being correct.

Call it in the air…

When it comes to a coin toss, do you always call heads? Always tails? Or do you alternate?

When a gambling casino game presents a grid of squares and asks you to pick four squares, do you “randomly” pick various squares, or do you just click the first four on the top row?

If it is random, either should have the same outcome.

And don’t get me started on picking lottery numbers. While we do not often see the picked numbers be “1, 2, 3, 4, 5 and 6”, that sequence should be just as likely as any other.

If it is random.

So let’s play a game in BASIC with a coin toss. Heads or tails will be represented using CoCo’s Color Basic RND() command. Doing RND(2) will produce either a 1 or 2 result.

NOTE: This is not random. This is psuedo random. I have discussed this previously, but for the sake of this blog post we will pretend it truly is random.

Would calling heads every time produce a better result than calling tails? Or would randomly choosing heads or tails each flip be better?

Let’s try…

0 'COINFLIP.BAS
5 'POKE 65495,0
10 W1=0:W2=0
20 FOR A=1 TO 1000
30 V=RND(2)
40 IF V=1 THEN W1=W1+1
50 IF V=RND(2) THEN W2=W2+1
60 NEXT
70 PRINT "ALWAYS GUESSING 1:";W1
80 PRINT "GUESSING RAND 1-2:";W2

This program will “randomly” flip a coin 1000 times and count how many times it landed on heads (1) versus how many times it matched a randomly (1-2) chosen value. At the end, it will print the results:

As you can see, in this “random” test, neither method really proved to be that different. We could also alter the output to print how many times guessing tails (2) would have worked (1000 clips minus how many times it was heads, 511 in this example, so 489 if my math is correct).

But it still feels better thinking we have some “control” over things and guessing rather than always choosing the same guess.

Alphabetically speaking…

Let’s modify the program to select a random letter, A-Z (represented by 1-26). We will now always guess A, versus randomly guess a letter (1-26):

0 'COINFLP2.BAS
5 'POKE 65495,0
10 W1=0:W2=0
20 FORA=1TO1000
30 V=RND(26)
40 IF V=1 THEN W1=W1+1
50 IF V=RND(26) THEN W2=W2+1
60 NEXT
70 PRINT"ALWAYS GUESSING 1:";W1
80 PRINT"GUESSING RND 1-26:";W2

And here is what I get…

Maybe this perspective will help you “always choose tails” or “always guess Aces of Spades” in the future.

And speaking of the future, there is another “random” test I want to experiment with, coming soon.

Until then…

Using BASIC to prove a (dumb) point.

I recently got lured in to downloading some casual game for my phone. (Thank you, gas station rewards program, for telling me I could earn a bonus from you if I downloaded this stupid game.)

I gave up these time wasters years ago when I realized how much time they wasted.

But with this addictive substance back on my phone, I was thrown back in and realized… nothing has changed. It’s even worse. These games are not games. They are ad platforms. Every few minutes you get an ad. Or, you have to watch an ad to get something you need.

I will repeat… These games are not games. They are ad platforms.

But, once new trend is how many ads for other games I see that offer “real cash” for playing them. They show folks who are broke pulling out their phone and then playing to get the money they need. “It’s just that easy!”

Obviously, if you could easily make hundreds of dollars playing a game, you wouldn’t need to pay to advertise that game. People would all know about it from word-of-mouth and we’d all be doing that.

What is even more amusing (at least to me) is how many ads warn you about “scam games” … while basically being the same game they are warning you to avoid.

But one thing caught my attention… These ads will claim to be “skill based” games. Many of them are card games like Solitaire.

While it is true there is skill to know how to play a certain set of random cards you have been dealt, there is zero skill that can prevent you from losing if you get enough bad hands. Likewise, any “skill” game that uses a roll of the dice you can 100% be guaranteed to lose if you had enough random bad rolls of the dice and your opponent had enough random good rolls.

And that gave me a (dumb) idea…

The house always wins.

As a kid, I remember playing Battleship with friends. In case you are unfamiliar, here is the wiki page for this game:

Battleship (game) – Wikipedia

Basically, you place your ships on a grid, aligned vertically or horizontally, and your opponent does the same. You then call out the coordinate you want to “bomb” on your opponent’s grid, and they tell you if you had a “hit” or a “miss.”

Some of us cheated.

Since there was no way to verify where the ships were, if your opponent called a shot that was a “hit,” you could easily move your piece out of the way to a new spot and report “miss.” Evil. But fun. This caused LONG games, playing until you basically got down to where the ships had no other place to be.

Because of this, I never trusted the Battleship clone I played on my Radio Shack Color Computer. How could I ever trust that computer wouldn’t “cheat” the same way?

But hey, Battleship is a game of skill — after all, it does not use any random roll of the dice or deal of a card.

But the card and dice games are completely “random” and no matter how skilled you are, you can have a hard time winning against bad randomness ;-)

I thought it would be fun to write a simple BASIC Blackjack (or 21) card game, except the computer would cheat. It would have a list of all un-dealt cards, and ensure it always gives the best card to the dealer, and the worst to the opponent. If things were truly random, this is a possibility with a truly random outcome.

The same cheating computer could be done for any random game — just as Monopoly. Imagine ALWAYS getting a roll that makes you pay rent or Go To Jail, while the computer always got a roll that got them to a safe property, or one they could buy.

Shall we play a game?

I will not present code for this yet. I have not written it. But, perhaps one of you will beat me to it.

My idea:

  1. The cards are in a random array of 52.
  2. The computer dealer will always get the best cards. Initially a face card and an ace, then as those cards are depleted, two cards totaling ten. At that point, there is no way the player can ever get two cards at 21. And, if the player got 20, the “house always wins” so even if that happened, the computer wins.
  3. After the player receives its cards, any request to “hit” would be a card just enough to make them bust.
  4. This process would continue through the rest of the deck.

I am unsure if, at some point, it would ever turn into a “fair” game. And this is why I want to write this dumb idea.

Thoughts?

Color BASIC supports two letter variables, except when it doesn’t.

Over on the CoCo Facebook group, in a discussion about “why is I always used for loops”, Rob R. left a comment that caught my interest:

“… I have a vague memory that (at least on the Mod I) there were one or two letters that weren’t usable since they could be tokenized to a command. At least some two letter variables would be verboten, like ‘TO.'”

– Rob R. on Facebook

This made me wonder: how many are there? Here are the ones that I think would be problematic:

  • AS – in Disk BASIC, there is an “AS” keyword used for the file system. “FIELD #1, 10 AS A$”
  • FN – as in “DEF FNA(B)=B*42”
  • IF – as in “IF A=1”
  • ON – as in “ON X GOTO/GOSUB” or on the CoCo 3, “ON BRK GOTO” or “ON ERR GOTO”
  • OR – as in “IF A=1 OR A=2”
  • TO – as in “FOR I=1 TO 10”

Using any of these such as “TO=1” or “FN=3” will create a ?SN ERROR.

Are there others?

Also, some of these will work in Color BASIC that will not work if you have Extended or Disk BASIC:

  • “DEF FN” was added in Extended, so on a Color BASIC machine you should be able to use FN as a variable.
  • “AS” was added in Disk BASIC, so you should be able to use AS on Color and Extended BASIC.

I wonder how many folks wrote BASIC programs using variables that were not allowed when they later added the Extended BASIC ROM and/or Disk BASIC, and wondered why they stopped working?

Until next time…

Color BASIC decimal, hex and octal are not the same?

There is something wonderful about having a poor memory. Whether I knew something before or not, I do not remember it now, so I get the pleasure of “learning something new” even if I previously knew the new thing.

In a reply to my Color BASIC and octal post, Juan Castro mentioned another quirk about how BASIC sees numbers represented as decimals versus hex or octal.

AND, OR, AND NOT demand signed 16-bit integers as input, whereas the &H/&O expressions (and consequantly my &B extension) return UNsigneded integers. That is VERY annoying:

PRINT &H8001 AND &H0007
?FC ERROR
OK

Come the %#&*£ ON!

– Juan Castro

I am unsure about this, since after the parser reads through the code, it turns any hex or octal or decimal value intothe internal 5-byte floating point representation of the value. “A=65535” turns into exactly the same type of variable as “A=&HFFFF”.

AND and OR should deal with them exactly the same way. In Juan’s example, decimal values will generate the same ?FC ERROR. But there is a workaround…

I stumbled upon this when playing around with using AND and OR to set or test bits. My Color BASIC and 16-bits and AND and OR and NOT article explains what I was trying to do, and how I ultimately ended up achieving it.

In Juan’s example, &H8001 is 32769 in decimal. &H0007 is 7. Having the “high bit” (bit 15) set of the first value effectively makes it a negative number:

1000000000000001 = &H8001 (32769)
^ ^
bit15 bit0

Any value with that bit set will not work with AND or OR, regardless if you represented it in decimal, hex or octal. You can go up to &H7FFF (32767) using AND or OR, but past that, you get the ?FC ERROR:

Juan has been working on some BASIC enhancements, adding new features (like a method of representing values in binary — which I would find quite useful). But seeing numbers in binary not work would be even more frustrating:

PRINT &B100000000000000 AND &H1
?FC ERROR

…at least, that is my assumption about what is “&B” binary representation would do.

Here is how to make Juan’s code work:

PRINT (&H8007-65536) AND &H0007
1

Any value that has that high bit set needs to have 65536 subtracted from it, then you get the results you would expect.

But unfortunately, you cannot use AND to test for that high bit to be set ;-) so instead you have to do silly things like this:

V=&H8007
IF V>&H7FFF THEN V=V-65536
PRINT V AND &H0001

And it’s just that easy!

Though, if you print V, you will see it is a negative number, as you might have guessed. If you don’t need to PRINT V, doing something as simple as this will let you AND and/or OR on values that have the high bit set.

Yuck.

“Come the %#&*£ ON!” is right.

Until next time…

What are the odds?

Back in the early 1980s, a friend of mine had TRS-80 Model III in their house. His mother was a writer, and had gotten it to use for writing books.

I believe it was this TRS-80 where I saw a version of Monopoly that taught me a strategy I had never learned as a kid:

The computer bought EVERYTHING it landed on, even if it had to mortgage properties to do so.

This simple strategy was a gamble, since you could end up with too many mortgaged properties and no source of income from rent. But. by doing this, the computer would end up owning so many random pieces it made it difficult to own a monopoly yourself.

And since then, that is how I played Monopoly. When it worked, it created some awfully long games (if no one had a monopoly to quickly drive other players bankrupt when they landed on their hotels).

In more modern times, I have watched YouTube videos concerning Monopoly strategies. They will break down the statistical odds of landing on any particular piece of property. For example, you know a pair of dice can produce values from 2 to 12. If truly random, the odds of getting a “1 and 1” should be the same as getting a “3 and 5.” Rather than focus on dice rolls, the strategies take into consideration things that alter locations: Go To Jail, Advance to Go, Take a Ride on Reading, etc.

These cards existing means there are more chances for a player to end up on Go, Reading Railroad, Jail, etc. This means the property squares after those spots have more chances of being landed on.

Board with board games. Move along…

But I digress… As Internet Rabbit Holes do, this led me to watch other videos about statistics in things like card games. In a randomly shuffled deck, there should be as much chance for the first card to be Ace of Spaces as there is for it to be Three of Clubs. It is random, after all.

For that first card drawn, that is a 1 out of 52 chance to be any card in the deck. (52 cards in the deck.)

But as a game plays on, there are fewer cards, so the odds of getting any of the remaining cards increases. For the second card drawn, you now know there is a 0% chance of getting whatever the first card is, and a 1 in 51 chance of getting any of the rest.

And so it continues…

For games like Blackjack or 21, you do not really care if it is a Ten of Diamonds or a King of Hearts or a Queen of Clubs or a Jack of Spades. They all have the value of 10 in the game. Thus, the likelihood of drawing a ten card is much higher than any other card in the deck.

You have four suits (clubs, spades, hearts, diamonds) so there are four of each card – Aces, Two through Ten, Jacks, Queens, and Kings. This means there are 16 cards in the deck that could be a value of 10 in the game. When you draw the first card, you should have a 16 in 52 chance of it being a ten card. That is about a 33% chance!

If you pay attention to what cards have been seen (either by you having it, or seeing it face up with another player), you can eliminate those cards from the possibilities — changing the odds of what you will get.

This is basically what I understand card counting to be. If you play a game, and you know you’ve seen three Kings so far (either in your hand, or played by others), you now know instead of four chances to draw a King, you only have one.

Math is hard. Make the computer do it.

I know this has been done before, and quite possible even on a Radio Shack Color Computer, but I thought it might be fun to create a program that displays the percentage likelihood of drawing a particular card from a deck. I suppose it could have the Blackjack/21 rule, where it treads 10, Jack, Queen and King the same, versus a “whole deck” rule where each card is unique (where you really need an 8 of Clubs to complete some run in poker or whatever game that does that; I barely know blackjack, and have never played poker).

I plan to play with this when I get time, but I decided to post this now in case others might want to work on it as well.

I envision a program that displays all the cards on the screen with a percentage below it. As cards are randomly drawn, that card’s percentage goes to 0% and all the others are adjusted.

It might be fun to visualize.

More to come, when time allows…