Deck of Cards: Shuffling and dealing hands

December 17 2005 at 7:02 AM
Forum Owner

DIM Card(52) AS STRING * 3: 'The card deck
DIM i AS INTEGER ' Temporary variable
CLS
PRINT "Card deck demo"
PRINT " - Press any key to see next 5 cards"
PRINT " - Press ESC to quit"
PRINT
GOSUB InitializeDeck
DIM TopCard AS INTEGER ' The next card to be dealt
DO
GOSUB ShuffleCards
DO
GOSUB Draw5
GOSUB WaitForKey
LOOP WHILE TopCard < 41
LOOP ' Until ESC is pressed

WaitForKey:
SLEEP
IF INKEY\$ <> CHR\$(27) THEN RETURN
CLS
SYSTEM

ShuffleCards:
PRINT "Shuffling the deck"
DIM Shuffle AS INTEGER
FOR Shuffle = 1 TO 5
FOR i = 1 TO 52
SWAP Card(i), Card(1 + INT(RND * 52))
NEXT i
NEXT Shuffle
TopCard = 1
RETURN

Draw5:
FOR i = 0 TO 4
PRINT Card(TopCard + i); " ";
NEXT i
PRINT
TopCard = TopCard + 5
RETURN

InitializeDeck:
DIM Suit AS STRING * 1 ' (Character that looks like a spade, heart, etc.)
FOR i = 0 TO 3
Suit = CHR\$(3 + i)
Card((i * 13) + 1) = " A" + Suit' The first card is an ace.
FOR value = 2 TO 9: Card((i * 13) + value) = STR\$(value) + Suit: NEXT value
Card((i * 13) + 10) = "10" + Suit
Card((i * 13) + 11) = " J" + Suit
Card((i * 13) + 12) = " Q" + Suit
Card((i * 13) + 13) = " K" + Suit
NEXT i
RETURN

 Respond to this message
Pete

LOL - I just posted a similar program at the QB forum 5- min ago in response to Matt!

December 17 2005, 4:21 PM
 Respond to this message
Solitaire
S

Deck of cards shuffled with the Shuffle Sort algorithm

January 12 2008, 4:38 PM
 Complete code is found in the "Proud" subforum: http://www.network54.com/Forum/178387/message/1151010823/Revised+deck+of+cards+with+long+or+short+display%2C+shuffled%2C+colored%2C+w-+or+w-o+jokers
 Respond to this message
qbguy

Card Shuffle

January 31 2008, 4:06 PM
 You could use two arrays instead of a TYPE, but then you would HAVE to use bubblesort. Bubble sort is just used here for ease of coding. DECLARE SUB BUBSORT (A() AS ANY) DEFINT A-Z TYPE CARD VALUE AS INTEGER RADIX AS INTEGER END TYPE DIM A(52) AS CARD FOR I = 1 TO 52 A(I).VALUE = I A(I).RADIX = INT(RND(1) * 32767) NEXT CALL BUBSORT(A()) CLS FOR I = 1 TO 52 SELECT CASE (A(I).VALUE MOD 13) CASE 1 PRINT "A"; CASE 2 TO 9 PRINT CHR\$((A(I).VALUE MOD 13) + 48); CASE 10 PRINT "10"; CASE 11 PRINT "J"; CASE 12 PRINT "Q"; CASE 0 PRINT "K"; END SELECT SELECT CASE (A(I).VALUE - 1) \ 13 CASE 0 PRINT CHR\$(3); CASE 1 PRINT CHR\$(4); CASE 2 PRINT CHR\$(5); CASE 3 PRINT CHR\$(6); END SELECT PRINT SLEEP 1 NEXT SUB BUBSORT (A() AS CARD) FOR I = LBOUND(A) TO UBOUND(A) FOR J = UBOUND(A) TO I + 1 STEP -1 IF A(J - 1).RADIX > A(J).RADIX THEN SWAP A(J - 1), A(J) NEXT NEXT END SUB
 Respond to this message
MONTREALER

Virtual Deck

