Skip to content

Commit

Permalink
canonicalize history representation of moves
Browse files Browse the repository at this point in the history
  • Loading branch information
S11001001 committed Jul 5, 2012
1 parent d61ed4e commit ad50c82
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/crosscram/game.clj
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion test/crosscram/test/game.clj
Expand Up @@ -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])
Expand Down

0 comments on commit ad50c82

Please sign in to comment.