-
Notifications
You must be signed in to change notification settings - Fork 0
/
wumpus.nw
320 lines (222 loc) · 6.58 KB
/
wumpus.nw
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
% -*- mode: noweb; ess-noweb-default-code-mode: lisp-mode; -*-
\documentclass{tufte-handout}
\input{preamble.tex}
\usepackage[xindy,nopostdot]{glossaries}
\makeglossaries
\input{glossary}
\hypersetup{
pdffitwindow=true,
pdfstartview={FitH},
pdftitle={Grand Theft Wumpus}
pdfauthor={Eric Bailey <eric@ericb.me>},
pdfsubject={Land of Lisp: Chapter 8},
pdfkeywords={Lisp, game, literate programming, noweb},
colorlinks=true,
linkcolor=ErlangRed,
urlcolor=ErlangRed
}
\title{%
Grand Theft Wumpus
\thanks{\cite{barski2010land-ch8}}
}
\date{%
December 14, 2017
\thanks{Last updated \today}
}
\begin{document}
\maketitle
@
% \begin{abstract}
% \end{abstract}
\begin{marginfigure}
\srclink{src/wumpus.lisp}:
<<*>>=
(in-package :cl-user)
(defpackage lol.wumpus
(:use :cl :prove))
(in-package :lol.wumpus)
@ %def lol.wumpus
\end{marginfigure}
\tableofcontents
\newpage
\section{Defining the Edges of Congestion City}
\todoo{Load the code from the previous chapter}
<<*>>=
;; TODO: (load "graph-util")
@
<<*>>=
(defparameter *congestion-city-nodes* nil)
(defparameter *congestion-city-edges* nil)
(defparameter *visited-nodes* nil)
(defparameter *node-num* 30 )
(defparameter *edge-num* 45)
(defparameter *worm-num* 3)
(defparameter *cop-odds* 15)
@
\section{Generating Random Edges}
Generate a random number between $1$ and [[*node-num*]].
<<*>>=
(defun random-node ()
(1+ (random *node-num*)))
@
\todoo{Describe edge-pair}
<<*>>=
(defun edge-pair (a b)
(unless (eql a b)
(list (cons a b) (cons b a))))
@
\todoo{Describe make-edge-list}
<<*>>=
(defun make-edge-list ()
(apply #'append (loop repeat *edge-num*
collect (edge-pair (random-node) (random-node)))))
@
\section{Preventing Islands}
\todoo{Describe direct-edges}
<<*>>=
(defun direct-edges (node edge-list)
(remove-if-not (lambda (x)
(eql (car x) node))
edge-list))
@
\todoo{Describe get-connected}
<<*>>=
(defun get-connected (node edge-list)
(let ((visited nil))
(labels ((traverse (node)
(unless (member node visited)
(push node visited)
(mapc (lambda (edge)
(traverse (cdr edge)))
(direct-edges node edge-list)))))
(traverse node))
visited))
@
\todoo{Describe find-islands}
<<*>>=
(defun find-islands (nodes edge-list)
(let ((islands nil))
(labels ((find-island (nodes)
(let* ((connected (get-connected (car nodes) edge-list))
(unconnected (set-difference nodes connected)))
(push connected islands)
(when unconnected
(find-island unconnected)))))
(find-island nodes))
islands))
@
\todoo{Describe connect-with-bridges}
<<*>>=
(defun connect-with-bridges (islands)
(when (cdr islands)
(append (edge-pair (caar islands) (caadr islands))
(connect-with-bridges (cdr islands)))))
@
\todoo{Describe connect-all-islands}
<<*>>=
(defun connect-all-islands (nodes edge-list)
(append (connect-with-bridges (find-islands nodes edge-list)) edge-list))
@
\section{Building the Final Edges for Congestion City}
\todoo{Describe make-city-edges}
<<*>>=
(defun make-city-edges ()
(let* ((nodes (loop for i from 1 to *node-num*
collect i))
(edge-list (connect-all-islands nodes (make-edge-list)))
(cops (remove-if-not (lambda (x)
(zerop (random *cop-odds*)))
edge-list)))
(add-cops (edges-to-alist edge-list) cops)))
@
\todoo{Describe edges-to-alist}
<<*>>=
(defun edges-to-alist (edge-list)
(mapcar (lambda (node1)
(cons node1
(mapcar (lambda (edge)
(list (cdr edge)))
(remove-duplicates (direct-edges node1 edge-list)
:test #'equal))))
(remove-duplicates (mapcar #'car edge-list))))
@
\todoo{Describe add-cops}
<<*>>=
(defun add-cops (edge-alist edges-with-cops)
(mapcar (lambda (x)
(let ((node1 (car x))
(node1-edges (cdr x)))
(cons node1
(mapcar (lambda (edge)
(let ((node2 (car edge)))
(if (intersection (edge-pair node1 node2)
edges-with-cops
:test #'equal)
(list node2 'cops)
edge)))
node1-edges))))
edge-alist))
@
\section{Building the Nodes for Congestion City}
\todoo{Describe neighbors}
<<*>>=
(defun neighbors (node edge-alist)
(mapcar #'car (cdr (assoc node edge-alist))))
@
\todoo{Describe within-one}
<<*>>=
(defun within-one (a b edge-alist)
(member b (neighbors a edge-alist)))
@
\todoo{Describe within-two}
<<*>>=
(defun within-two (a b edge-alist)
(or (within-one a b edge-alist)
(some (lambda (x)
(within-one x b edge-alist))
(neighbors a edge-alist))))
@
\todoo{Describe make-city-nodes}
<<*>>=
(defun make-city-nodes (edge-alist)
(let ((wumpus (random-node))
(glow-worms (loop for i below *worm-num*
collect (random-node))))
(loop for n from 1 to *node-num*
collect (append (list n)
(cond ((eql n wumpus) '(wumpus))
((within-two n wumpus edge-alist) '(blood!)))
(cond ((member n glow-worms)
'(glow-worm))
((some (lambda (worm)
(within-one n worm edge-alist))
glow-worms)
'(lights!)))
(when (some #'cdr (cdr (assoc n edge-alist)))
'(sirens!))))))
@
\newpage
\section{Full Listing}
\inputminted[linenos,firstline=1,lastline=48,fontsize=\small]{lisp}{wumpus.lisp}
\newpage
\inputminted[linenos,firstline=51,lastline=90,fontsize=\small]{lisp}{wumpus.lisp}
\newpage
\inputminted[linenos,firstline=93,fontsize=\small]{lisp}{wumpus.lisp}
\section{Tests}
\todor{Implement tests}
<<test/wumpus.lisp>>=
(in-package :lol.wumpus)
@
% \section{Index}
% \nowebindex
% \newpage
% \section{Chunks}
% \nowebchunks
\newpage
\printglossaries
\setlength{\linewidth}{1.4\textwidth}
\bibliography{lol}
\bibliographystyle{plainnat}
\newpage
\listoftodos[To-Do]
\end{document}