CardTrick for Windows Only
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
Blog Home Page: http://traverrb.blogspot.com/
Programs and Files Discussed in the Blog: http://traver.org/traverrb/
0 Comments:
Post a Comment
<< Home