Last active
December 15, 2015 16:39
-
-
Save flada-auxv/5290501 to your computer and use it in GitHub Desktop.
Land of Lisp 第11章 loopとformatを悪用したジョークスクリプト 終盤のformatとかよく分かってない。。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defparameter *robot-num* 10) | |
(defun robots () | |
;; namedによって(return-from main とループを抜けれるように名前を持たせている | |
(loop named main | |
;; 幅64のゲーム盤におけるオフセット | |
;; withはループ内にローカル変数を作成する | |
with directions = '((q . -65) (w . -64) (e . -63) | |
(a . -1) (d . 1) | |
(z . 63) (x . 64) (c . 65)) | |
for pos = 544 | |
then (progn (format t "~%qwe/asd/zxc to move, (t)eleport, (l)eave:") | |
(force-output) | |
(let* ((c (read)) | |
(d (assoc c directions))) ;; assocで入力からオフセットを得る | |
(cond (d (+ pos (cdr d))) | |
((eq 't c) (random 1024)) ;; 縦16*横64=1024 | |
((eq 'l c) (return-from main 'bye)) | |
(t pos)))) | |
for monsters = (loop repeat *robot-num* | |
collect (random 1024)) | |
then (loop for mpos in monsters | |
collect (if (> (count mpos monsters) 1) | |
mpos | |
(cdar (sort (loop for (k . d) in directions | |
for new-mpos = (+ mpos d) | |
;; マンハッタン距離の計算 | |
collect (cons (+ (abs (- (mod new-mpos 64) | |
(mod pos 64))) | |
(abs (- (ash new-mpos -6) | |
(ash pos -6)))) | |
new-mpos)) | |
'< | |
:key #'car)))) | |
when (loop for mpos in monsters | |
always (> (count mpos monsters) 1)) | |
return 'player-wins | |
do (format t | |
"~%|~{~<|~%|~,65:;~A~>~}|" | |
(loop for p | |
below 1024 | |
collect (cond ((member p monsters) | |
(cond ((= p pos) (return-from main 'player-loses)) | |
((> (count p monsters) 1) #\#) | |
(t #\A))) | |
((= p pos) | |
#\@) | |
(t | |
#\ )))))) | |
(princ (robots)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment