;; Name Nhi Nguyen ;; Class (CECS 342-07) ;; Project Name (Prog 4 - Fibonacci Solitaire using LISP) ;; Due Date (11/09/2023) ;; ;; I certify that this program is my own original work. I did not copy any part of this program from ;; any other source. I further certify that I typed each and every line of code in this program (defparameter *deck* ()) (defparameter *hearts* '((A "H") (2 "H") (3 "H") (4 "H") (5 "H") (6 "H") (7 "H") (8 "H") (9 "H") (T "H") (J "H") (Q "H") (K "H"))) (defparameter *spades* '((A "S") (2 "S") (3 "S") (4 "S") (5 "S") (6 "S") (7 "S") (8 "S") (9 "S") (T "S") (J "S") (Q "S") (K "S"))) (defparameter *clubs* '((A "C") (2 "C") (3 "C") (4 "C") (5 "C") (6 "C") (7 "C") (8 "C") (9 "C") (T "C") (J "C") (Q "C") (K "C"))) (defparameter *diamonds* '((A "D") (2 "D") (3 "D") (4 "D") (5 "D") (6 "D") (7 "D") (8 "D") (9 "D") (T "D") (J "D") (Q "D") (K "D"))) (defun concat-lists (seq1 seq2) (if (null seq1) seq2 (cons (car seq1) (concat-lists (cdr seq1) seq2)))) (defun new-deck() (setq *deck* ()) (setq *deck* (concat-lists *deck* *diamonds* )) (setq *deck* (concat-lists *deck* *hearts* )) (setq *deck* (concat-lists *deck* *spades* )) (setq *deck* (concat-lists *deck* *clubs* )) ) (defun get-value (card) (cond ((or (eq card 'A) (eq card '1)) 1) ((member card '(T J Q K)) 10) ((numberp card) card) (t (error "Invalid card: ~a" card))) ) (defun fibo (N) (if (or (= N 0) (= N 1)) 1 (let ((F1 (fibo (- N 1))) (F2 (fibo (- N 2)))) (+ F1 F2))) ) (defun is-fibo (sum) (let ((curr 0) (result 0)) (loop while (<= result sum) do (incf curr) (setf result (fibo curr)) (when (= result sum) (return t)))) ) (defun display-deck() (format t "~a~%" *deck*) ) (defun shuffle-deck() (loop for i from 0 below (length *deck*) for j = (random (length *deck*)) do (rotatef (nth i *deck*) (nth j *deck*))) (format t "Deck shuffled!~%") ) (defun play-solitaire() (let ((*deck* (new-deck)) (sum 0) (piles-num 0) (win nil) (card nil) (first-card nil) (value 0)) (shuffle-deck) (loop while (/= (length *deck*) 0) do (setf card (pop *deck*)) (setf first-card (first card)) (setf value (get-value first-card)) (setf sum (+ sum value)) (when (is-fibo sum) (format t "~a, Fibo: ~b~% " card sum) (incf piles-num) (setf sum 0)) (when (/= (length *deck*) 0) (setf win (is-fibo sum))) finally (if win (format t "Winner in ~a piles" piles-num) (format t "Loser in ~a piles" piles-num)))) ) (defun play-until-win() (format t "You won after hands!~%") ) (loop (format t "1- New Deck~%") (format t "2- Display Deck~%") (format t "3- Shuffle Deck~%") (format t "4- Play Solitaire~%") (format t "5- Play until Win~%") (format t "6- Exit~%") (format t "Enter choice 1 - 6: ") (case (read) (1 (new-deck)) (2 (display-deck)) (3 (shuffle-deck)) (4 (play-solitaire)) (5 (play-until-win)) (6 (return(format t "Exit~%"))) (t (format t "Invalid choice!~%"))) )
Write, Run & Share Common Lisp code online using OneCompiler's Common Lisp online compiler for free. It's one of the robust, feature-rich online compilers for Common Lisp language, running the latest Common Lisp version 5.3. Getting started with the OneCompiler's Common Lisp editor is easy and fast. The editor shows sample boilerplate code when you choose language as Common Lisp and start coding.
OneCompiler's Common Lisp online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Common Lisp program which takes name as input and prints hello message with your name.
(setq name (read))
(princ "Hello ")
(write name)
Common Lisp is a generic language suitable for a wide range of industry applications. It is often referred as Programmable programming language because of it's high extensibility, machine independence, extensive control structures, dynamic updation of programs etc.
Common LISP was invented by John McCarthy in 1958 and was first implemenyted by Steve Russell on an IBM 704 computer.
defvar
keyword and these variables will be in effect until a new value is assigned.(defvar x 10)
(write x)
let
and prog
are used to declare local variables.(let ((var1 value1) (var2 value2).. (varn valuen))<expressions>)
setq
(setq a 10)
This is the simplest looping mechanism in LISP. This allows the execute the set of statements repeatedly until a return statement is encountered.
(loop (s-expressions))
For loop is used to iterate a set of statements based on a condition.
(loop for loop-variable in <a list>
do (action)
)
Do is also used to iterate a set of statements and then check the condition
(do ((var1 val1 updated-val1)
(var2 val2 updated-val2)
(var3 val3 updated-val3)
...)
(test return-value)
(s-expressions)
)
Dotimes is used to iterate for fixed number of iterations.
(dotimes (n val)
statements