Introduction


T
he book shows how to shuffle a stack or list of cards. The easiest method to shuffle a stack of 32 cards is to generate a random number from 1 to 32 and move that card to the top of a new stack. Then repeat this process until all cards have been moved. There is only one problem: To ensure to not draw the same card twice.

T
o ensure this you can either check if the drawn card already is in the destination stack or to remove it from the source stack after moving. The first method seems to be easier to implement but it is very dangerous. In the case the random number generator returns bad numbers only the shuffle function may hang in an endless-loop because the function wants to move the same card again and again. So the second method is much saver. But it is a bit more difficult to implement.
Shuffling cards


T
he shuffling function is using a for-loop for every card in the source stack. In that loop it draws a random card from 1 to the number of cards in that stack. That card is being moved to the result stack and then removed from the source stack. In the next loop the upper limit for the random number is reduced by 1.

'Author     : Daniel, Master Sourcerer at Kitana's Cas...
'Last change: August 1, 2001
'Email      : sourcerer@kitana.org


$LIB ALL OFF

DIM CARDNAME$(7),CARDCOLOR%(3)

'Initialize random number generator
RANDOMIZE TIMER

'Read card names and colors
FOR I%=0 TO 3:READ CARDCOLOR%(I%):NEXT I%
FOR I%=0 TO 7:READ CARDNAME$(I%):NEXT I%

'Create a list of all 32 cards
CARDS$=""
FOR I%=1 TO 32
  CARDS$=CARDS$+CHR$(I%)  'every character represents ...
NEXT I%

'Shuffe cards
CARDS$=SHUFFLE$(CARDS$)

'Show shuffled cards
CLS
X%=2:Y%=1  'output position
FOR I%=1 TO LEN(CARDS$)
  C%=ASC(MID$(CARDS$,I%,1))-1  'draw card
  CL%=INT(C%/8)                'card color
Arena Play Ground Throne Room Theatre Hall Library Back Print Next page Last page