Begining Programming - Part II

Section Four, Concept to Code

Translating Our Model to Code

NewsLetter TOC

Newsletter Home

Blast

MDI

OS Editor

LBW Review

Fast Data

SQLite

Tsunami

Begin Prog II

Locate/LBCard

Ok, back to our case study. We have now completed the analysis phase of the project. We have good, clear model of the problem that presents the discrete steps needed to solve the problem. We have reviewed much of the highlights from last lesson. We have added some new concepts and commands to our vocabulary. This is a simple world, so we will go simply from analysis to programming. If the tasks were more complex there might be other phases of the development process here. A key element that we will encounter in the not so distant future is Graphic User Interface (GUI) design. At this stage in our experience we will simply slap the data out there onto the screen and not worry about the design so much as the elements of the language and art of programming.

We are ready to create the program based on our model. We will be creating this program in Liberty Basic's console window much like we did in the last lesson.

Lets begin with the header. Every program you write that will potentially be shared should at least mention the author, copyright, a brief description and perhaps what language it is written for.

'Draw5.bas - by Brad Moore, public domain 2003

'Lay down five unique cards from deck of 52

'For LB2 or LB3

I added the label "start" to the code. It makes a good place to zoom to, and I just might use it later to jump to. The next thing I chose to do, although it was not in the flow, was clear the screen.

[start]

'Begin by clearing the screen

Cls

I will be counting cards to keep track of how many are showing. This will require a variable to hold the running total. I always initialize my variables (if they are counters) before I use them to insure I know their state. LB does set these to zero, but I find that I avoid a lot of programming headaches by explicitly taking care if this myself. As you may have noticed, the variable is not a string, so it is intended to hold a number and allow math operations.

'We need to initialize our counter

cardcnt = 0

Now we perform the first process box. Drawing a card. I store the result of the random function (RND - review it from the first lesson) in the numeric variable "drawn".

[drawCard]

'We will represent the random cutting and

'drawing of a card with a RND statement

drawn = Int(Rnd(0)*52)+1

I chose to increment the counter right away (so that I could use the incremented value in my array subscript).

'we have a card - increment the count

cardcnt = cardcnt + 1

Now I assign the value of drawn (a number between 1 and 52) to the array variable called "cards" with the subscript of "cardcnt". The first time through the value of cardcnt is 1, so the value of drawn is assigned to the variable "cards(1)".

'Let us put the card into an array of cards

cards(cardcnt) = drawn

Now to show all the cards. This is done every time a card is added. I use a FOR-NEXT loop to display the cards. First I print a label out as a string literal. It is followed by a semi-colon which will cause the next information printed to follow the current information on the same line, concatenating it. The card values are printed one at a time, also on the same line by displaying the value assigned to the array variable "cards". The loop is executed beginning at 1 and counting up until the counter variable "x" is greater than the stop value, in this case the value assigned t cardcnt. In our first pass "cardcnt" is only 1, so it prints the first value and it is done. In subsequent passes, as "cardcnt" increases the program will cycle through the FOR-NEXT loop additional times.

'Show the cards so far

Print "Cards so far: ";

For x = 1 to cardcnt

Print cards(x);" ";

Next x

As the comment says - an extra print statement to keep all the data outputted to the screen from piling up on one line. This was an execution time item I discovered, and not something that we planned for in our flow chart. As you translate from flow chart or pseudo-code, do not be surprised to find things you did not expect or did not show in your model. It happens to all of us!

'Add a print statement to cause next output to be

'on a new line and concatenated onto the current line

Print ""

Now the first of two evaluations. This time we check to see if there are more than one cards. It does not make sense to look for duplicates after all if there is only one card - huh?

'If there are more than one card(s) make sure

'this is not a duplicate

If cardcnt > 1 Then

So there is more than one card - we will loop through the cards comparing all of them to the last card drawn (except the last one, after all it is the one we are comparing the others to). If we hit a match then we bomb out of the loop. This is an extension to the FOR-NEXT command, the EXIT FOR command. It allows us to bomb out of the FOR-NEXT loop with out causing damage. You see you can not simply leave a FOR-NEXT loop in the middle of the cycle or the stack (a place the computer tracks internal pointers to your data) will become corrupted and you will have some really weird and unexpected problems. EXIT FOR lets us gracefully leave the loop while cleaning up the mess behind us.

'There are more than one cards, loop cardcnt - 1 times

For x = 1 to cardcnt - 1

If cards(x) = drawn Then Exit For

Next x

Actually we did this premature exiting to do something else tricky. You see if the FOR-NEXT loop ran the entire loop without exiting early with a EXIT-FOR command, the loop control counter "x" would be equal to the stop value plus one. In our case the stop value was "cardscnt - 1", so if the loop executed completely without premature exit the loop control counter would be equal to "cardscnt". If it were less than "cardscnt" then we would know that the value it equals is a duplicate for our card.

'if x equals cardcnt then there were no duplicates,

'ix x is less than cardcnt then a duplicate was found.

If x < cardcnt Then

If we actually did find a duplicate then we will report it. This was not in the flow chart, but I added so that we could see the program working.

'Report what we found

Print "Duplicate found - same as card ";x

Now we will remove the card from the display and decrement the counter. As the comment notes, these actions are really one and the same.

'remove the last drawn card (this is automatic

'if we simply decrement the counter cardcnt

cardcnt = cardcnt - 1

Of course now we need to get that card again...

'Now go get a new card...

GoTo [drawCard]

END IF statements are critical - don't forget them. They close the IF command black. They let the compiler know when and where the special statements to be executed for this condition end and the rest of the code continues. Think of them as the other book end in a pair of book ends.

End If

End If

Since we are trying to get five unique cards, we really ought to check to see that we have made it yet. If not we go back up to the top and do it all over again.

'If there are less than 5 cards - get another card

If cardcnt < 5 Then GoTo [drawCard]

Well that kind of completed the code that we mapped out in the flow chart. After running the program a couple times I decided a little enhancement was in order. The following displays the final results and asks the user whether they want to run the program again. The cards are displayed as above using a FOR-NEXT command.

'We are all done - Lets report success...

Print "All cards drawn - no duplicates"

Print "Cards: ";

For x = 1 to cardcnt

Print cards(x);" ";

Next x


'Print a blank to move to the next line...

Print ""


'See if they want to run the program again...

Print "Run again (y or n)";

Input a$

Well, I was right way back at the top of the program, I do want a place to return to so that I can run the program all over again. It is the "start" label. If the user typed a small or upper case "y" then we would run the program again.

'evaluate the answer

If a$ = "y" or a$ = "Y" Then GoTo [start]

End

A Sample Run

Here is a sample run from our program. It shows the program hitting a duplicate and tossing out the last drawn card and getting another card from the deck.

Cards so far: 9

Cards so far: 9 11

Cards so far: 9 11 9

Duplicate found - same as card 1

Cards so far: 9 11 23

Cards so far: 9 11 23 3

Cards so far: 9 11 23 3 18

All cards drawn - no duplicates

Cards: 9 11 23 3 18

Run again (y or n)?