repl.it
@kolja/

2048

Clojure

implementation of 2048

fork
loading
Files
  • main.clj
main.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
(defn insert-2 [board]
  (let [flat (into [] (flatten board))
        zero-indices (seq (keep-indexed
                       #(when (zero? %2) %1)
                       flat))]
    (cond
      (some #{2048} flat) (pr "you win")
      (empty? zero-indices) (pr "game over")
      :else (partition 4 (assoc flat (rand-nth zero-indices) 2)))))

(defn rotate [board]
  (map
    (fn [n]
        (map #(nth % n)
             board))
    [3 2 1 0]))

(defn remove-zeros [row]
 (remove zero? row))

(defn fill [row]
 (take 4 (concat row [0 0 0 0])))

(defn sum-up [acc x]
  (let [l (last acc)]
    (if (= x l)
      (conj (pop acc) (+ l x) 0)
      (conj acc x))))

(defn sum-pairs [v]
  (remove-zeros (reduce sum-up [] v)))

(defn render [v]
  (doseq [line (for [row v]
                 (apply str (map #(format "%5d " %) row)))]
    (println line "\n")) v)

(defn slide [board]
 (map (comp
        fill
        sum-pairs
        remove-zeros
        ) board))

(loop [board (insert-2 (partition 4 (repeat 16 0)))]
  (when (render board)
    (let [input ({"h" 4 "k" 3 "l" 2 "j" 1} (read-line))
          command (apply comp
                         (assoc (into [] (repeat 5 rotate))
                                input slide))] ;; (comp rotate rotate slide rotate rotate)
      (-> board command insert-2 recur))))

Clojure 1.8.0 Java HotSpot(TM) 64-Bit Server VM 1.8.0_91-b14
?