Shuffle Cards NetLogo Model

Produced for the book series "Exercises for Artificial Intelligence";

Author: W. J. Teahan; Publisher: Ventus Publishing Aps.

powered by NetLogo

view/download model file: Shuffle-Cards.nlogo


This model shows how you can create a pack of cards using turtle shapes and then shuffle them using the shuffle command.


It's purpose is to illustrate that a lot can be done using just turtle shapes.


The model uses three breeds of turtle agents to represent each card in the pack. These are: cards, suits and ranks. Each of these agents has a particular shape which is used to visually depict a card in the pack when it is shown. The card turtle agent owns a suit (e.g. clubs, spades, diamonds and hearts; this is represented by a suit turtle agent) and two ranks (e.g. Ace, King, represented by two rank turtle agents). These ranks appear at the top and bottom of a card - each of these is the 180 degrees rotation of the other.

Each card in the pack is created as separate agents (card, card-suit, card-rank-1 and card-rank-2) and this is then stored in a global list called the pack.


Select the slider values you want, then press the Shuffle button to display a shuffled deck of cards.


The model's Interface button and sliders are defined as follows:

- Shuffle: This button clears the environment, creates a new pack, then shuffles it, displaying the result in the environment.

- card-size: This is the size of each card.

- suit-size: This is the size of each suit depicted in the middle of each card.

- rank-size: This is the size of each rank depicted to the top left and bottom right of each card.


Notice the extra shapes that have been added to the shapes library for the model by running the Turtles Shapes Editor in the Tools menu of the Interface. You will find a number of shapes added - these begin with "number: ..." and "suit: ...".

Notice how the card shape has been drawn using the shapes editor. Click on the outside of the card to verify that the thin outside line exists in the shape specification.

Notice the order that the agent positions are specified using the setxy command in the shuffle-pack-of-cards procedure for the three types of agents that are used to depict each card. The order is the card agent drawn first, followed by the card-suit, card-rank-1 and card-rank-2 agents. This ensures that the card agent appears underneath the other three agents and does not obscure them.


Try changing the values of the sliders to see what happens.

Try commenting out the last line of the setup procedure (set pack shuffle-pack-of-cards pack -90 30) to see what happens.

Have a go at improving the letter and number shapes that are used to specify the card rank.


Create a model that can be used to play a card game. One trick that you can use is to hide the cards in the environment (using the hide-turtle command) and only show them when required. (See the Shuffle and Deal model to see how this could be done).


The shuffle command in NetLogo makes shuffling the pack of cards very easy.


See the Shuffle and Deal model.


To refer to this model in publications, please use:

Teahan, W. J. (2010). Shuffle and Deal NetLogo model.
Exercisaes for Artificial Intelligence. Ventus Publishing Aps.


; Shuffle Cards model.
; Shuffles a pack of cards.
; Written by William John Teahan (2010)
; Copyright 2009 William John Teahan. All Rights Reserved.

breed [cards card]   ; a pack of cards
breed [suits suit]   ; clubs, spades, diamonds, hearts
breed [ranks rank]   ; Ace, King, Queen, Jack, Ten, ...

[ card-suit          ; an agent: the card's suit
  card-rank-1        ; an agent: the rank of the card shown top-left e.g. Ace, King, Queen, Jack, Ten, ...
  card-rank-2        ; an agent: the rank of the card shown bottom-right e.g. Ace, King, Queen, Jack, Ten, ...

[ pack               ; the pack of cards

to setup
; Setups the model.

  let this-card nobody
  ask patches
    set pcolor 89 ;; make background full of light cyan coloured patches

  set pack create-pack-of-cards -90 30
  set pack shuffle-pack-of-cards pack -90 30

to-report create-pack-of-cards [start-x start-y]
; Creates and reports a list of cards in sorted order.

  let x 0
  let y 0
  let this-suit nobody
  let this-card nobody
  let this-pack []

  set y start-y  
  foreach ["suit: clubs" "suit: spades" "suit: hearts" "suit: diamonds"]
    set x start-x
    set this-suit ?
    foreach ["card: Ace" "card: King" "card: Queen" "card: Jack" "number: Ten"
             "number: Nine" "number: Eight" "number: Seven" "number: Six"
             "number: Five" "number: Four" "number: Three" "number: Two"]
      set this-card create-card this-suit ? x y
      set this-pack fput this-card this-pack
      set x x + 15 
    set y y - 22
  report this-pack

to-report shuffle-pack-of-cards [this-pack start-x start-y]
; Shuffles the pack of cards, and returns it.

  set pack shuffle pack ; randomise the pack

  ; also need to reset the x, y co-ordinates
  let x start-x
  let y start-y
  let p 0
  foreach pack
    if (p mod 13 = 0) and (p != 0)
      [ set x start-x
        set y y - 22
    ask ?
      setxy x y
      ask card-suit
      [ setxy x y ]
      ask card-rank-1
      [ setxy x - 4 y + 6]
      ask card-rank-2
      [ setxy x + 4 y - 6 ]
    ] ; reset x and y positions for the card
    set x x + 15
    set p p + 1
  report pack

to dump-pack-of-cards [this-pack]
; Dumps the pack of cards (for debugging purposes).

  print this-pack

to-report card-colour [this-shape]
; Reports the colour of the card whose suit is specified
; by this-shape.

  ifelse (this-shape = "suit: clubs") or (this-shape = "suit: spades")
    [ report black ]
    [ report red ]

to-report create-card [this-card-suit this-card-rank x y]
; Creates and returns a new card with the suit specified
; by this-shape and places it at co-ordinates (x,y).

  let this-card nobody
  let this-suit nobody
  let this-rank-1 nobody
  let this-rank-2 nobody

  let this-colour card-colour this-card-suit
  create-suits 1
    set this-suit self
    set size suit-size
    set shape this-card-suit
    set color this-colour
    setxy x y

  create-ranks 1
    set this-rank-1 self
    set size rank-size
    set shape this-card-rank
    set heading 0
    set color this-colour
    setxy x - 4 y + 6

  create-ranks 1
    set this-rank-2 self
    set size rank-size
    set shape this-card-rank
    set heading 180
    set color this-colour
    setxy x + 4 y - 6

  create-cards 1
    set this-card self
    set size card-size
    set shape "card"
    set color white
    set card-suit this-suit
    set card-rank-1 this-rank-1
    set card-rank-2 this-rank-2
    setxy x y

  report this-card
; Copyright 2010 by William John Teahan.  All rights reserved.
; Permission to use, modify or redistribute this model is hereby granted,
; provided that both of the following requirements are followed:
; a) this copyright notice is included.
; b) this model will not be redistributed for profit without permission
;    from William John Teahan.
; Contact William John Teahan for appropriate licenses for redistribution for
; profit.
; To refer to this model in publications, please use:
; Teahan, W. J. (2010).  Shuffle-Cards NetLogo model.
;   Artificial Intelligence. Ventus Publishing Aps