The QBasic Forum      Other Subforums, Links and Downloads
  Next Topic >>Return to Index  

Deck of Cards: Shuffling and dealing hands

December 17 2005 at 7:02 AM
  (Premier Login iorr5t)
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   
AuthorReply
Pete
(no login)

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
(Login Solitaire1)
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
(no login)

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
(no login)

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   

(Login burger2227)
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
(Login The-Universe)
Admin

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   

(Login DSMan195276)
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
(Login The-Universe)
Admin

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
(Login DSMan195276)
R

*Much shorter then mine! :-)

October 21 2009, 2:44 PM 


 
 Respond to this message   
Solitaire
(Login Solitaire1)
S

A much longer program to shuffle deck of cards

November 7 2009, 6:57 AM 

I wrote this several years ago. It displays card name in black and red over a white background, includes option to display short or long names, may add or remove jokers, and can reshuffle numerous times.

=============================================================================

DIM N AS INTEGER, suit AS INTEGER, rank AS INTEGER, x AS INTEGER
DIM J AS INTEGER, D AS INTEGER, mixup AS INTEGER
DIM A AS STRING, B AS STRING, temp AS STRING, E AS STRING
RANDOMIZE TIMER
N = 52: J = 0: D = 0
COLOR 8, 7
DO
    CLS : PRINT , " Display ordered deck of"; N; "cards"
    PRINT
    REDIM deck(N) AS STRING
    FOR suit = 1 TO 4
        SELECT CASE suit
            CASE 1
                IF D = 0 THEN B$ = " of Hearts" ELSE B$ = CHR$(3)
            CASE 2
                IF D = 0 THEN B$ = " of Spades" ELSE B$ = CHR$(6)
            CASE 3
                IF D = 0 THEN B$ = " of Diamonds" ELSE B$ = CHR$(4)
            CASE 4
                IF D = 0 THEN B$ = " of Clubs" ELSE B$ = CHR$(5)
        END SELECT
        FOR rank = 1 TO 13
            x = rank + ((suit - 1) * 13)
            SELECT CASE rank
                CASE 1
                    IF D = 0 THEN A$ = " Ace" ELSE A$ = " A"
                CASE 11
                    IF D = 0 THEN A$ = " Jack" ELSE A$ = " J"
                CASE 12
                    IF D = 0 THEN A$ = " Queen" ELSE A$ = " Q"
                CASE 13
                    IF D = 0 THEN A$ = " King" ELSE A$ = " K"
                CASE ELSE
                    A$ = STR$(rank)
            END SELECT
            IF D = 0 THEN deck$(x) = A$ + B$ ELSE deck(x) = " " + B$ + A$
        NEXT rank
    NEXT suit
    IF N = 54 THEN
        IF D = 0 THEN deck$(53) = " Joker" ELSE deck$(53) = " ? ?"
        IF D = 0 THEN deck$(54) = " Joker" ELSE deck$(54) = " ? ?"
    END IF
    FOR rank = 1 TO 13
        x = rank
        FOR suit = 1 TO 4
            IF INSTR(deck$(x), "Hearts") OR INSTR(deck$(x), "Diamonds") OR INSTR(deck$(x), CHR$(3)) OR INSTR deck$(x), CHR$(4)) THEN
                COLOR 4
            ELSE
                COLOR 0
            END IF
            PRINT TAB(suit * 20 - 20); deck$(x);
            x = x + 13
        NEXT suit
        PRINT
    NEXT rank
    IF N = 54 THEN
        COLOR 1
        PRINT deck$(53); TAB(20); deck$(54)
    END IF
    COLOR 8
    PRINT : PRINT : PRINT TAB(8); "Press any key to shuffle the deck."
    E$ = INPUT$(1)
    DO
        CLS : PRINT , " Display shuffled deck of"; N; "cards"
        PRINT
        FOR x = 1 TO N       'swap array positions randomly
            mixup = INT(RND * N - x + 1) + x
            temp$ = deck$(mixup)
            deck$(mixup) = deck$(x)
            deck$(x) = temp$
        NEXT x

        FOR rank = 1 TO 13
            x = rank
            FOR suit = 1 TO 4
                IF INSTR(deck$(x), "Hearts") OR INSTR(deck$(x), "Diamonds") OR INSTR(deck$(x), CHR$(3)) OR INSTR(deck$(x), CHR$(4)) THEN
                    COLOR 4
                ELSEIF deck$(x) = " Joker" OR deck$(x) = " ? ?" THEN
                    COLOR 1
                ELSE
                    COLOR 0
                END IF

                PRINT TAB(suit * 20 - 20); deck$(x);
                x = x + 13
            NEXT suit
            PRINT
        NEXT rank
        IF N = 54 THEN
            FOR x = 53 TO 54
                IF INSTR(deck$(x), "Hearts") OR INSTR(deck$(x), "Diamonds") OR INSTR(deck$(x), CHR$(3)) OR INSTR(deck$(x), CHR$(4)) THEN
                    COLOR 4
                ELSEIF deck$(x) = " Joker" OR deck$(x) = " ? ?" THEN
                    COLOR 1
                ELSE
                    COLOR 0
                END IF
                PRINT deck$(x); TAB(20);
            NEXT x
        END IF
        COLOR 8
        PRINT : PRINT
        PRINT TAB(4); "Press any key to shuffle the deck again, Enter to reorder, J to include"
        PRINT TAB(4); "or remove jokers, D to change long or short display, Q or Esc to quit."
        E$ = UCASE$(INPUT$(1))
        IF E$ = "J" THEN J = NOT J
        IF E$ = "D" THEN D = NOT D
        IF J = 0 THEN N = 52 ELSE N = 54
    LOOP UNTIL E$ = "Q" OR E$ = CHR$(27) OR E$ = CHR$(13) OR E$ = "J" OR E$ = "D"
LOOP UNTIL E$ = "Q" OR E$ = CHR$(27)
COLOR 7, 0
END


 
 Respond to this message   
Current Topic - Deck of Cards: Shuffling and dealing hands
  Next Topic >>Return to Index  

Newbies usually go to www.qbasic.com and click on The QBasic Forum
Forum regulars have their own ways, which include The QBasic Community Forums