Advertisement
Guest User

Untitled

a guest
Mar 15th, 2022
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns clominoes.game)
  2.  
  3. (def tiles
  4.   (for [x (range 7)
  5.         y (range (inc x))]
  6.     [x y]))
  7.  
  8. (defn new-game [n-players]
  9.   (let
  10.       [pieces (partition n-players (shuffle tiles))]
  11.     (->> pieces
  12.          (map-indexed (fn [idx part] {:hand part :name (format "Player %d" (inc idx))}))
  13.          (map #(into #{} %))
  14.     )))
  15.  
  16. (defn- match-left [[u1 d1] [u2 d2]]
  17.   (cond (= d1 u2) [u1 d1]
  18.         (= u1 u2) [d1 u1]))
  19.  
  20. (defn- match-right [[u1 d1] [u2 d2]]
  21.   (cond (= d1 d2) [d1 u1]
  22.         (= u1 d2) [u1 d1]))
  23.  
  24. (defn- match [game position p1]
  25.   (let [
  26.         board (:board game)
  27.         match-function ({:left match-left :right match-right} position)
  28.         piece ({:left #(first board) :right #(last board)} position)]
  29.     (match-function p1 (piece))))
  30.  
  31.  
  32. (defn move [game [player-pos piece] position]
  33.   (let [
  34.         player (get-in game [:players player-pos])
  35.         hand (:hand player)
  36.         m-piece (match game position piece) ;; "matched piece"
  37.         new-hand (disj hand piece)
  38.         new-player (assoc player :hand new-hand)
  39.         new-board  (if (= :left position)
  40.                       (into [] (concat [m-piece] (:board game)))
  41.                       (conj (:board game) m-piece))
  42.         ]
  43.       (assoc game
  44.              :board new-board
  45.              :players (assoc (:players game) player-pos new-player))))
  46.              
  47.      
  48.  
  49.  
  50. (def game {
  51.            :players [
  52.                      {:hand #{[1 2] [3 2] [3 3] [4 3]} :name "P1"}
  53.                      {:hand #{[1 2] [3 2] [3 3] [4 3]} :name "P2"}
  54.                      {:hand #{[1 2] [3 2] [3 3] [4 3]} :name "P3"}
  55.                      {:hand #{[1 2] [3 2] [3 3] [4 3]} :name "P4"}
  56.                      ]
  57.            :board [[4 5] [5 6] [6 1]]
  58.            })
  59.  
  60. (clojure.pprint/pprint game)
  61.  
  62. (def new-game (move game [2 [1 2]] :right))
  63.  
  64. (clojure.pprint/pprint new-game)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement