-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathminizorg.lisp
executable file
·331 lines (290 loc) · 8.19 KB
/
minizorg.lisp
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/local/bin/lispe
; Actions on data structures
(defmacro belong (x l) (in l (keystr x)))
(defmacro remove(o k v) (pop (key o k) v))
(data (Move _) (Break _ _) (Open _ _) (Kill _ _) (Pick _ _) (Take _) (Drop _))
; pick up is the same as take with one more parameter
(defpat action ( (Pick 'up x))
(action (Take x))
)
; take an object on the ground
(defpat action ( (Take x) )
(if
(check_object position x)
(block
(push belongings x)
(println "Ok we have picked up:" x)
)
(println "Cannot pick up the:" x)
)
)
; drop it at current position
(defpat action ( (Drop x))
(if
(check_belongings x)
(block
(key objects (keystr position) x)
(pop belongings (find belongings x))
(println "Ok! We have dropped" x " on the ground")
(display_position position)
)
)
)
; break a window with an object: it has to be a stone
(defpat action( (Break w x))
(if (check_object position w)
(if (and
(check_belongings x)
(eq x 'stone))
(block
(update_direction (keystr position) "0:2")
(key castlemap "1:2" "You are standing in front of a broken window")
(println "Window is broken")
)
(println "Cannot break a window with that" x)
)
(println "Get a sight check. There is no window over here")
)
)
; open door with a key
; It triggers new potential move to other positions
(defpat action ((Open 'door x))
(if (= position '(1 1))
(if (and (check_belongings x) (eq x 'key))
(block
(update_direction (keystr position) "0:1")
(update_direction "0:1" "0:0")
(key castlemap "1:1" "You are standing in front of an open gate")
(println "The door opens")
)
(println "Cannot open a door with that" x)
)
(println "A door here??? Really!!!")
)
)
; kill the ogre with a sword or you'll get into trouble
(defpat action ((Kill 'ogre x))
(if (= position '(1 0))
(if (and (check_belongings x) (eq x 'sword))
(block
(setg theend true)
(println "You kill the ogre. He was guarding an wonderfull treasure. You are now rich beyond measure. Congratulation!!!")
)
(block
(push dangers "1:0")
(println "This is not a very efficient weapon. The ogre attacks you...")
)
)
(println "There is no ogre here")
)
)
; Moving from one position to another
(defpat action ( (Move 'north))
(setq p (checkposition (- (car position) 1) (cadr position)))
(if (= p position)
(println "Cannot go in this direction")
(block
(setg position p)
(display_position position)
)
)
)
(defpat action ( (Move 'south))
(setg p (checkposition (+ (car position) 1) (cadr position)))
(if (= p position)
(println "Cannot go in this direction")
(block
(setg position p)
(display_position position)
)
)
)
(defpat action ( (Move 'west))
(setg p (checkposition (car position) (- (cadr position) 1)))
(if (= p position)
(println "Cannot go in this direction")
(block
(setg position p)
(display_position position)
)
)
)
(defpat action ( (Move 'east))
(setg p (checkposition (car position) (+ (cadr position) 1)))
(if (= p position)
(println "Cannot go in this direction")
(block
(setg position p)
(display_position position)
)
)
)
; Default action
(defpat action(_) (random_choice 1 msgs 10))
; Data for the game and basic instructions
; build the key string
(defun keystr(p)
(join p ":")
)
; check if a path is within the description in 'moving'
(defun check_valid_path(p)
(if (belong p (key moving (keystr position)))
p
position
)
)
; update valid directions in both ways
(defun update_direction (current_position direction)
(key moving current_position (cons direction (key moving current_position)))
(cond
((key moving direction)
(key moving direction (cons current_position (key moving direction)))
)
(true
(key moving direction (cons current_position ()))
)
)
)
; check if we are within the confines of the game: 3x3 square map.
(defun checkposition(x y)
(check_valid_path
(cond
((< x 0) (list 0 y))
((< y 0) (list x 0))
((> x 2) (list 2 y))
((> y 2) (list x 2))
(true (list x y))
)
)
)
; check if the object is available on the ground and pick it up
; we also remove it from objects
; the last 'true' is actually a hack. pop returns the dictionary as output,
; if the dictionary is empty then it will be evaluated as nil in a 'if'
(defun check_object(p x)
(setq k (keystr p))
(check
(in (key objects k) x)
(remove objects k x)
true
)
)
; check if we own the object x
(defun check_belongings(x)
(if (nullp (find belongings x))
(block
(println "You don't own a" x)
nil
)
true
)
)
; display a description of where we are at current position
(defun display_position(p)
(setq k (keystr p))
(println k (select (key castlemap k) "Nothing to see"))
(println (if (key objects k) (+ "There is a " (key objects k)) ""))
(println "You own: " belongings)
)
; check if the position is a dangerous one
(defun check_danger (position)
(belong position dangers)
)
; some synonyms
(setq synonyms
{"rock":"stone"
"pebble":"stone"
"Head":"Move"
"Walk":"Move"
"Go":"Move"
"Attack":"Kill"
"Slay":"Kill"
"Slash": "Kill"
"Smash":"Break"
"Shatter":"Break"
"Crash":"Break"
"glass":"window"
"Grab":"Take"
"Get":"Take"
"gate":"door"
"left":"west"
"right":"east"
"forward":"north"
"backward":"south"
"back":"south"
}
)
(setq stopwords {"to":true "a":true "the":true "with":true "your":true "his":true "her":true})
; these are the valid move from one position to another
(setq moving (key))
(update_direction "1:1" "1:0")
(update_direction "1:1" "1:2")
(update_direction "1:1" "2:1")
(update_direction "1:2" "2:2")
(update_direction "2:1" "2:0")
(update_direction "2:1" "2:2")
(update_direction "2:0" "1:0")
; the description for each square, which contains something
(setq castlemap {
"1:1":"You are standing in front of a gate."
"1:2":"You are standing in front of a large window"
"1:0":"You are standing in front of a Ogre"
"2:1":"You wake up an angry venominous snake. It bites you. The pain is terrible..."
"0:0":"Your are in a large dark room"
"0:2":"You are on in small room"
"2:0":"You are in the middle of a forest"
"2:2":"You are in a large plain. A river is flowing east"
}
)
(setq objects {
"1:1": '(stone door)
"1:2": '(window)
"0:2": '(key)
"0:0": '(sword)
}
)
; initialisation
(setq belongings '())
(setq position '(1 1))
(setq dangers '("2:1"))
(setq theend nil)
(setq commands '(commencement))
; We display our initial psoition
(display_position position)
(setq msgs '(
"Not sure what you want to do!!!"
"Do not know what to do here"
"Sorry, I did not understand..."
"You want to do what?"
"Sorry, I took a walk. What did you say?"
"Please... Don't say it again"
"Nice try... But no"
)
)
(while (neq (car commands) 'End)
(print "Your order sire? ")
(setq dialog (input))
(check (eq dialog "end")
(println "Ok... Bye...")
(break)
)
(setq dialog (lower dialog))
(setq dialog (+ (upper (at dialog 0)) (extract dialog 1 (size dialog))))
(setq commands (map (\ (x) (select (key synonyms x) x)) (split dialog " ")))
; we transform each of our strings into atoms, for match sake
;we remove useless words: the, a etc..
(setq commands (filter (\ (x) (not (key stopwords x))) (map 'atom commands)))
; our commands is now a list of atoms that should match a data structure
(maybe
(println (action commands))
(println (random_choice 1 msgs 10))
)
(check (check_danger position)
(println "you are dead!!!")
(setq theend true)
)
(if theend
(setq commands '(End))
)
)
(print "The end")