February 1 2008, 1:29 PM
 Using a virtual deck, the cards in order can be dealt in random order, no longer requiring any method of shuffling. ============================================================================ SCREEN 0 CLS deckofcards\$ = "As 2s 3s 4s 5s 6s 7s 8s 9s 10sJs Qs Ks Ac 2c 3c 4c 5c 6c 7c 8c 9c 10cJc Qc Kc Ad 2d 3d 4d 5d 6d 7d 8d 9d 10dJd Qd Kd Ah 2h 3h 4h 5h 6h 7h 8h 9h 10hJh Qh Kh " ' this is the full deck numberofplayers% = 0 WHILE numberofplayers% < 1 OR numberofplayers% > 5 LOCATE 3, 3 INPUT " Enter the Number of Players (1-5):"; numberofplayers% WEND cardstodeal% = 0 maxcardstodeal% = INT(52 / numberofplayers%) WHILE cardstodeal% < 1 OR cardstodeal% > maxcardstodeal% LOCATE 4, 3 PRINT "Enter the Number of Cards to Deal (1-"; maxcardstodeal%; "):"; INPUT cardstodeal% WEND player\$ = "Player 1 Player 2 Player 3 Player 4 Player 5" LOCATE 6, 4: PRINT LEFT\$(player\$, 15 * numberofplayers%) under\$ = "-------- -------- -------- -------- -------- " LOCATE 7, 4: PRINT LEFT\$(under\$, 15 * numberofplayers%) RANDOMIZE TIMER FOR l1% = 1 TO cardstodeal% FOR l2% = 1 TO numberofplayers% selection% = INT(RND * LEN(deckofcards\$) / 3 + 1) * 3 - 2 ' pick a card z\$ = MID\$(deckofcards\$, selection%, 3) LOCATE 7 + l1%, 15 * l2% - 8: PRINT z\$ deckofcards\$ = LEFT\$(deckofcards\$, selection% - 1) + MID\$(deckofcards\$, selection% + 3, LEN(deckofcards\$) - selection%) ' remove the selected card from the deck = shrink the deck NEXT l2%,l1% PRINT : PRINT PRINT TAB(6); "Remaining cards in the deck: "; IF deckofcards\$ = "" THEN PRINT "None" ELSE PRINT deckofcards\$ END
 Respond to this message

R

You could also use ASCII for the suits

February 2 2008, 10:10 AM
 Instead of a string of letters, just add the ASCII code characters 3 to 6 in the original deck string. Ted
 Respond to this message
Pete

My mini shuffle code...

February 2 2008, 11:36 AM
 RANDOMIZE TIMER: CLS : REDIM card(52) DO UNTIL noc = 52: x = INT(RND * 52) FOR i = 1 TO noc: IF card(i) = x THEN EXIT FOR NEXT IF i > noc THEN noc = noc + 1: card(noc) = x IF CSRLIN > 13 THEN column = column + 20: LOCATE 1 LOCATE , column + 1 SELECT CASE x MOD 13 CASE 1 TO 9: PRINT LTRIM\$(STR\$(x MOD 13 + 1)); CASE 10: PRINT "J"; CASE 11: PRINT "Q"; CASE 12: PRINT "K"; CASE 0: PRINT "A"; END SELECT PRINT CHR\$(3 + x MOD 4) END IF LOOP
 Respond to this message

R

Different way to shuffle card array

October 10 2009, 11:21 AM
 I made a WAR card game player a while ago, and to try to save space, instead of DIMing cards as a string, I took cards and DIMed it as a integer. Treating each variable as it's card respectively(such cards(1) is always going to be the 1 of clubs.) I randomly gave have of the cards variables 1's or 0's(1's being my cards, 0's being the computers). Normally the space saved isn't a big deal, but this could also help your program run faster. simply give each "card" a number between 1 and 52(each having a unique number), and then decide where the card is in the deck(IE. You have the cards shuffled in a pile. The card in the middle(26 card) would be the cards() that = 26, then the array number would represent the card there). This could be faster then using strings(I found it faster because you already have the card value for each card, and you don't need to spend any time figuring that out.) Anyway, that's my take on that.
 Respond to this message
Pete

Here's a small amount of code I wrote a few years ago. It gives value + suit in ASCII.

October 18 2009, 8:03 AM
 RANDOMIZE TIMER: CLS : REDIM card(52) DO UNTIL noc = 52: x = INT(RND * 52) FOR i = 1 TO noc: IF card(i) = x THEN EXIT FOR NEXT IF i > noc THEN noc = noc + 1: card(noc) = x IF CSRLIN > 13 THEN column = column + 20: LOCATE 1 LOCATE , column + 1 SELECT CASE x MOD 13 CASE 1 TO 9: PRINT LTRIM\$(STR\$(x MOD 13 + 1)); CASE 10: PRINT "J"; CASE 11: PRINT "Q"; CASE 12: PRINT "K"; CASE 0: PRINT "A"; END SELECT PRINT CHR\$(3 + x MOD 4) END IF LOOP
 Respond to this message
DSMan195276
R

*Much shorter then mine! :-)

October 21 2009, 2:44 PM
 Respond to this message
Solitaire