cl/guess.lisp
2024-01-03 18:17:45 +00:00

25 lines
573 B
Common Lisp
Executable file

(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))
)