Skip to content

Commit

Permalink
Basic domino placement
Browse files Browse the repository at this point in the history
  • Loading branch information
timmc committed May 28, 2012
1 parent 463f4d5 commit 5335fb7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/crosscram/game.clj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ A gamestate value (which is provided to the bot) is a map of:
This will sometimes simply be called a game value.")


;; Implementation details

;; In 2 dimensions:
Expand All @@ -40,3 +39,31 @@ This will sometimes simply be called a game value.")
;; TODO(timmc:2012-05-23) Should we canonicalize the order of the squares in
;; the domino on receipt from a bot?

(defn board
"Given a dimensions vector of [rows, columns], generate an empty board."
[[rows columns]]
(vec (repeat rows (vec (repeat columns nil))))) ;; TODO dimension-agnostic

(defn domino-squares
"Return a sequence of the coordinates occupied by a domino."
[domino]
(seq domino))

(defn ^:internal set-square
"Set the value of a square in a board."
[board square val]
(assoc-in board square val))

(defn place-domino
"Place a domino on the board, assumed to be a valid move. The returned
board will have the specified move ordinal in the squares covered by the
domino."
[board domino move-ord]
(reduce #(set-square % %2 move-ord) board (domino-squares domino)))

(defn lookup-square
"Discover if a board position is empty. Given a location [r c] on a board,
return the ordinal of the move that filled it, or nil if empty."
[board square]
(get-in board square nil))

19 changes: 19 additions & 0 deletions test/crosscram/test/game.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(ns crosscram.test.game
(:use clojure.test
crosscram.game))

(deftest board-manip
(let [empty (board [2 3])
tile [[0 2] [1 2]]
move0 (place-domino empty tile 0)]
;; impl
(is (= empty [[nil nil nil] [nil nil nil]]))
(is (= move0 [[nil nil 0] [nil nil 0]]))
;; impl - non-vertical
(is (= (place-domino empty [[0 0] [0 1]] 5)
[[5 5 nil] [nil nil nil]]))
;; api
(is (= (lookup-square empty [0 2]) nil))
(is (= (lookup-square move0 [0 2]) 0))
(is (= (lookup-square move0 [1 2]) 0))
(is (= (lookup-square move0 [1 1]) nil))))

0 comments on commit 5335fb7

Please sign in to comment.