Created
April 19, 2015 07:32
-
-
Save takoeight0821/549ad0c2d3659153e0d4 to your computer and use it in GitHub Desktop.
じゃんけん
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
;;; * (read-hand) | |
;;; > rock | |
;;; ROCK | |
;;; > very-dangerous-words | |
;;; hands: rock, paper, scissor | |
;;; > | |
(defun read-hand () | |
(princ "> ") | |
(finish-output) ;SBCLの最適化によるIO順序の入れ替わりを防ぐ | |
(let ((p-hand (read))) | |
(if (member p-hand '(rock paper scissor)) | |
p-hand | |
(progn (princ "hands: rock, paper, scissor") (fresh-line) (finish-output) (read-hand))))) | |
(defun select-hand () | |
(case (random 3 (make-random-state t)) | |
(0 'rock) | |
(1 'paper) | |
(2 'scissor))) | |
(defun eval-hand (p-hand np-hand) | |
(cond ((eq p-hand np-hand) "draw") | |
((member (cons p-hand np-hand) '((rock . scissor) (scissor . paper) (paper . rock)) :test #'equal) "you win") | |
(t "you lose"))) | |
;;; ROCK vs SCISSOR -> you win | |
(defun print-hand (p-hand np-hand game-result) | |
(format t "~A vs ~A -> ~A~%" p-hand np-hand game-result)) | |
(defun game-loop () | |
(let ((p-hand (read-hand)) (np-hand (select-hand))) | |
(print-hand p-hand np-hand (eval-hand p-hand np-hand))) | |
(game-loop)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment