Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns clominoes.game)
- (def tiles
- (apply concat
- (for [x (range 7)]
- (for [y (range (+ 1 x))]
- [x y]))))
- (defn- player [number n-players pieces]
- {:hand (let
- [n-pieces (quot 28 n-players)
- i (* number n-pieces)
- j (+ i n-pieces)]
- (subvec pieces i j))
- :name (format "Player %d" number)})
- (defn new-game [n-players]
- (let [pieces (shuffle tiles)]
- {:players (vec (for [i (range 0 n-players)]
- (player i n-players pieces)))
- :board []
- "turn" 0}))
- (defn vec-remove
- "remove elem in coll"
- [coll pos]
- (into []
- (for [elem (map-indexed vector coll)
- :let [i (elem 0)]
- :when (not= i pos)]
- (elem 1))))
- (defn- get-piece [game [player piece]]
- (let [
- player (get (:players game) player)
- hand (if player (:hand player))
- p (if hand (get hand piece))]
- p))
- (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-pos] position]
- (let [
- player (get (:players game) player-pos)
- hand (if player (:hand player))
- piece (if hand (get hand piece-pos))
- m-piece (if piece (match game position piece)) ;; "matched piece"
- new-hand (if m-piece (vec-remove hand piece-pos))
- new-player (if new-hand (assoc player :hand new-hand))
- new-board (if m-piece
- (if (= :left position)
- (into [] (concat [m-piece] (:board game)))
- (conj (:board game) m-piece)))
- ]
- (if 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]]
- })
- (def new-game (move game [2 0] :right))
- (clojure.pprint/pprint new-game)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement