Traver REALbasic for Windows: The Blog

REALbasic discussion and programs specifically for Windows (and some discussion of differences between REALbasic and Visual Basic).

Sunday, January 15, 2006

Introducing REALbasic for Windows: The Blog

REALbasic (RB) is a cross-platform computer language used to develop programs for Windows, Mac, and Linux. In some ways, it is very similar to Visual Basic 6.0, except that an RB program will run without the hassle of a complicated installation procedure or the presence of a separate runtime file: you can put your program into a single file on a CD-ROM, and run it from there!

The purpose of this blog is to two-fold: (1) to share comments about things I've learned about RB and (2) to announce the availability of RB source code for (I hope) hundreds of short RB programs."

The emphasis will be upon short programs that are "simple samples," learning examples that may serve as building blocks for larger programs. In many cases the programs will do things that you won't find elsewhere, in spite of the small size of the programs. Only source code will be provided, so this blog (or RBlog) is intended specifically for REALbasic programmers. Most of the programs have only been tested for Windows (using RB 2006 on Windows XP, although earlier versions of RB or Windows may often work as well).

Disclaimer: No promises, warranties, guarantees, etc. of any kind are made here. The code may or may not work for the purpose you intend. If it doesn't, then I warned you of that. If it does, and if you would like to say "Thank you" in some concrete way (e.g., by sending a donation), then that would be great and much appreciated (although I will have to earn it first by providing enough useful programs that you will want to do so). I'll explain in a future entry how that may be done.

The plans are also to include comments on and make reference to longer programs from time to time. Links will be provided to those larger programs as well. Most of the programs, however, will be "barebones," not much more than enough code snippets to accomplish the purpose.

Even though REALbasic is a cross-platform language, I sometimes write programs that work only in Microsoft Windows. That's because that's what my computer runs, and I used to program primarily in Visual Basic, a language very similar to REALbasic. I was accustomed to being able to use API calls to access the Windows operating system, so as to put to use the DLLs (Dynamic Link Libraries) that already existed as part of the Windows OS.

Well, one of those DLLs, specifically Cards.dll, makes it easy to do the graphics for playing cards in any program in which playing cards play a part: solitaire (e.g., Klondike), card games (e.g., Poker), and even magic (card tricks)! Aaron Ballman shows how to make a card game for Windows on his Web site. Here's what he says:

Making a card game with Microsoft's Cards.dll
If you're like me and enjoy making fun card games,
but hate the tedium of the graphics logic, then you'll
be very interested in this simple set of classes which
make use of the Microsoft Cards.dll library. The sample
project includes a simple demonstrating of the classes
by implementing the game "War."

You can find Aaron Ballman's article and code here

Well, I thought I'd write a card trick based on Aaron's code. The program is called CardTrick.rbp and it works only for Microsoft Windows. Most of the programs I reference here in this blog will work on all three platforms, - Mac, Windows, and Linux - but occasionally (as in this case) I'll write a program that will only work in Windows.

The trick isn't especially impressive. If you're familiar with how doing a "binary search" is an efficient way to locate an item, similarly a "ternary search" is efficient as well. Mathematically, it is only necessary for you to tell the computer which of the three columns three times in order for the computer to narrow the choice down to one card out of twenty-seven (since 3 x 3 x 3 = 27). So it's not much of a magic trick, but the main purpose of the program is just to show another simple use of the Windows Cards.dll.

Doing graphics can be a bit tricky. Working with graphics on the screen causes the Paint event to take place, and if the Paint even does some painting itself, you can easily get lost in a vicious circle. My solution to that was to insert this at the top of the Paint event handler:

If DontDoPaintStuff = True Then Exit

DontDoPaintStuff is a global Boolean, and before I do some graphics work (like dealing a card) I set DontDoPaintStuff to True and then set it back to False as soon as I've finished. This keeps me out of vicious loops and keeps the graphics from getting messed up, as you'll see if you try out the program.

If you've got a Mac and have access to playing card graphics, you may want to see if you can modify CardTrick.rbp to work on the Mac. The fifty-two cards are numbered from 0 to 51 (four complete suits, no joker), and here's how I shuffled the deck:

Dim I, RandomPosition, Holder As Integer
Dim r as New Random
For I = 0 To 51
N(I) = I
Next I
For I = 51 DownTo 0
RandomPosition = r.InRange(0,I)
Holder = N(I)
N(I) = N(RandomPosition)
N(RandomPosition) = Holder
Next I

What's happening is that you're taking the last card and swapping it with a random card chosen from the rest of the deck, taking the next-to-last card and swapping it with a random card chosen from the (now one card smaller) remainder of the deck, and so on. The end result is that the entire deck has been randomized (shuffled), and you haven't had to worry about accidentally choosing the same card twice. This is simpler than some methods I've seen of shuffling a deck and is no less random than any other method.

Barry Traver



Home Page for This Blog: http://traverrbw.blogspot.com/

Programs and Files Discussed in the Blog: http://traver.org/traverrbw/

1 Comments:

At 7:35 AM, Blogger DFH said...

Barry,

It's ages since we last had any contact. I'm still around.

Engineering & Theology

Warmest regards,

David Haslam

 

Post a Comment

<< Home