Begining Programming - Part II

Section Five, The Program

The Program - Ready to Run

NewsLetter TOC

Newsletter Home

Blast

MDI

OS Editor

LBW Review

Fast Data

SQLite

Tsunami

Begin Prog II

Locate/LBCard

So, here is the program without all the commentary. You can copy and paste this right into LB2 or LB3 and run the program (I can't remember whether LB2 supports EXIT-FOR or not).

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

'Lay down five unique cards from deck of 52

'For LB2 or LB3


[start]

'Begin by clearing the screen

Cls


'We need to initialize our counter

cardcnt = 0


[drawCard]

'We will represent the random cutting and

'drawing of a card with a RND statement

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


'we have a card - increment the count

cardcnt = cardcnt + 1


'Let us put the card into an array of cards

cards(cardcnt) = drawn


'Show the cards so far

Print "Cards so far: ";

For x = 1 to cardcnt

Print cards(x);" ";

Next x


'Add a print statement to cause next output to be

'on a new line and concatenated onto the current line

Print ""


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

'this is not a duplicate

If cardcnt > 1 Then


'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


'if x equals cardcnt then there were no duplicates,

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

If x < cardcnt Then


'Report what we found

Print "Duplicate found - same as card ";x


'remove the last drawn card (this is automatic

'if we simply decrement the counter cardcnt

cardcnt = cardcnt - 1


'Now go get a new card...

GoTo [drawCard]


End If


End If


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

If cardcnt < 5 Then GoTo [drawCard]


'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$


'evaluate the answer

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

End