From d4e8f66c8bed16245328a010e55541d6265af5e0 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Fri, 14 Feb 2025 23:27:06 +0000 Subject: Implement a read -> redeal -> print loop Like REPL stands for Read -> Evaluate -> Print -> Loop, here we implement a similar loop which reads a set of indices for cards to remove from the user, redeals those cards then loops back. This keeps happening until either: - the user does not want to redeal (empty input) - there aren't enough cards for a redeal --- src/main.lisp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'src/main.lisp') diff --git a/src/main.lisp b/src/main.lisp index 422beab..8a0631d 100644 --- a/src/main.lisp +++ b/src/main.lisp @@ -62,6 +62,24 @@ (->> (make-deck) alexandria:shuffle (split 5))) +(defun print-hand (hand) + (->> hand cardset->str (format t "Hand=[~a]~%"))) + +(defun read-redeal-print (hand deck) + (cond + ((<= (length deck) 5) + (cons hand deck)) + ((null hand) + (destructuring-bind ((hand) . deck) (deal-hands 1 deck) + (print-hand hand) + (read-redeal-print hand deck))) + (t + (let ((indices (read-until-valid-integers))) + (if (null indices) + (cons hand deck) + (destructuring-bind (hand . deck) (redeal-hand hand indices deck) + (print-hand hand) + (read-redeal-print hand deck))))))) (defun start () (setf *random-state* (make-random-state t)) -- cgit v1.2.3-13-gbd6f