;;; Basic guess-a-number game.
;;; The 'mystery number' is randomly generated.
;;; The loop runs until you guess the right number.
;;; The code is very basic. It has been tested on both Linux (Slime + SBCL) and Windows (Lisp in a Box + CLisp)
(defun guess(n a)
(cond((= n a) 'correct-number)
((< n a) 'too-low)
((> n a) 'too-high)))
(defun mystery-number ()
(setq secret-number (random 20))
(format t "~&I am thinking of a number between 0 and 20.~%Enter your guess...~%")
(loop (format t "~&Number: ")
(let ((n (parse-integer (read-line) :junk-allowed t)))
(when (not n) (progn
(format t "~&You entered a string that cannot be parsed as a number")
(return)))
(if (string-equal "correct-number" (guess n secret-number))
(progn
(format t "~&Congratulations! That's the number")
(return))
(format t "~&Hmmm....let me see... ~A~%" (guess n secret-number))))))
No replies to this topic
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









