-
Notifications
You must be signed in to change notification settings - Fork 7
/
core.clj
164 lines (139 loc) · 4.16 KB
/
core.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
(ns snakejure.core
(:use (clojure.contrib seq-utils
import-static))
(:import (java.awt Color)))
(def width 40)
(def height 40)
(def indent 4)
(def dirs {:left [0 -1]
:right [0 1]
:up [-1 0]
:down [1 0]} )
(def opposite {:left :right
:right :left
:up :down
:down :up})
(defmacro let-map
"There are no neccesaty in this macro,
because I found out how to do it using let.
But it's my first macro and I'll keep it yet."
[[mp & ks] & body]
`(let ~(vec (apply concat
(for [key ks]
[key `(~(keyword key) ~mp)])))
~@body))
(defn level-to-str
"Converts level to handy string for logging purpose.
If you specify one or more parts of level (e.g. :walls, :snake)
they will be excluded from string"
[level & excl]
(str "############################\n"
(->> level
(keys)
(remove #(includes? excl %))
(map #(str % " " (print-str (level %)) \newline))
(apply str))
"############################"))
(defn create-wall
"Creates new wall in given point."
[p]
{:location p
:type :wall
:color (Color/BLACK)})
(defn create-noiser
"Creates new noiser in given point."
[p]
{:location p
:type :noiser
:color-noise (Color/BLUE)
:color-silence (Color/MAGENTA)})
(defn create-snake
"Creates new snake with given body. And direction to right."
[body]
{:body body
:type :snake
:dir :right
:color (Color/CYAN)})
(defn create-apple
"Creates new apple in given point."
[p]
{:location p
:type :apple
:color (Color/RED)})
(defn overlaps-body?
"Check, if snake's body overlaps some given point."
[{body :body} point]
(includes? body point))
(defn add-points
"Add two or more points by coordinates."
[& args]
(vec (apply map + args)))
(defn overlaps-walls?
"Check, if some given point overlaps walls."
[walls point]
(not (every? false? (map #(= point (:location %)) walls))))
(defn move-snake
"Moves snake in current direction. If grow is given,
snake will grow, length will increase by 1 unit."
[{:keys [body dir] :as snake} & grow]
(assoc snake :body (cons (add-points (first body) (dirs dir))
(if grow
body
(butlast body)))))
(defn eats?
"Checks if snake can ear appple."
[{[head] :body} {apple :location}]
(= head apple))
(defn turn-snake
"Turns snake in given direction."
[{:keys [dir] :as snake} new-dir]
(if (= [0 0] (add-points (dirs dir) (dirs new-dir)))
snake
(assoc snake :dir new-dir)))
(defn lose?
"Checks if user lose. It's possible if snake bite itself or hit wall."
[{[head & body] :body} walls]
(or (includes? body head)
(overlaps-walls? walls head)))
(defn win?
"Check if use win. User wins, when snake lay on every noser simultaneously."
[{body :body} noisers]
(every? true? (map #(includes? body (:location %)) noisers)))
(defn inside?
"Checks if point is inside allowed bounds,
it depends on width, height and indent vars."
[[y x]]
(and (>= x indent)
(< x (- width indent))
(>= y indent)
(< y (- height indent))))
(defn normalize-level
"Normalizes :d in level, so snake head will be inside screen."
[{[head & rst] :body dir :dir} d]
(if (inside? (add-points head d))
d
(add-points d ((dir opposite) dirs))))
(defn add-d
"Adds d to new level in such way, that snake will in the center."
[{ {[[y x]] :body} :snake :as level}]
(assoc level :d [ (- (quot height 2) y)
(- (quot width 2) x)] ))
(defn update-snake-direction
"Takes level ref, and set new snake with updated direction in it."
[level dir]
(dosync (alter level
assoc :snake (turn-snake (:snake @level) dir))))
(defn update-snake-position
"Takes level ref and moves snake in it,
altering ref."
[level]
(let-map [@level snake apple walls apple-generator d]
(dosync
(if (eats? snake apple)
(let [new-snake (move-snake snake :grow)]
(alter level assoc :snake new-snake
:apple (apple-generator @level)
:d (normalize-level new-snake d)))
(let [new-snake (move-snake snake)]
(alter level assoc :snake new-snake
:d (normalize-level new-snake d)))))))