Skip to content

Instantly share code, notes, and snippets.

@takoeight0821
Created April 19, 2015 07:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takoeight0821/549ad0c2d3659153e0d4 to your computer and use it in GitHub Desktop.
Save takoeight0821/549ad0c2d3659153e0d4 to your computer and use it in GitHub Desktop.
じゃんけん
;;; * (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