Guest User

robots game

a guest
Mar 19th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 4.07 KB | None | 0 0
  1. > robots.lisp
  2. (import 'charset:utf-8 'keyword)
  3.  
  4. ; Original code: http://landoflisp.com/robots.lisp
  5.  
  6. ; To get the flag you'll have to get a shell...
  7.  
  8. (defun readfile (file)
  9.   (let ((in (open file :if-does-not-exist nil)))
  10.     (when in
  11.       (loop for line = (read-line in nil)
  12.             while line do (format t "~a~%" line))
  13.       (close in))))
  14.  
  15. (defun win ()
  16.   (format t (colorize 'green "~%Congratulations, you won!!!"))
  17.   (loop
  18.     (format t "~%What do you want to do?")
  19.     (format t "~%~{~a to read a file, ~a to play again, ~a to quit~}~%> "
  20.             (mapcar (lambda (x) (colorize 'green x)) '("r" "c" "q")))
  21.     (setq c (read))
  22.     (ccase c
  23.            ('r (progn
  24.                  (format t "File? (FYI flag is not in \"flag\", \"key\", etc.)~%> ")
  25.                  (readfile (read-line))))
  26.            ('c (return 1))
  27.            ('q (return 0)))))
  28.  
  29. (defun fail ()
  30.   (format t (colorize 'red "You lost!")) 0)
  31.  
  32. (defun colorize (color text)
  33.   (setq colors '((gray . 30) (red . 31) (green . 32) (yellow . 33) (blue . 34)))
  34.  
  35.   (format nil
  36.           "~c[1;~am~a~c[0m" #\ESC
  37.           (cdr (assoc color colors))
  38.           text #\ESC))
  39.  
  40. (defun play-game ()
  41.   "Returns 1 if player won and wishes to continue, 0 otherwise"
  42.   (loop named main
  43.         with directions = '((q . -65) (w . -64) (e . -63) (a . -1)
  44.                                       (d .   1) (z .  63) (x .  64) (c . 65))
  45.         for pos = 544
  46.         then (progn (format t
  47.                             "~%~{~a/~a/~a to move, (~a)eleport, (~a)eave: ~}"
  48.                             (mapcar (lambda (x) (colorize 'blue x)) '("qwe" "asd" "zxc" "t" "l")))
  49.                     (force-output)
  50.                     (let* ((c (read))
  51.                            (d (assoc c directions)))
  52.                       (cond (d (+ pos (cdr d)))
  53.                             ((eq 't c) (random 1024))
  54.                             ((eq 'l c) (progn
  55.                                         (format t (colorize 'yellow "Good-bye!"))
  56.                                         (return-from main 0)))
  57.                             (t pos))))
  58.  
  59.         for monsters = (loop repeat 40
  60.                              collect (random 1024))
  61.         then (loop for mpos in monsters
  62.                    collect (if (> (count mpos monsters) 1)
  63.                              mpos
  64.                              (cdar (sort (loop for (k . d) in directions
  65.                                                for new-mpos = (+ mpos d)
  66.                                                collect (cons (+ (abs (- (mod new-mpos 64)
  67.                                                                         (mod pos 64)))
  68.                                                                 (abs (- (ash new-mpos -6)
  69.                                                                         (ash pos -6))))
  70.                                                              new-mpos))
  71.                                          '<
  72.                                          :key #'car))))
  73.         do (progn
  74.              (format t
  75.                      "~a~%║~{~<║~%║~,650:;~a~>~}║~a"
  76.                      (format nil "~%╔~{~<-~%-~,650:;~64,1,1,'═A~>~}╗" '("╡ GAME ╞"))
  77.                      (loop for p
  78.                            below 1024
  79.                            collect (cond ((member p monsters)
  80.                                           (cond ((= p pos) (return-from main (fail)))
  81.                                                 ((> (count p monsters) 1) (colorize 'yellow #\✟))
  82.                                                 (t (colorize 'red #\☠))))
  83.                                          ((= p pos)
  84.                                           (colorize 'green #\☺))
  85.                                          (t (colorize 'gray #\ ))))
  86.                      (format nil "~%╚~{~<-~%-~,650:;~64,1,1,'═A~>~}╝" '("")))
  87.              )
  88.         when (loop for mpos in monsters
  89.                    always (> (count mpos monsters) 1))
  90.         return (win)
  91.         ))
  92.  
  93. (handler-case
  94.   (loop while (= (play-game) 1))
  95.   (error (e) (write-line "Invalid command")))
Add Comment
Please, Sign In to add comment