From eb0eb13f45f336d4a1f40411ab72b053fc359079 Mon Sep 17 00:00:00 2001 From: eduardoroboto Date: Sun, 12 May 2024 20:22:15 -0300 Subject: [PATCH] Everything done, up to chapter 6 * Wizard Games * Examples --- example.lisp | 24 ++++++++++++++++++++++++ wizard_game.lisp | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 example.lisp diff --git a/example.lisp b/example.lisp new file mode 100644 index 0000000..cefde11 --- /dev/null +++ b/example.lisp @@ -0,0 +1,24 @@ + + +;; (defun say-hello() +;; (print "Please type your name:") +;; (let ((name (read))) +;; (print "Nice to meet you, ") +;; (print name))) + + +(defun add-five () + (print "please enter a number:") + (let ((num (read))) + (print "When I add five I get") + (print (+ num 5)))) + + +(defun say-hello () + (princ "Please type your name:") + (let ((name (read-line))) + (princ "Nice to meet you, ") + (princ name))) + + +(defparameter *foo* '(+ 1 2)) diff --git a/wizard_game.lisp b/wizard_game.lisp index 6397a8a..65e7c59 100644 --- a/wizard_game.lisp +++ b/wizard_game.lisp @@ -77,3 +77,48 @@ (defun inventory () (cons 'items (objects-at 'body *objects* *object-locations*))) + + +(defun game-repl () + (let ((cmd (game-read))) + (unless (eq (car cmd) 'quit) + (game-print (game-eval cmd)) + (game-repl)))) + + +(defun game-read () + (let ((cmd (read-from-string + (concatenate 'string "(" (read-line) ")" )))) + (flet ((quote-it (x) + (list 'quote x))) + (cons (car cmd) (mapcar #'quote-it (cdr cmd)))))) + +(defparameter *allowed-commands* '(look walk pickup inventory)) + + +(defun game-eval (sexp) + (if (member (car sexp) *allowed-commands*) + (eval sexp) + '(i do not know that command.))) + + +(defun tweak-text (lst caps lit) + (when lst + (let ((item (car lst)) + (rest (cdr lst))) + (cond ((eq item #\space) (cons item (tweak-text rest caps lit))) + ((member item '(#\! #\? #\.)) (cons item (tweak-text rest t lit))) + ((eq item #\") (tweak-text rest caps (not lit))) + (lit (cons item (tweak-text rest nil lit))) + ((or caps lit) (cons (char-upcase item) (tweak-text rest nil lit))) + (t (cons (char-downcase item) (tweak-text rest nil nil))))))) + + +(defun game-print (lst) + (princ (coerce (tweak-text (coerce (string-trim "() " + (prin1-to-string lst)) + 'list) + t + nil) + 'string)) + (fresh-line)) -- 2.45.2