-
Notifications
You must be signed in to change notification settings - Fork 10
/
number-guessing-game.cl
44 lines (41 loc) · 1.54 KB
/
number-guessing-game.cl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
(defparameter **lambdalisp-suppress-repl** t) ;; Enters script mode and suppresses REPL messages
(defun number-guessing-game ()
(let ((n ()) (n-tries 0) (answer (* 3 14)))
(loop
(format t "Let's play a number guessing game. I'm thinking of a number between 1 and 10000.~%")
(format t "Say a number, and I'll tell you if it's less than, greater than, or equal to my number.~%")
(format t "Can you guess what number I'm thinking of?~%")
(format t "Please enter a number:~%")
(block gameloop
(loop
(block input-n
(loop
(format t "number> ")
(finish-output nil)
(setq n (read))
(if (integerp n)
(return-from input-n))
(format t "Please enter a number (you entered ~a).~%" n)))
(setq n-tries (+ 1 n-tries))
(cond
((< n answer)
(format t "Your guess is less than my number.~%"))
((> n answer)
(format t "Your guess is greater than my number.~%"))
(t
(return-from gameloop)))))
(format t "That's right! I was thinking of ~a. Congratulations!~%" n)
(format t "Number of tries: ~a~%" n-tries)
(format t "Do you want to try again? [y/N]~%")
(format t "y/N> ")
(finish-output nil)
(setq n (read))
(cond
((eq n 'y)
(format t "All right!~%")
(setq n-tries 0))
(t
(return))))
(format t "Thank you for playing!~%")
(exit)))
(number-guessing-game)