Last active
December 30, 2015 11:49
-
-
Save tkych/7825241 to your computer and use it in GitHub Desktop.
Tick Tack Toe, ver.3 (Real Man Version), E_Mattsanさんによる「漢らしいC++」を勉強のためCommon Lispで書いてみました。
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
;;;; Last modified: 2013-12-06 23:19:04 tkych | |
;; E_Mattsanさんによる「漢らしいC++」を勉強のためCommon Lispで書いてみました。 | |
;;==================================================================== | |
;; Tick Tack Toe, ver.3 (Real Man Version) | |
;;==================================================================== | |
;; - [Tick-Tack-Toe 〜 横へな 2012.7.6](http://nabetani.sakura.ne.jp/hena/1/) | |
;; - [E_Mattsanさんによるビット演算の解答:漢らしいC++によるどう書く、再び](http://qiita.com/emattsan/items/5dfd820a4047376c3bf3) | |
;; - [その解説:ビット演算で解く Tick-Tack-Toe](http://d.hatena.ne.jp/E_Mattsan/20120709/1341841649) | |
;; | |
#| 実行コストの比較 | |
E_Mattsanさんによる「漢らしいC++」を勉強のためCommon Lispで書いてみました。 | |
結局、3種類の実装を書きましたが、私の環境では実行コストに劇的な違いを見ることはできませんでした。 | |
宣言を入れた場合や、より制限された環境、または大量の入力では違いが出てくるかもしれません。 | |
Env: SBCL-1.0.57.0.debian | |
Linux-3.2.0-4-amd64 | |
X86-64 Intel(R) Core(TM) i3-2330M CPU @ 2.20GHz | |
Ver.1: https://gist.github.com/tkych/7804206 | |
Evaluation took: | |
0.000 seconds of real time | |
0.000000 seconds of total run time (0.000000 user, 0.000000 system) | |
100.00% CPU | |
362,836 processor cycles | |
32,544 bytes consed | |
Ver.2: https://gist.github.com/tkych/7824725 | |
Evaluation took: | |
0.000 seconds of real time | |
0.000000 seconds of total run time (0.000000 user, 0.000000 system) | |
100.00% CPU | |
330,484 processor cycles | |
32,768 bytes consed | |
Ver.3: このファイルのプログラム | |
Evaluation took: | |
0.000 seconds of real time | |
0.000000 seconds of total run time (0.000000 user, 0.000000 system) | |
100.00% CPU | |
276,944 processor cycles | |
32,752 bytes consed | |
|# | |
;;-------------------------------------------------------------------- | |
;; Package | |
;;-------------------------------------------------------------------- | |
(in-package :cl-user) | |
(defpackage :tick-tack-toe-v3 (:use :cl)) | |
(in-package :tick-tack-toe-v3) | |
;;-------------------------------------------------------------------- | |
;; Main | |
;;-------------------------------------------------------------------- | |
;; 解説:http://d.hatena.ne.jp/E_Mattsan/20120709/1341841649 | |
(defparameter *board* | |
#(#xb6b6 #xeeee #x5e5e #xf5f5 #x2d2d #xdddd #x7373 #xebeb #x9b9b)) | |
(defun main (input) | |
(let ((board (copy-seq *board*)) | |
(message #("Foul : x won." "Foul : o won." "o won." "x won."))) | |
(dotimes (i (min 9 (length input))) | |
(let ((n (1- (digit-char-p (char input i)))) | |
(player (mod i 2))) | |
(if (logtest (logxor (svref board n) | |
(ash (svref board n) -8)) | |
#xff) | |
(RETURN-FROM main (svref message player)) | |
(progn | |
(setf (svref board n) | |
(logior (svref board n) | |
(ash #xff (* 8 player)))) | |
(unless (zerop (reduce #'logand board)) | |
(RETURN-FROM main (svref message (+ player 2)))))))) | |
"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