By Tom Cone, Tampa PC Users Group
A number of years ago I wrote several articles presenting a series of programming "puzzles" for members of the users group. My original solutions were written in various DOS languages including dBaseIII+ and PowerBASIC. Recently the publishers of PowerBASIC have added extensions to their windows BASIC compiler that caught my eye. I decided to take the plunge and try to write a true 32-bit native code solution to one of my early puzzles.
The compiler I am using is called Power BASIC DLL Compiler for Windows, PB/DLL vers. 6.0. This version includes new features collectively called Dynamic Dialog Tools. These permit you to build and manipulate the Windows graphic user interface using command line statements with varying argument lists. You can create different types of dialog forms (checkboxes, listboxes, text boxes, message boxes, combo boxes, and so), adding controls to them for interaction with the user.
Using this compiler you can build DLLs for use with Visual BASIC (in fact thats how the publisher positioned the product initially, and its still apparently a major portion of their market). But you can also build stand alone EXE files which dont require any runtime support.
The source code below is what I developed. You programmer types may be surprised at how easy it is to read, and how little work was required to create and manipulate the interface.
When this code is compiled it creates a single stand alone EXE file which is 12 kb in size. Thats right! 12 kilobytes.
Heres the source (comment lines are preceded by an apostrophe):
'----------------------------------------------------------
'DOORS.BAS for PB/DLL 6.0
'500 room hotel puzzle
'By Tom Cone - 11/25/99
'Assume hotel with 500 rooms. Each room has one door.
' AT outset ALL are closed.
'Assume 500 guests arrive, one at a time.
'Guest 1 visits each room, and opens each door.
'Guest 2 visits every other room and closes each door.
'Guest 3 visits every third room. If door is closed, he opens it. If it
' is opened, he closes it.
'Guest 4 visits every fourth room. If door is closed, she opens it.
' if it's open, she closes it.
'And so on.
'After 500th guest visits the 500th room, how many doors are open
' in the hotel?
'----------------------------------------------------------
#COMPILE EXE 'directs compiler to make EXE file
FUNCTION WINMAIN (BYVAL CurInst AS LONG, _
BYVAL PrvInst AS LONG, _
CmdLine AS ASCIIZ PTR, _
BYVAL CmdShow AS LONG) EXPORT AS LONG
%textcontrol = 100 'program uses a single control. Each must be
' separately numbered.
LOCAL hDlg AS LONG 'this variable will hold handle of the dialog
' ** Create a new dialog template, the arguments specify size & location
DIALOG NEW 0, "Progress Report", ,, 260, 50, 0, 0 TO hDlg
' ** Add controls to it; arguments set size & location
CONTROL ADD TEXTBOX, hDlg, %textcontrol, "", 14, 12, 200, 12, 0
'now declare variables needed for calculations
DIM i AS LONG 'counter
DIM totaldoorsopen AS LONG 'running count of doors opened
DIM totaldoorsclosed AS LONG 'running count of doors closed
DIM patron AS LONG 'current patron
DIM door(1:500) AS LONG 'array of doors
'-1 = open
' 0 = closed
DIM currentdoornumber AS LONG
DIM doorstoskip AS LONG 'number of doors to be skipped as a patron
'moves thru hotel
totaldoorsopen = 0 'setup start condition
totaldoorsclosed = 500 'setup start condition
FOR i = 1 TO 500
door(i) = 0 'close all doors
NEXT
' ** show the dialog
DIALOG SHOW MODELESS hDlg 'this displays the dialog box w/blank text
FOR patron = 1 TO 500 'queue up the patrons to begin their walk
doorstoskip = patron 'set to match this patron
currentdoornumber = patron 'begin with this patron's first door
WHILE currentdoornumber <= 500 'loop through all doors
IF door(currentdoornumber) = 0 THEN 'if first door for this
patron is closed
door(currentdoornumber) = -1 'open it
totaldoorsopen = totaldoorsopen + 1
totaldoorsclosed = totaldoorsclosed - 1
ELSE 'door was open
door(currentdoornumber) = 0 'so close it
totaldoorsopen = totaldoorsopen - 1
totaldoorsclosed = totaldoorsclosed + 1
END IF
currentdoornumber = currentdoornumber + doorstoskip 'skip to next door
WEND 'go check next door
'loop ends when currendoornumber > 500
' ** update value of textcontrol and refresh the dialog
CONTROL SET TEXT hDlg, %textcontrol, "Current patron: "+STR$(patron)+_
" Current total doors opened: "+STR$(totaldoorsopen)
DIALOG DOEVENTS
SLEEP 10 'needed to slow down the display so you can watch it change
NEXT patron
' ** close the dialog box
DIALOG END hDlg
' ** display msgbox with final counts
MSGBOX "Final count of open doors: "+_
STR$(totaldoorsopen)+CHR$(13)+CHR$(10)+"Final count of closed doors: "+_
STR$(totaldoorsclosed),,"All Guests Finished"
END FUNCTION WinMain ends here. Program terminates when user presses OK.
' end of source
'-----------------------------------------------------------
As the program runs, the text box appears on screen with spinning numeric counters. Heres a snapshot:

When the calculations are ended, this text box is closed, and a final message box is
presented. It looks like this:

I am intrigued with this compiler and am particularly attracted to the small size and fast
speed of the resulting executable file. If you would like more information on it, visit http://www.powerBASIC.com. The compiler is available
for $189 (plus shipping) with documentation on disk. A softcover book is available at
extra charge.
If you are working with a different language (like Visual BASIC, for example), I challenge
you to try your hand at converting my code so that it will work for you. Maybe one of you
would be willing to write it up for publication next month for comparison purposes. How
about it? Any takers out there? u