Contents
Articles
Behaviors
Books
Director News
Director Web Sites
FAQ
Games
Mailing Lists
News Groups
Project Examples
Reviews
Software
Tools
Useful Web Sites
Utilities
Xtras

Don't miss these
Replace tabs with spaces
Drop Shaddow Animating Lame Button-Alphamania
ColorCursor
Generic behavior to set System Colors
Versiown
RavJoystick Xtra
robotduck.com
Puzzler II - Slide Puzzle
Copy matching Pixels
Circular Rollover behavior
 

 

 

Article Building a Generic Card Shuffler

Added on 10/11/1999

 

Compatibilities:
D7 D8 Mac PC Shockwave

This item has not yet been rated

Author: MediaMacros (website)

I have been contracted to design a series of card games for a company. I understand the basic logic for different games like poker or rummy, but how can I set up a good "all purpose" shuffler? I want to use a standard 52 card deck and give the user the ability to trade in certain cards. Where do I start?

Get the source
Sounds like a job for lists.  Lists are great for this type of thing since they are basically a series of values that you can add to, remove from etc. in one simple variable.  Lets start by looking at the basic characteristics of a deck of cards.  You have 4 suits and each has the same cards (2-A or 2-14 in number values).   We can quickly build a list of all values if we assign each suit a number (1-4) and then append that with the number of the card (2-14)  With a repeat loop nested inside another repeat loop we can build this list very quickly via lingo.  Then we name the cards to match so that we can call each card by name.  For example...

The 2 of clubs would be 1-2.  The Ace of spades, 4-14, etc.

Now that we have this "master list" of values we can begin our shuffling procedure.  Using the random command we can grab a card from our virtual deck and place it in any open card space.  We first use the duplicate command to make our current card list equal to the master list.  The reason we use duplicate and not just set one list equal to another is that Director handles list references differently than other variables.  If you set a variable = to a string and then set another variable equal to the first you get a copy that is independent.

With lists you are referencing another variable so we use the duplicate command to make a copy, rather than reference the previous version...

test = []
test2 = duplicate(test)

From here we can use Director's random command with the count property to get a random card out of the list.  Once we do this we can set the suit and card number for that card and then remove that card from the current deck list.  This way we can reference the current list again if we want to swap a card and never get a duplicate a card.

I have added checking for the user to select cards to "trade" to the dealer as well as display for the user and a computer opponent.  Note that this is not a full game and has no AI (artificial intelligence), it is just the shuffling, swapping, and dealing script.  Combine it with your own rules logic for any game to create most any popular card game.  Below is the code.

--Copyright 1999 Chuck Neal
--chuck@mediamacros.com
--If you find this code helpful, send me an e-mail and let me know. :-)

property spriteNum, suit, cardNum, player

global cardList, masterList, activeList, flipList, swap

on getPropertyDescriptionList me
  p_list= [:]
  p_list.addProp(#player, [#format : #string, #default : "Human", #comment : "Which Player:", #range : ["Human", "Computer"]])
  return p_list
end

on beginSprite me
  if masterList = void then
    masterList = []
    --create master list of cards
    repeat with x = 1 to 4
      --add each card
      repeat with y = 2 to 14
        masterList.add(x & "-" & y)
      end repeat
    end repeat
  end if
  if cardList = void then cardList = duplicate(masterList)
  if activeList = void then activeList = []
  shuffle()
end

on shuffle me
  if activeList.getOne(spriteNum) <> 0 then
    --old game list, reset the list
    cardList = duplicate(masterList)
    activeList = []
  end if
  --set a card
  getCard()
  activeList.add(spriteNum)
  if flipList <> [] then flipList = []
  swap = true
end
on mouseUp me
  if flipList = void then flipList = []
  if player = "Human" and swap = true then
    if flipList.getOne(spriteNum) = 0 then
      flipList.add(spriteNum)
      sprite(spriteNum).member = "0-0"
    else
      flipList.deleteOne(spriteNum)
      sprite(spriteNum).member = member(suit & "-" & cardNum)
    end if
  end if
end

on changeCards me
  if flipList.getOne(spriteNum) <> 0 then
    --change the card
    getCard()
    flipList.deleteOne(spriteNum)
  end if
  swap= false
end

on getCard me
  the itemDelimiter = "-"
  whichCard = random(cardList.count)
  suit = cardList[whichCard].item[1]
  cardNum = cardList[whichCard].item[2]
  cardList.deleteAt(whichCard)
  if player = "Human" then
    sprite(spriteNum).member = member(suit & "-" & cardNum)
  else
    sprite(spriteNum).member = member("0-0")
  end if
end

on showCards me
  sprite(spriteNum).member = member(suit & "-" & cardNum)
  swap= false
end

on getBehaviorDescription me
  return "This is a card shuffler, swapper, and basic controller. Add your own AI for any basic card game."
end


Give the script a try. Not that this is not a full game, just a controller for shuffling. (Exhibit 1)

So what now?  Well, pick a game and start working out the logic.   You can add a handler to report back the cards for each player and determine the winner.  Add a little more logic and create a computer player to balance the odds and challenge a human opponent.  Try it out and see what you can come up with.   Happy coding.

 


Contact

MMI
36 South Court Sq
Suite 300
Newnan, GA 30263
USA

Send e-mail