News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

Card shuffle

Started by geecee, March 17, 2009, 01:46:11 AM

Previous topic - Next topic

geecee

This is a short programme that simulates a card pack shuffle.

PlayBASIC Code: [Select]
restart:

` Clear screen
cls rgb(0,0,0)

` Define title and write to screen
title$="Card Shuffle"
centertext 400,10,title$

` Declare array/s
dim randomseries(52)

` Determine first down and second down coordinates
firstdown=20
seconddown=20

n=rnd(51)+1

` Go to subroutine to shuffle cards
shufflecards(cardpack)

` Display cards
for k=1 to 52
if k<=26 then firstdown=firstdown+20:setcursor 200,firstdown
if k>=27 then seconddown=seconddown+20:setcursor 460,seconddown

` Go to subroutine to determine face value of cards
gosub cards

` Display cards
if n=k
print card$+suit$+" "+"***"
else
print card$+suit$+" "
endif

next

print ""
centertext 400,580,"Press any key to rerun ...... Esc to exit"

sync
waitkey
waitnokey

` Go to restart to rerun
goto restart

` --------------------------------------------------
` Function to shuffle cards
` --------------------------------------------------
function shufflecards(cardpack)
for nextnumber=1 to 52

repeat
x=rnd(51)+1
alreadyfound=0
` Check if a number is already found
` and if yes generate another
for j = 1 to nextnumber
if randomseries(j)=x
alreadyfound=1
exit
endif
next j
until alreadyfound=0

` Accept generated number
randomseries(nextnumber)=x

next nextnumber

endfunction

` --------------------------------------------------
` Subroutine to determine card value
` --------------------------------------------------
cards:
card$=""
if randomseries(k)=1 or randomseries(k)=14 or randomseries(k)=27 or randomseries(k)=40 then card$="Ace of "
if randomseries(k)=2 or randomseries(k)=15 or randomseries(k)=28 or randomseries(k)=41 then card$="Two of "
if randomseries(k)=3 or randomseries(k)=16 or randomseries(k)=29 or randomseries(k)=42 then card$="Three of "
if randomseries(k)=4 or randomseries(k)=17 or randomseries(k)=30 or randomseries(k)=43 then card$="Four of "
if randomseries(k)=5 or randomseries(k)=18 or randomseries(k)=31 or randomseries(k)=44 then card$="Five of "
if randomseries(k)=6 or randomseries(k)=19 or randomseries(k)=32 or randomseries(k)=45 then card$="Six of "
if randomseries(k)=7 or randomseries(k)=20 or randomseries(k)=33 or randomseries(k)=46 then card$="Seven of "
if randomseries(k)=8 or randomseries(k)=21 or randomseries(k)=34 or randomseries(k)=47 then card$="Eight of "
if randomseries(k)=9 or randomseries(k)=22 or randomseries(k)=35 or randomseries(k)=48 then card$="Nine of "
if randomseries(k)=10 or randomseries(k)=23 or randomseries(k)=36 or randomseries(k)=49 then card$="Ten of "
if randomseries(k)=11 or randomseries(k)=24 or randomseries(k)=37 or randomseries(k)=50 then card$="Jack of "
if randomseries(k)=12 or randomseries(k)=25 or randomseries(k)=38 or randomseries(k)=51 then card$="Queen of "
if randomseries(k)=13 or randomseries(k)=26 or randomseries(k)=39 or randomseries(k)=52 then card$="King of "

if randomseries(k)>=1 and randomseries(k)<=13 then suit$="Hearts"
if randomseries(k)>=14 and randomseries(k)<=26 then suit$="Clubs"
if randomseries(k)>=27 and randomseries(k)<=39 then suit$="Diamonds"
if randomseries(k)>=40 and randomseries(k)<=52 then suit$="Spades"

return



:)
geecee
LANG MEY YER LUM REEK

A smile costs less than electricity and gives more light :)

kevin

#1
 Looks good, well all expect the last sub-routine to find the cards name.   Perfect opportunity for a function.  This will also remove the dependency  upon the randomized card array.
 
Which could look a bit like this.

PlayBASIC Code: [Select]
Function GetCardName(ThisCard)
if ThisCard=1 or ThisCard=14 or ThisCard=27 or ThisCard=40 then card$="Ace"
if ThisCard=2 or ThisCard=15 or ThisCard=28 or ThisCard=41 then card$="Two"
if ThisCard=3 or ThisCard=16 or ThisCard=29 or ThisCard=42 then card$="Three"
if ThisCard=4 or ThisCard=17 or ThisCard=30 or ThisCard=43 then card$="Four"
if ThisCard=5 or ThisCard=18 or ThisCard=31 or ThisCard=44 then card$="Five"
if ThisCard=6 or ThisCard=19 or ThisCard=32 or ThisCard=45 then card$="Six"
if ThisCard=7 or ThisCard=20 or ThisCard=33 or ThisCard=46 then card$="Seven"
if ThisCard=8 or ThisCard=21 or ThisCard=34 or ThisCard=47 then card$="Eight"
if ThisCard=9 or ThisCard=22 or ThisCard=35 or ThisCard=48 then card$="Nine"
if ThisCard=10 or ThisCard=23 or ThisCard=36 or ThisCard=49 then card$="Ten"
if ThisCard=11 or ThisCard=24 or ThisCard=37 or ThisCard=50 then card$="Jack"
if ThisCard=12 or ThisCard=25 or ThisCard=38 or ThisCard=51 then card$="Queen"
if ThisCard=13 or ThisCard=26 or ThisCard=39 or ThisCard=52 then card$="King"

if ThisCard>=1 and ThisCard<=13 then suit$="Hearts"
if ThisCard>=14 and ThisCard<=26 then suit$="Clubs"
if ThisCard>=27 and ThisCard<=39 then suit$="Diamonds"
if ThisCard>=40 and ThisCard<=52 then suit$="Spades"

result$=card$+" of "+suit$
EndFunction REsult$




 There's a lot of unnecessary logic in that function though.  Granted in this example,the extra time isn't going impact performance in any noticeable way, but it would if the iteration count was increased.  So in such situations, we can remove the IF/THENS blocks by replacing it with a calculation. Which is the  more efficient  option.


 Here we're storing the names in two arrays and then using the GetCardName function to calc the Card and Suit indexs and just pull them out of the name arrays..

PlayBASIC Code: [Select]
 // set up arrays
Dim CardTable$(13)
Dim SuitTable$(4)
Count=SplitToArray("Ace,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten,Jack,Queen,King",",",CardTable$(),0)
Count=SplitToArray("Hearts,Clubs,Diamonds,Spades",",",SuitTable$(),0)


For lp=1 to 10
print getCardName(lp)
next
sync
waitkey


// get card function

Psub GetCardName(ThisCard)
ThisCard=ClipRange(ThisCard,1,52)-1
ThisSuit=(ThisCard)/13
ThisCard=mod(ThisCard,13)
result$=CardTable$(ThisCard)+" of "+SuitTable$(ThisSuit)
EndPsub REsult$





geecee

Thanks for your reply kevin.

It certainly works but is a bit beyond my ken.

QuoteCount=SplitToArray("Ace,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten,Jack,Queen,King",",",CardTable$(),0)
Count=SplitToArray("Hearts,Clubs,Diamonds,Spades",",",SuitTable$(),0)

Your help is much appreciated.

:)
geecee

LANG MEY YER LUM REEK

A smile costs less than electricity and gives more light :)