Skip to content

Instantly share code, notes, and snippets.

@tkych
Created December 6, 2013 14:15
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 tkych/7824725 to your computer and use it in GitHub Desktop.
Save tkych/7824725 to your computer and use it in GitHub Desktop.
Tick Tack Toe, ver.2, ビット演算を用いて書き直しました。
;;;; Last modified: 2013-12-06 23:14:02 tkych
;; ビット演算を用いて書き直しました。
;;====================================================================
;; Tick Tack Toe, ver.2
;;====================================================================
;; - [Tick-Tack-Toe 〜 横へな 2012.7.6](http://nabetani.sakura.ne.jp/hena/1/)
;;--------------------------------------------------------------------
;; Package
;;--------------------------------------------------------------------
(in-package :cl-user)
(defpackage :tick-tack-toe-v2 (:use :cl))
(in-package :tick-tack-toe-v2)
;;--------------------------------------------------------------------
;; Main
;;--------------------------------------------------------------------
;; MEMO:
;; pos: 876543210
;; bits: #b*********
(defparameter *win-board-pattern*
'(#b000000111 ; row1
#b000111000 ; row1
#b111000000 ; row3
#b001001001 ; col1
#b010010010 ; col2
#b100100100 ; col3
#b001010100 ; diag tr-bl
#b100010001 ; diag tl-br
))
(defun winp (board)
(some (lambda (pat) (= pat (logand board pat)))
*win-board-pattern*))
(defun main (input)
(let ((board0 #b000000000) ; for player o
(board1 #b000000000)) ; for player x
(loop :for player := 0 :then (mod (1+ player) 2) ; 0 as o, 1 as x
:for c :across (subseq input 0 (min 9 (length input)))
:for pos := (1- (digit-char-p c))
:do (if (logbitp pos (logior board0 board1))
(RETURN (if (zerop player) "Foul : x won." "Foul : o won."))
(cond ((zerop player)
(setf (ldb (byte 1 pos) board0) 1)
(when (winp board0)
(RETURN "o won.")))
(t
(setf (ldb (byte 1 pos) board1) 1)
(when (winp board1)
(RETURN "x won.")))))
:finally (RETURN "Draw game."))))
;;--------------------------------------------------------------------
;; Tests
;;--------------------------------------------------------------------
(defun =>? (got want)
(assert (string= got want)))
(time
(progn
(=>? (main "79538246") "x won.")
(=>? (main "35497162193") "x won.")
(=>? (main "61978543") "x won.")
(=>? (main "254961323121") "x won.")
(=>? (main "6134278187") "x won.")
(=>? (main "4319581") "Foul : x won.")
(=>? (main "9625663381") "Foul : x won.")
(=>? (main "7975662") "Foul : x won.")
(=>? (main "2368799597") "Foul : x won.")
(=>? (main "18652368566") "Foul : x won.")
(=>? (main "965715") "o won.")
(=>? (main "38745796") "o won.")
(=>? (main "371929") "o won.")
(=>? (main "758698769") "o won.")
(=>? (main "42683953") "o won.")
(=>? (main "618843927") "Foul : o won.")
(=>? (main "36535224") "Foul : o won.")
(=>? (main "882973") "Foul : o won.")
(=>? (main "653675681") "Foul : o won.")
(=>? (main "9729934662") "Foul : o won.")
(=>? (main "972651483927") "Draw game.")
(=>? (main "5439126787") "Draw game.")
(=>? (main "142583697") "Draw game.")
(=>? (main "42198637563") "Draw game.")
(=>? (main "657391482") "Draw game.")
))
;;====================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment