add guessing game

This commit is contained in:
abbie 2024-01-03 18:17:45 +00:00
parent ff9844fa41
commit da1c4fcb34
Signed by: threeoh6000
GPG key ID: 801FE4AD456E922C

25
guess.lisp Executable file
View file

@ -0,0 +1,25 @@
(setf *random-state* (make-random-state t))
(defparameter *ans* (+ 1 (random 9)))
(defvar *guess*)
(defparameter *lives* 3)
(loop
(if (= *lives* 0)
(return (format t "Game over!~%"))
(format t "~%"))
(format t "Guess a number:~%")
(defparameter *guess* (read))
(handler-case
(check-type *guess* number)
(simple-type-error ()
(defparameter *guess* 0)
)
)
(cond
((= *guess* *ans*) (return (format t "Correct!~%")))
((> *guess* *ans*) (format t "Too high!~%"))
((< *guess* *ans*) (format t "Too low!~%"))
)
(defparameter *lives* (- *lives* 1))
)