'=========================================================================== ' Subject: CARD SHUFFLE Date: 06-29-96 (20:16) ' Author: Jonathan Leger Code: QB, QBasic, PDS ' Origin: leger@mail.dtx.net Packet: DEMOS.ABC '=========================================================================== DEFINT A-Z DECLARE SUB MakeDeck (deck$) DECLARE SUB Shuffle (deck$) DECLARE FUNCTION CardValue (card$) DECLARE FUNCTION DrawCard$ (deck$) DECLARE FUNCTION DealCards$ (deck$, numcards) CONST TRUE = -1 CONST FALSE = NOT TRUE CLS MakeDeck newdeck$ '*** Build a deck Shuffle newdeck$ '*** Shuffle the deck FUNCTION CardValue (card$) '*** Returns the value of a card. This should be changed depending on the '*** particular game you are playing. SELECT CASE LEFT$(card$, 1) CASE "A" '*** Aces are worth 15 CardValue = 15 CASE "K", "Q", "J", "0" '*** Face cards and 10 are worth 10 CardValue = 10 CASE ELSE '*** All other cards are worth their '*** numerical value (2C=2, 3C=3, etc.) CardValue = VAL(LEFT$(card$, 1)) END SELECT END FUNCTION FUNCTION DealCards$ (deck$, numcards) '*** Returns the next numcards cards in the deck and cuts those cards '*** out of the deck. This function should be used when a player draws '*** a card (or cards) as well as when dealing. Deal$ = LEFT$(deck$, numcards * 2) deck$ = RIGHT$(deck$, LEN(deck$) - numcards * 2) END FUNCTION DEFSNG A-Z SUB MakeDeck (deck$) '*** Simply stuffs all of the cards in a deck into a string, deck$ deck$ = "2H3H4H5H6H7H8H9H0HJHQHKHAH" deck$ = deck$ + "2S3S4S5S6S7S8S9S0SJSQSKSAS" deck$ = deck$ + "2D3D4D5D6D7D8D9D0DJDQDKDAD" deck$ = deck$ + "2C3C4C5C6C7C8C9C0CJCQCKCAC" END SUB SUB Shuffle (deck$) '*** Copy the deck into a temporary deck so we can mess with the deck. tempdeck$ = deck$ '*** It's good not to have the same deck every time we shuffle, so we '*** generate a random number seed from the CPU's timer. RANDOMIZE TIMER '*** Let's shuffle it! FOR card = 52 TO 1 STEP -1 '*** Chose which card will be the next card "shuffled" into the deck. nextcard = INT(RND * card) + 1 '*** Get the location of that card in the temporary deck. cardloc = ((nextcard - 1) * 2) + 1 '*** Extract the card from the temporary deck. nextcard$ = MID$(tempdeck$, cardloc, 2) '*** Add the new card to the "shuffled" deck. newdeck$ = newdeck$ + nextcard$ '*** Remove the new card from the temporary deck. tempdeck$ = LEFT$(tempdeck$, cardloc - 1) + RIGHT$(tempdeck$, LEN(tempdeck$) - cardloc - 1) NEXT card '*** Copy the new "shuffled" deck into the deck variable passed to the sub. deck$ = newdeck$ END SUB