Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
;;; * (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))
(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)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.