From ad50c82465510c87a959970f5f4865808c03cf72 Mon Sep 17 00:00:00 2001 From: Stephen Compall Date: Thu, 5 Jul 2012 19:52:54 -0400 Subject: [PATCH] canonicalize history representation of moves --- src/crosscram/game.clj | 8 +++++++- test/crosscram/test/game.clj | 5 ++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/crosscram/game.clj b/src/crosscram/game.clj index 30e4973..d32b5a4 100644 --- a/src/crosscram/game.clj +++ b/src/crosscram/game.clj @@ -69,6 +69,12 @@ This will sometimes simply be called a game value.") (xor2 (= (Math/abs (long (- r0 r1))) 1) (= (Math/abs (long (- c0 c1))) 1)))))) +(defn canonical-domino + "Answer a domino such that for all representations D1,D2 of a +domino, (= (canonical-domino d1) (canonical-domino d2))" + [domino] + (into [] (sort domino))) + (defn domino-squares "Return a sequence of the coordinates occupied by a valid domino." [domino] @@ -190,7 +196,7 @@ for the indicated player. The player ID may be 0 or 1." (let [ord (count (:history game)) board (place-domino (:board game) move ord)] (-> game - (assoc-in [:history ord] move) + (assoc-in [:history ord] (canonical-domino move)) (assoc :board board)))) (defn rotate-game diff --git a/test/crosscram/test/game.clj b/test/crosscram/test/game.clj index b7e8d7b..72e6167 100644 --- a/test/crosscram/test/game.clj +++ b/test/crosscram/test/game.clj @@ -26,7 +26,10 @@ ;; orientation (is (horizontal? [[5 9] [5 10]])) (is (horizontal? [[5 10] [5 9]])) - (is (not (horizontal? [[9 5] [10 5]])))) + (is (not (horizontal? [[9 5] [10 5]]))) + ;; canonical order + (is (= (canonical-domino [[0 1] [2 3]]) + (canonical-domino [[2 3] [0 1]])))) (deftest boards-and-moves (let [empty (make-board [2 3])