Advertisement
Guest User

Untitled

a guest
Mar 13th, 2022
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns clominoes.game)
  2.  
  3. (def tiles
  4.   (apply concat
  5.          (for [x (range 7)]
  6.            (for [y (range (+ 1 x))]
  7.              [x y]))))
  8.  
  9. (defn- player [number n-players pieces]
  10.   {:hand (let
  11.              [n-pieces (quot 28 n-players)
  12.               i (* number n-pieces)
  13.               j (+ i n-pieces)]
  14.            (subvec pieces i j))
  15.    :name (format "Player %d" number)})
  16.  
  17. (defn new-game [n-players]
  18.   (let [pieces (shuffle tiles)]
  19.     {:players (vec (for [i (range 0 n-players)]
  20.                      (player i n-players pieces)))
  21.      :board []
  22.      "turn" 0}))
  23.  
  24.  
  25. (defn vec-remove
  26.   "remove elem in coll"
  27.   [coll pos]
  28.   (into []
  29.         (for [elem (map-indexed vector coll)
  30.               :let [i (elem 0)]
  31.               :when (not= i pos)]
  32.           (elem 1))))
  33.  
  34.  
  35. (defn- get-piece [game [player piece]]
  36.   (let [
  37.         player (get (:players game) player)
  38.         hand (if player (:hand player))
  39.         p (if hand (get hand piece))]    
  40.     p))
  41.  
  42. (defn- match-left [[u1 d1] [u2 d2]]
  43.   (cond (= d1 u2) [u1 d1]
  44.         (= u1 u2) [d1 u1]))
  45.  
  46. (defn- match-right [[u1 d1] [u2 d2]]
  47.   (cond (= d1 d2) [d1 u1]
  48.         (= u1 d2) [u1 d1]))
  49.  
  50. (defn- match [game position p1]
  51.   (let [
  52.         board (:board game)
  53.         match-function ({:left match-left :right match-right} position)
  54.         piece ({:left #(first board) :right #(last board)} position)]
  55.     (match-function p1 (piece))))
  56.  
  57.  
  58. (defn move [game [player-pos piece-pos] position]
  59.   (let [
  60.         player (get (:players game) player-pos)
  61.         hand (if player (:hand player))
  62.         piece (if hand (get hand piece-pos))
  63.         m-piece (if piece (match game position piece)) ;; "matched piece"
  64.         new-hand (if m-piece (vec-remove hand piece-pos))
  65.         new-player (if new-hand (assoc player :hand new-hand))
  66.         new-board (if m-piece
  67.                     (if (= :left position)
  68.                       (into [] (concat [m-piece] (:board game)))
  69.                       (conj (:board game) m-piece)))
  70.         ]
  71.     (if m-piece
  72.       (assoc game
  73.              :board new-board
  74.              :players (assoc (:players game) player-pos new-player)
  75.              ))))
  76.      
  77.  
  78.  
  79. (def game {
  80.            :players [
  81.                      {:hand [[1 2] [3 2] [3 3] [4 3]] :name "P1"}
  82.                      {:hand [[1 2] [3 2] [3 3] [4 3]] :name "P2"}
  83.                      {:hand [[1 2] [3 2] [3 3] [4 3]] :name "P3"}
  84.                      {:hand [[1 2] [3 2] [3 3] [4 3]] :name "P4"}
  85.                      ]
  86.            :board [[4 5] [5 6] [6 1]]
  87.            })
  88.  
  89. (def new-game (move game [2 0] :right))
  90.  
  91. (clojure.pprint/pprint new-game)
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement