Skip to content

Instantly share code, notes, and snippets.

@matthew-ball
Created November 29, 2014 14:03
Show Gist options
  • Save matthew-ball/9b22900e70b2112056ef to your computer and use it in GitHub Desktop.
Save matthew-ball/9b22900e70b2112056ef to your computer and use it in GitHub Desktop.
(defmacro add-to-list (object list)
`(setf ,list (push ,object ,list)))
(defun print-card (card stream ignore)
(declare (ignore ignore))
(format stream "~a of ~a" (card-value card) (card-suit card)))
(defstruct (card (:print-function print-card))
suit
value)
(defun card (suit value)
(make-card :suit suit :value value))
(defstruct deck
cards)
(defun deck (suits values)
(let ((deck (make-deck)))
(dolist (suit suits)
(dolist (value values)
(add-to-list (card suit value) (deck-cards deck))))
deck))
;; (defun add-card (card deck)
;; (setf (deck-cards deck) (push card (deck-cards deck))))
;; (defun remove-card (deck)
;; (pop (deck-cards deck)))
(defun shuffle (deck)
(let ((cards (deck-cards deck)))
(dotimes (i (length cards))
(rotatef (nth i cards) (nth (random (length cards)) cards)))
cards))
(defparameter *standard-deck* (deck '(hearts spades diamonds clubs)
'(ace two three four five six seven eight nine ten jack queen king)))
(shuffle *standard-deck*)
(defstruct (hand (:include deck))
size)
(defun hand (size player)
(let ((hand (make-hand :size size)))
(setf (player-hand player) hand)))
(defstruct player
name
hand)
(defun player (name)
(let ((player (make-player :name name)))
player))
(defstruct (dealer (:include player)))
(defun dealer ()
(let ((dealer (make-dealer :name "dealer")))
dealer))
(defun deal (hand &optional (deck *standard-deck*))
(dotimes (i (hand-size hand))
(let ((card (pop (deck-cards deck))))
(add-to-list card (hand-cards hand))))
hand)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment