dealing cards in Clojure

Posted by Ralph on Stack Overflow See other posts from Stack Overflow or by Ralph
Published on 2010-04-24T12:56:36Z Indexed on 2010/04/24 13:03 UTC
Read the original article Hit count: 289

Filed under:
|

I am trying to write a Spider Solitaire player as an exercise in learning Clojure. I am trying to figure out how to deal the cards.

I have created (with the help of stackoverflow), a shuffled sequence of 104 cards from two standard decks. Each card is represented as a

(defstruct card :rank :suit :face-up)

The tableau for Spider will be represented as follows:

(defstruct tableau :stacks :complete)

where :stacks is a vector of card vectors, 4 of which contain 5 cards face down and 1 card face up, and 6 of which contain 4 cards face down and 1 card face up, for a total of 54 cards, and :complete is an (initially) empty vector of completed sets of ace-king (represented as, for example, king-hearts, for printing purposes). The remainder of the undealt deck should be saved in a ref

(def deck (ref seq))

During the game, a tableau may contain, for example:

(struct-map tableau
  :stacks [[AH 2C KS ...]
           [6D QH JS ...]
           ...
           ]
  :complete [KC KS])

where "AH" is a card containing {:rank :ace :suit :hearts :face-up false}, etc.

How can I write a function to deal the stacks and then save the remainder in the ref?

© Stack Overflow or respective owner

Related posts about clojure

Related posts about refs