Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns clominoes.game)
- (def tiles
- (for [x (range 7)
- y (range (inc x))]
- [x y]))
- (defn new-game [n-players]
- (let
- [pieces (partition n-players (shuffle tiles))]
- (->> pieces
- (map-indexed (fn [idx part] {:hand part :name (format "Player %d" (inc idx))}))
- (map #(into #{} %))
- )))
- (defn- match-left [[u1 d1] [u2 d2]]
- (cond (= d1 u2) [u1 d1]
- (= u1 u2) [d1 u1]))
- (defn- match-right [[u1 d1] [u2 d2]]
- (cond (= d1 d2) [d1 u1]
- (= u1 d2) [u1 d1]))
- (defn- match [game position p1]
- (let [
- board (:board game)
- match-function ({:left match-left :right match-right} position)
- piece ({:left #(first board) :right #(last board)} position)]
- (match-function p1 (piece))))
- (defn move [game [player-pos piece] position]
- (let [
- player (get-in game [:players player-pos])
- hand (:hand player)
- m-piece (match game position piece) ;; "matched piece"
- new-hand (disj hand piece)
- new-player (assoc player :hand new-hand)
- new-board (if (= :left position)
- (into [] (concat [m-piece] (:board game)))
- (conj (:board game) m-piece))
- ]
- (assoc game
- :board new-board
- :players (assoc (:players game) player-pos new-player))))
- (def game {
- :players [
- {:hand #{[1 2] [3 2] [3 3] [4 3]} :name "P1"}
- {:hand #{[1 2] [3 2] [3 3] [4 3]} :name "P2"}
- {:hand #{[1 2] [3 2] [3 3] [4 3]} :name "P3"}
- {:hand #{[1 2] [3 2] [3 3] [4 3]} :name "P4"}
- ]
- :board [[4 5] [5 6] [6 1]]
- })
- (clojure.pprint/pprint game)
- (def new-game (move game [2 [1 2]] :right))
- (clojure.pprint/pprint new-game)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement