Using Standard Windows Widgets
Something I've been wanting to cover is the integration of standard Liberty Basic dialog boxes into the Magic 6 ball program. Liberty Basic has three such standard dialog boxes which can be used, and we will be using all three. They are the PROMPT dialog box, the NOTICE dialog box and the CONFIRM dialog box.
The first one we will use is the NOTICE dialog box. This is what we call a Windows widget. It is a prepackaged Windows tool we call. It will show a basic Windows style dialog box with the message we pass it. Here is the syntax for the command from the Liberty Basic Help file:
This command pops up a dialog box which displays "string expression" and which has a button OK which the user presses after the message is read. Pressing Enter also closes the dialog box.
Two forms are allowed. If "string expression" has no Cr character (ASCII 13), then the title of the dialog box will be 'Notice' and "string expression" will be the message displayed inside the dialog box. If "string expression" does have a Cr character, then the part of "string expression" before Cr will be used as the title for the dialog box, and the part of "string expression" after Cr will be displayed as the message inside. Further Cr's will force line breaks into the text contained in the message.
notice "Super Stats is Copyright 2001, Mathware"
notice "Fatal Error!" + chr$(13) + "The entry buffer is full!"
So as the help file says, it shoots a dialog box up onto the window with the text you give it. You can experiment with this really easy. Open a new, blank page in Liberty Basic and type the following:
notice "The Beginner Series is really great!"
Now run the code and you will see the following:
As you can see, the NOTICE command is quite easy to use and the output is most windows like. The example was interesting, but what we really want to do is use the Notice command to introduce the Magic 6 ball to the user. This version of the Magic 6 ball that we will create will build on the last version, utilizing the arrays for processing. Here is a clip of the first section of the program including the NOTICE command:
'The Magic 6 ball - version 3 'Written by Joshua Moore and Brad Moore' 'Copyright 2002, all rights reserved 'written for LB3.x 'You are free to incorporate any portion of this code 'into your own programs without notification or credit 'to the authors. Please do not distribute as-is. 'DIMension a string array to hold the answers DIM answer$(6) 'Now assign the text strings to each array member answer$(1) = "MAYBE..." answer$(2) = "NO! - Never - Don't ask again!" answer$(3) = "Its not clear now." answer$(4) = "Yes" answer$(5) = "Probably not..." answer$(6) = "You can count on it!" 'Start the interaction with the user Notice "Hello - I am the Magic 6 ball - I can tell the future. " + _ "After we have been introduced, you will have a chance to " + _ "Ask me a yes or no question and I will tell the answer!"
Observe that the notice command span three lines of text. I could have written this statement on a single line, stretching way out to the right in the editor, but for readability and easy of understanding I choose to split the statement over three lines. This is possible because of the continuation character at the end of each line of the statement (except the last line - there is nothing to continue there). The continuation character is the underscore "_" and it signals to Liberty Basic that the current line extends on to the next line.
The next command is the PROMPT command. It is great for getting quick and dirty input of a value, but it lacks features of the NOTICE and CONFIRM commands, such as dynamic sizing and modifiable titles. Also the current version of Liberty Basic (3.02) will only allow PROMT to return a string variable (this is not a issue for us though).
PROMPT opens a dialog window and displays a short string of text. Then it waits for the user to type some data into the text field and then click the OK button. It will return the text the user entered into the string supplied as one of the parameters of the command. Here is what the Liberty Basic Help file says about PROMPT:
PROMPT string; responseVar$
The PROMPT statement opens a dialog box, displays string, and waits for the user to type a response and press 'Return' (or press the OK or Cancel button). The entered information is placed in responseVar$. If Cancel is pressed, then a string of zero length is returned. If responseVar is set to some string value before PROMPT is executed, then that value will become the 'default' or suggested response. This means that when the dialog is opened, the contents of responseVar$ will already be entered as a response for the user, who then has the option to either type over that 'default' response, or to press 'Return' and accept it.
A simple experiment in a new Liberty Basic programming window shows the following prompt for the command below:
prompt "What is your favorite color?";color$
The following code shows the next section of the program using the PROMPT command to get the users name, and just a little bit further down it is also used to get the users question. Notice also that if the PROMPT command returns an empty string (which it will do if the Cancel button is clicked, or if there was no input and the OK button was clicked) that the NOTICE command is used to warn the user of the error and then the user is returned back to the PROMPT command to get the data again. Here is the next section of program that demonstrates all of this:
[getPlayer] prompt "You know me, but who are you (enter name)?";name$ if name$ = "" then Notice "You did not enter anything for your name! - Try agian..." goto [getPlayer] end if [loop] Prompt name$ + " Ask Magic 6 a 'yes' or 'no' question.";question$ if question$ = "" then Notice "I am sorry, I did not hear you. Perhaps you should type a little louder." goto [loop] end if 'get a random number between 1 and 6 number = int(rnd(1)*6) + 1 'now simply print the answer Notice "Magic 6 ball says: " + answer$(number)
By using the NOTICE and PROMPT commands we have simplified our code even more. There are no more print statements to the Liberty Basic console, which saves a lot of code that we used in formatting the output to the screen.
The final widget that is used is the CONFIRM command. It is useful when we are asking the user a Yes or No question, such as "Do you wish to play again". The CONFIRM command will return the value of the button the user clicks, whether that is "yes" or "no". The value is returned into the string variable that is passed as a parameter to the command. Here is what the Liberty Basic Help file says about the CONFIRM command:
CONFIRM string; responseVar
This statement opens a dialog box displaying the contents of string and presenting two buttons marked 'Yes' and 'No'. When the selection is made, the string "yes" is returned if 'Yes' is pressed, and the string "no" is returned if 'No' is pressed. The result is placed in responseVar.
We want to use the CONFIRM command to find out whether the user wants to play the game again. The implementation is simple and straight forward. We pass a string with the query "Do you wish to play again" and then capture the response in the variable answer$.
Right here I want to point out the difference in variables. The array answer$() is not the same variable answer$. They are not related. I just noticed that they were similarly named, but they do not have any relationship with each other. Every character counts in variable naming. Case is also very important. If your program is not behaving the way you suspect it should, investigate your variable names. You might have a misspelling, or mixed case issue.
So having said this, lets take a look at the end of the program.
'ask if they want to play again Confirm "Do you want to play again?";answer$ if answer$ = "yes" then goto [loop] end if Notice "Thanks for playing"
We are again using the NOTICE command at the tail end of the program to thank the user for playing. If the return from the CONFIRM command (in the variable answer$) is equal to "yes" then the user clicked the YES button and the program goes back up to loop to run through it again. This version of the program is also in the Appendix, under Appendix C.