-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathants.py.html
629 lines (475 loc) · 69.4 KB
/
ants.py.html
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
<html>
<head>
<title>ants.py</title>
<link href="css/assignments.css" rel="stylesheet" type="text/css">
</head>
<body>
<h3>ants.py (<a href="ants.py">plain text</a>)</h3>
<hr>
<pre>
<span style="color: darkred">"""
The ants module implements game logic for Ants Vs. SomeBees.
Name:
Login:
TA:
Section:
"""
</span><span style="color: blue; font-weight: bold">import </span>random
<span style="color: blue; font-weight: bold">import </span>sys
<span style="color: blue; font-weight: bold">from </span>ucb <span style="color: blue; font-weight: bold">import </span>main<span style="font-weight: bold">, </span>interact<span style="font-weight: bold">, </span>trace
<span style="color: blue; font-weight: bold">from </span>collections <span style="color: blue; font-weight: bold">import </span>OrderedDict
<span style="color: green; font-style: italic">################
# Core Classes #
################
</span><span style="color: blue; font-weight: bold">class </span>Place<span style="font-weight: bold">(</span>object<span style="font-weight: bold">):
</span><span style="color: darkred">"""A Place holds insects and has an exit to another Place."""
</span><span style="color: blue; font-weight: bold">def </span>__init__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>name<span style="font-weight: bold">, </span>exit<span style="font-weight: bold">=</span><span style="color: blue">None</span><span style="font-weight: bold">):
</span><span style="color: darkred">"""Create a Place with the given exit.
name -- A string; the name of this Place.
exit -- The Place reached by exiting this Place (may be None).
"""
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>name <span style="font-weight: bold">= </span>name
<span style="color: blue">self</span><span style="font-weight: bold">.</span>exit <span style="font-weight: bold">= </span>exit
<span style="color: blue">self</span><span style="font-weight: bold">.</span>bees <span style="font-weight: bold">= [] </span><span style="color: green; font-style: italic"># A list of Bees
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>ant <span style="font-weight: bold">= </span><span style="color: blue">None </span><span style="color: green; font-style: italic"># An Ant
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>entrance <span style="font-weight: bold">= </span><span style="color: blue">None </span><span style="color: green; font-style: italic"># A Place
# Phase 1: Add an entrance to the exit
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: blue; font-weight: bold">def </span>add_insect<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>insect<span style="font-weight: bold">):
</span><span style="color: darkred">"""Add an Insect to this Place.
There can be at most one Ant in a Place, unless exactly one of them is
a BodyguardAnt (Phase 2), in which case there can be two. If add_insect
tries to add more Ants than is allowed, an assertion error is raised.
There can be any number of Bees in a Place.
"""
</span><span style="color: blue; font-weight: bold">if </span>insect<span style="font-weight: bold">.</span>is_ant<span style="font-weight: bold">():
</span><span style="color: green; font-style: italic"># Phase 2: Special handling for BodyguardAnt
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: blue; font-weight: bold">assert </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>ant <span style="color: blue; font-weight: bold">is </span><span style="color: blue">None</span><span style="font-weight: bold">, </span><span style="color: red">'Two ants in {0}'</span><span style="font-weight: bold">.</span>format<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">)
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>ant <span style="font-weight: bold">= </span>insect
<span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>bees<span style="font-weight: bold">.</span>append<span style="font-weight: bold">(</span>insect<span style="font-weight: bold">)
</span>insect<span style="font-weight: bold">.</span>place <span style="font-weight: bold">= </span><span style="color: blue">self
</span><span style="color: blue; font-weight: bold">def </span>remove_insect<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>insect<span style="font-weight: bold">):
</span><span style="color: darkred">"""Remove an Insect from this Place."""
</span><span style="color: blue; font-weight: bold">if not </span>insect<span style="font-weight: bold">.</span>is_ant<span style="font-weight: bold">():
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>bees<span style="font-weight: bold">.</span>remove<span style="font-weight: bold">(</span>insect<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">assert </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>ant <span style="font-weight: bold">== </span>insect<span style="font-weight: bold">, </span><span style="color: red">'{0} is not in {1}'</span><span style="font-weight: bold">.</span>format<span style="font-weight: bold">(</span>insect<span style="font-weight: bold">, </span><span style="color: blue">self</span><span style="font-weight: bold">)
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>ant <span style="font-weight: bold">= </span><span style="color: blue">None
</span>insect<span style="font-weight: bold">.</span>place <span style="font-weight: bold">= </span><span style="color: blue">None
</span><span style="color: blue; font-weight: bold">def </span>__str__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
</span><span style="color: blue; font-weight: bold">return </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>name
<span style="color: blue; font-weight: bold">class </span>Insect<span style="font-weight: bold">(</span>object<span style="font-weight: bold">):
</span><span style="color: darkred">"""An Insect, the base class of Ant and Bee, has armor and a Place."""
</span><span style="color: blue; font-weight: bold">def </span>__init__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>armor<span style="font-weight: bold">, </span>place<span style="font-weight: bold">=</span><span style="color: blue">None</span><span style="font-weight: bold">):
</span><span style="color: darkred">"""Create an Insect with an armor amount and a starting Place."""
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>armor <span style="font-weight: bold">= </span>armor
<span style="color: blue">self</span><span style="font-weight: bold">.</span>place <span style="font-weight: bold">= </span>place <span style="color: green; font-style: italic"># set by Place.add_insect and Place.remove_insect
</span><span style="color: blue; font-weight: bold">def </span>reduce_armor<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>amount<span style="font-weight: bold">):
</span><span style="color: darkred">"""Reduce armor by amount, and remove the insect from its place if it
has no armor remaining.
>>> test_insect = Insect(5)
>>> test_insect.reduce_armor(2)
>>> test_insect.armor
3
"""
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>armor <span style="font-weight: bold">-= </span>amount
<span style="color: blue; font-weight: bold">if </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>armor <span style="font-weight: bold"><= </span><span style="color: red">0</span><span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">print</span><span style="font-weight: bold">(</span><span style="color: red">'{0} ran out of armor and expired'</span><span style="font-weight: bold">.</span>format<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">))
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>place<span style="font-weight: bold">.</span>remove_insect<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">def </span>action<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>colony<span style="font-weight: bold">):
</span><span style="color: darkred">"""Perform the default action that this Insect takes each turn.
colony -- The AntColony, used to access game state information.
"""
</span><span style="color: blue; font-weight: bold">def </span>is_ant<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
</span><span style="color: darkred">"""Return whether this Insect is an Ant."""
</span><span style="color: blue; font-weight: bold">return False
def </span>__repr__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
</span>cname <span style="font-weight: bold">= </span>type<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">).</span>__name__
<span style="color: blue; font-weight: bold">return </span><span style="color: red">'{0}({1}, {2})'</span><span style="font-weight: bold">.</span>format<span style="font-weight: bold">(</span>cname<span style="font-weight: bold">, </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>armor<span style="font-weight: bold">, </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>place<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">class </span>Bee<span style="font-weight: bold">(</span>Insect<span style="font-weight: bold">):
</span><span style="color: darkred">"""A Bee moves from place to place, following exits and stinging ants."""
</span>name <span style="font-weight: bold">= </span><span style="color: red">'Bee'
</span><span style="color: blue; font-weight: bold">def </span>sting<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>ant<span style="font-weight: bold">):
</span><span style="color: darkred">"""Attack an Ant, reducing the Ant's armor by 1."""
</span>ant<span style="font-weight: bold">.</span>reduce_armor<span style="font-weight: bold">(</span><span style="color: red">1</span><span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">def </span>move_to<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>place<span style="font-weight: bold">):
</span><span style="color: darkred">"""Move from the Bee's current Place to a new Place."""
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>place<span style="font-weight: bold">.</span>remove_insect<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">)
</span>place<span style="font-weight: bold">.</span>add_insect<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">def </span>blocked<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
</span><span style="color: darkred">"""Return True if this Bee cannot advance to the next Place."""
</span><span style="color: green; font-style: italic"># Phase 2: Special handling for NinjaAnt
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: blue; font-weight: bold">return </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>place<span style="font-weight: bold">.</span>ant <span style="color: blue; font-weight: bold">is not </span><span style="color: blue">None
</span><span style="color: blue; font-weight: bold">def </span>action<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>colony<span style="font-weight: bold">):
</span><span style="color: darkred">"""A Bee's action stings the Ant that blocks its exit if it is blocked,
or moves to the exit of its current place otherwise.
colony -- The AntColony, used to access game state information.
"""
</span><span style="color: blue; font-weight: bold">if </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>blocked<span style="font-weight: bold">():
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>sting<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>place<span style="font-weight: bold">.</span>ant<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">if </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>place<span style="font-weight: bold">.</span>name <span style="font-weight: bold">!= </span><span style="color: red">'Hive' </span><span style="color: blue; font-weight: bold">and </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>armor <span style="font-weight: bold">> </span><span style="color: red">0</span><span style="font-weight: bold">:
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>move_to<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>place<span style="font-weight: bold">.</span>exit<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">class </span>Ant<span style="font-weight: bold">(</span>Insect<span style="font-weight: bold">):
</span><span style="color: darkred">"""An Ant occupies a place and does work for the colony."""
</span>implemented <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">False </span><span style="color: green; font-style: italic"># Only implemented Ant classes should be instantiated
</span>damage <span style="font-weight: bold">= </span><span style="color: red">0
</span>food_cost <span style="font-weight: bold">= </span><span style="color: red">0
</span><span style="color: blue; font-weight: bold">def </span>__init__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>armor<span style="font-weight: bold">=</span><span style="color: red">1</span><span style="font-weight: bold">):
</span><span style="color: darkred">"""Create an Ant with an armor quantity."""
</span>Insect<span style="font-weight: bold">.</span>__init__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>armor<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">def </span>is_ant<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
</span><span style="color: blue; font-weight: bold">return True
class </span>HarvesterAnt<span style="font-weight: bold">(</span>Ant<span style="font-weight: bold">):
</span><span style="color: darkred">"""HarvesterAnt produces 1 additional food per turn for the colony."""
</span>name <span style="font-weight: bold">= </span><span style="color: red">'Harvester'
</span>implemented <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">True
def </span>action<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>colony<span style="font-weight: bold">):
</span><span style="color: darkred">"""Produce 1 additional food for the colony.
colony -- The AntColony, used to access game state information.
"""
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: blue; font-weight: bold">def </span>random_or_none<span style="font-weight: bold">(</span>l<span style="font-weight: bold">):
</span><span style="color: darkred">"""Return a random element of list l, or return None if l is empty."""
</span><span style="color: blue; font-weight: bold">return </span>random<span style="font-weight: bold">.</span>choice<span style="font-weight: bold">(</span>l<span style="font-weight: bold">) </span><span style="color: blue; font-weight: bold">if </span>l <span style="color: blue; font-weight: bold">else </span><span style="color: blue">None
</span><span style="color: blue; font-weight: bold">class </span>ThrowerAnt<span style="font-weight: bold">(</span>Ant<span style="font-weight: bold">):
</span><span style="color: darkred">"""ThrowerAnt throws a leaf each turn at the nearest Bee in its range."""
</span>name <span style="font-weight: bold">= </span><span style="color: red">'Thrower'
</span>implemented <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">True
</span>damage <span style="font-weight: bold">= </span><span style="color: red">1
</span><span style="color: blue; font-weight: bold">def </span>nearest_bee<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>hive<span style="font-weight: bold">):
</span><span style="color: darkred">"""Return the nearest Bee in a Place that is not the Hive, connected to
the ThrowerAnt's Place by following entrances.
This method returns None if there is no such Bee.
Problem B5: This method returns None if there is no Bee in range.
"""
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: blue; font-weight: bold">return </span>random_or_none<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>place<span style="font-weight: bold">.</span>bees<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">def </span>throw_at<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>target<span style="font-weight: bold">):
</span><span style="color: darkred">"""Throw a leaf at the target Bee, reducing its armor."""
</span><span style="color: blue; font-weight: bold">if </span>target <span style="color: blue; font-weight: bold">is not </span><span style="color: blue">None</span><span style="font-weight: bold">:
</span>target<span style="font-weight: bold">.</span>reduce_armor<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>damage<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">def </span>action<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>colony<span style="font-weight: bold">):
</span><span style="color: darkred">"""Throw a leaf at the nearest Bee in range."""
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>throw_at<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>nearest_bee<span style="font-weight: bold">(</span>colony<span style="font-weight: bold">.</span>hive<span style="font-weight: bold">))
</span><span style="color: blue; font-weight: bold">class </span>Hive<span style="font-weight: bold">(</span>Place<span style="font-weight: bold">):
</span><span style="color: darkred">"""The Place from which the Bees launch their assault.
assault_plan -- An AssaultPlan; when & where bees enter the colony.
"""
</span>name <span style="font-weight: bold">= </span><span style="color: red">'Hive'
</span><span style="color: blue; font-weight: bold">def </span>__init__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>assault_plan<span style="font-weight: bold">):
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>name <span style="font-weight: bold">= </span><span style="color: red">'Hive'
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>assault_plan <span style="font-weight: bold">= </span>assault_plan
<span style="color: blue">self</span><span style="font-weight: bold">.</span>bees <span style="font-weight: bold">= []
</span><span style="color: blue; font-weight: bold">for </span>bee <span style="color: blue; font-weight: bold">in </span>assault_plan<span style="font-weight: bold">.</span>all_bees<span style="font-weight: bold">:
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>add_insect<span style="font-weight: bold">(</span>bee<span style="font-weight: bold">)
</span><span style="color: green; font-style: italic"># The following attributes are always None for a Hive
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>entrance <span style="font-weight: bold">= </span><span style="color: blue">None
self</span><span style="font-weight: bold">.</span>ant <span style="font-weight: bold">= </span><span style="color: blue">None
self</span><span style="font-weight: bold">.</span>exit <span style="font-weight: bold">= </span><span style="color: blue">None
</span><span style="color: blue; font-weight: bold">def </span>strategy<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>colony<span style="font-weight: bold">):
</span>exits <span style="font-weight: bold">= [</span>p <span style="color: blue; font-weight: bold">for </span>p <span style="color: blue; font-weight: bold">in </span>colony<span style="font-weight: bold">.</span>places<span style="font-weight: bold">.</span>values<span style="font-weight: bold">() </span><span style="color: blue; font-weight: bold">if </span>p<span style="font-weight: bold">.</span>entrance <span style="color: blue; font-weight: bold">is </span><span style="color: blue">self</span><span style="font-weight: bold">]
</span><span style="color: blue; font-weight: bold">for </span>bee <span style="color: blue; font-weight: bold">in </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>assault_plan<span style="font-weight: bold">.</span>get<span style="font-weight: bold">(</span>colony<span style="font-weight: bold">.</span>time<span style="font-weight: bold">, []):
</span>bee<span style="font-weight: bold">.</span>move_to<span style="font-weight: bold">(</span>random<span style="font-weight: bold">.</span>choice<span style="font-weight: bold">(</span>exits<span style="font-weight: bold">))
</span><span style="color: blue; font-weight: bold">class </span>AntColony<span style="font-weight: bold">(</span>object<span style="font-weight: bold">):
</span><span style="color: darkred">"""An ant collective that manages global game state and simulates time.
Attributes:
time -- elapsed time
food -- the colony's available food total
queen -- the place where the queen resides
places -- A list of all places in the colony (including a Hive)
bee_entrances -- A list of places that bees can enter
"""
</span><span style="color: blue; font-weight: bold">def </span>__init__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>strategy<span style="font-weight: bold">, </span>hive<span style="font-weight: bold">, </span>ant_types<span style="font-weight: bold">, </span>create_places<span style="font-weight: bold">, </span>food<span style="font-weight: bold">=</span><span style="color: red">4</span><span style="font-weight: bold">):
</span><span style="color: darkred">"""Create an AntColony for simulating a game.
Arguments:
strategy -- a function to deploy ants to places
hive -- a Hive full of bees
ant_types -- a list of ant constructors
create_places -- a function that creates the set of places
"""
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>time <span style="font-weight: bold">= </span><span style="color: red">0
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>food <span style="font-weight: bold">= </span>food
<span style="color: blue">self</span><span style="font-weight: bold">.</span>strategy <span style="font-weight: bold">= </span>strategy
<span style="color: blue">self</span><span style="font-weight: bold">.</span>hive <span style="font-weight: bold">= </span>hive
<span style="color: blue">self</span><span style="font-weight: bold">.</span>ant_types <span style="font-weight: bold">= </span>OrderedDict<span style="font-weight: bold">((</span>a<span style="font-weight: bold">.</span>name<span style="font-weight: bold">, </span>a<span style="font-weight: bold">) </span><span style="color: blue; font-weight: bold">for </span>a <span style="color: blue; font-weight: bold">in </span>ant_types<span style="font-weight: bold">)
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>configure<span style="font-weight: bold">(</span>hive<span style="font-weight: bold">, </span>create_places<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">def </span>configure<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>hive<span style="font-weight: bold">, </span>create_places<span style="font-weight: bold">):
</span><span style="color: darkred">"""Configure the places in the colony."""
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>queen <span style="font-weight: bold">= </span>Place<span style="font-weight: bold">(</span><span style="color: red">'AntQueen'</span><span style="font-weight: bold">)
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>places <span style="font-weight: bold">= </span>OrderedDict<span style="font-weight: bold">()
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>bee_entrances <span style="font-weight: bold">= []
</span><span style="color: blue; font-weight: bold">def </span>register_place<span style="font-weight: bold">(</span>place<span style="font-weight: bold">, </span>is_bee_entrance<span style="font-weight: bold">):
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>places<span style="font-weight: bold">[</span>place<span style="font-weight: bold">.</span>name<span style="font-weight: bold">] = </span>place
<span style="color: blue; font-weight: bold">if </span>is_bee_entrance<span style="font-weight: bold">:
</span>place<span style="font-weight: bold">.</span>entrance <span style="font-weight: bold">= </span>hive
<span style="color: blue">self</span><span style="font-weight: bold">.</span>bee_entrances<span style="font-weight: bold">.</span>append<span style="font-weight: bold">(</span>place<span style="font-weight: bold">)
</span>register_place<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>hive<span style="font-weight: bold">, </span><span style="color: blue; font-weight: bold">False</span><span style="font-weight: bold">)
</span>create_places<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>queen<span style="font-weight: bold">, </span>register_place<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">def </span>simulate<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
</span><span style="color: darkred">"""Simulate an attack on the ant colony (i.e., play the game)."""
</span><span style="color: blue; font-weight: bold">while </span>len<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>queen<span style="font-weight: bold">.</span>bees<span style="font-weight: bold">) == </span><span style="color: red">0 </span><span style="color: blue; font-weight: bold">and </span>len<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>bees<span style="font-weight: bold">) > </span><span style="color: red">0</span><span style="font-weight: bold">:
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>hive<span style="font-weight: bold">.</span>strategy<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">) </span><span style="color: green; font-style: italic"># Bees invade
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>strategy<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">) </span><span style="color: green; font-style: italic"># Ants deploy
</span><span style="color: blue; font-weight: bold">for </span>ant <span style="color: blue; font-weight: bold">in </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>ants<span style="font-weight: bold">: </span><span style="color: green; font-style: italic"># Ants take actions
</span><span style="color: blue; font-weight: bold">if </span>ant<span style="font-weight: bold">.</span>armor <span style="font-weight: bold">> </span><span style="color: red">0</span><span style="font-weight: bold">:
</span>ant<span style="font-weight: bold">.</span>action<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">for </span>bee <span style="color: blue; font-weight: bold">in </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>bees<span style="font-weight: bold">: </span><span style="color: green; font-style: italic"># Bees take actions
</span><span style="color: blue; font-weight: bold">if </span>bee<span style="font-weight: bold">.</span>armor <span style="font-weight: bold">> </span><span style="color: red">0</span><span style="font-weight: bold">:
</span>bee<span style="font-weight: bold">.</span>action<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">)
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>time <span style="font-weight: bold">+= </span><span style="color: red">1
</span><span style="color: blue; font-weight: bold">if </span>len<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>queen<span style="font-weight: bold">.</span>bees<span style="font-weight: bold">) > </span><span style="color: red">0</span><span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">print</span><span style="font-weight: bold">(</span><span style="color: red">'The ant queen has perished. Please try again.'</span><span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">print</span><span style="font-weight: bold">(</span><span style="color: red">'All bees are vanquished. You win!'</span><span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">def </span>deploy_ant<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>place_name<span style="font-weight: bold">, </span>ant_type_name<span style="font-weight: bold">):
</span><span style="color: darkred">"""Place an ant if enough food is available.
This method is called by the current strategy to deploy ants.
"""
</span>constructor <span style="font-weight: bold">= </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>ant_types<span style="font-weight: bold">[</span>ant_type_name<span style="font-weight: bold">]
</span><span style="color: blue; font-weight: bold">if </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>food <span style="font-weight: bold">< </span>constructor<span style="font-weight: bold">.</span>food_cost<span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">print</span><span style="font-weight: bold">(</span><span style="color: red">'Not enough food remains to place ' </span><span style="font-weight: bold">+ </span>ant_type_name<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>places<span style="font-weight: bold">[</span>place_name<span style="font-weight: bold">].</span>add_insect<span style="font-weight: bold">(</span>constructor<span style="font-weight: bold">())
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>food <span style="font-weight: bold">-= </span>constructor<span style="font-weight: bold">.</span>food_cost
<span style="color: blue; font-weight: bold">def </span>remove_ant<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>place_name<span style="font-weight: bold">):
</span><span style="color: darkred">"""Remove an Ant from the Colony."""
</span>place <span style="font-weight: bold">= </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>places<span style="font-weight: bold">[</span>place_name<span style="font-weight: bold">]
</span><span style="color: blue; font-weight: bold">if </span>place<span style="font-weight: bold">.</span>ant <span style="color: blue; font-weight: bold">is not </span><span style="color: blue">None</span><span style="font-weight: bold">:
</span>place<span style="font-weight: bold">.</span>remove_insect<span style="font-weight: bold">(</span>place<span style="font-weight: bold">.</span>ant<span style="font-weight: bold">)
</span>@property
<span style="color: blue; font-weight: bold">def </span>ants<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
</span><span style="color: blue; font-weight: bold">return </span><span style="font-weight: bold">[</span>p<span style="font-weight: bold">.</span>ant <span style="color: blue; font-weight: bold">for </span>p <span style="color: blue; font-weight: bold">in </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>places<span style="font-weight: bold">.</span>values<span style="font-weight: bold">() </span><span style="color: blue; font-weight: bold">if </span>p<span style="font-weight: bold">.</span>ant <span style="color: blue; font-weight: bold">is not </span><span style="color: blue">None</span><span style="font-weight: bold">]
</span>@property
<span style="color: blue; font-weight: bold">def </span>bees<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
</span><span style="color: blue; font-weight: bold">return </span><span style="font-weight: bold">[</span>b <span style="color: blue; font-weight: bold">for </span>p <span style="color: blue; font-weight: bold">in </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>places<span style="font-weight: bold">.</span>values<span style="font-weight: bold">() </span><span style="color: blue; font-weight: bold">for </span>b <span style="color: blue; font-weight: bold">in </span>p<span style="font-weight: bold">.</span>bees<span style="font-weight: bold">]
</span>@property
<span style="color: blue; font-weight: bold">def </span>insects<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
</span><span style="color: blue; font-weight: bold">return </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>ants <span style="font-weight: bold">+ </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>bees
<span style="color: blue; font-weight: bold">def </span>__str__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
</span>status <span style="font-weight: bold">= </span><span style="color: red">' (Food: {0}, Time: {1})'</span><span style="font-weight: bold">.</span>format<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>food<span style="font-weight: bold">, </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>time<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">return </span>str<span style="font-weight: bold">([</span>str<span style="font-weight: bold">(</span>i<span style="font-weight: bold">) </span><span style="color: blue; font-weight: bold">for </span>i <span style="color: blue; font-weight: bold">in </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>ants <span style="font-weight: bold">+ </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>bees<span style="font-weight: bold">]) + </span>status
<span style="color: blue; font-weight: bold">def </span>ant_types<span style="font-weight: bold">():
</span><span style="color: darkred">"""Return a list of all implemented Ant classes."""
</span>all_ant_types <span style="font-weight: bold">= []
</span>new_types <span style="font-weight: bold">= [</span>Ant<span style="font-weight: bold">]
</span><span style="color: blue; font-weight: bold">while </span>new_types<span style="font-weight: bold">:
</span>new_types <span style="font-weight: bold">= [</span>t <span style="color: blue; font-weight: bold">for </span>c <span style="color: blue; font-weight: bold">in </span>new_types <span style="color: blue; font-weight: bold">for </span>t <span style="color: blue; font-weight: bold">in </span>c<span style="font-weight: bold">.</span>__subclasses__<span style="font-weight: bold">()]
</span>all_ant_types<span style="font-weight: bold">.</span>extend<span style="font-weight: bold">(</span>new_types<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">return </span><span style="font-weight: bold">[</span>t <span style="color: blue; font-weight: bold">for </span>t <span style="color: blue; font-weight: bold">in </span>all_ant_types <span style="color: blue; font-weight: bold">if </span>t<span style="font-weight: bold">.</span>implemented<span style="font-weight: bold">]
</span><span style="color: blue; font-weight: bold">def </span>interactive_strategy<span style="font-weight: bold">(</span>colony<span style="font-weight: bold">):
</span><span style="color: darkred">"""A strategy that starts an interactive session and lets the user make
changes to the colony.
For example, one might deploy a ThrowerAnt to the first tunnel by invoking:
colony.deploy_ant('tunnel_0_0', 'Thrower')
"""
</span><span style="color: blue; font-weight: bold">print</span><span style="font-weight: bold">(</span><span style="color: red">'colony: ' </span><span style="font-weight: bold">+ </span>str<span style="font-weight: bold">(</span>colony<span style="font-weight: bold">))
</span>msg <span style="font-weight: bold">= </span><span style="color: red">'<Control>-D (<Control>-Z <Enter> on Windows) completes a turn.\n'
</span>interact<span style="font-weight: bold">(</span>msg<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">def </span>start_with_strategy<span style="font-weight: bold">(</span>args<span style="font-weight: bold">, </span>strategy<span style="font-weight: bold">):
</span>usage <span style="font-weight: bold">= </span><span style="color: darkred">"""python3 [ants.py|ants_gui.py] [OPTIONS]
Run the Ants vs. SomeBees project.
-h, --help Prints this help message
-f, --full Loads a full layout and assault plan
-w, --water Loads a full map with water.
-i, --insane Loads an insane assault plan. Good luck!
"""
</span><span style="color: blue; font-weight: bold">if </span><span style="color: red">"-h" </span><span style="color: blue; font-weight: bold">in </span>args <span style="color: blue; font-weight: bold">or </span><span style="color: red">"--help" </span><span style="color: blue; font-weight: bold">in </span>args<span style="font-weight: bold">:
</span><span style="color: blue; font-weight: bold">print</span><span style="font-weight: bold">(</span>usage<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">return
</span>assault_plan <span style="font-weight: bold">= </span>make_test_assault_plan<span style="font-weight: bold">()
</span>layout <span style="font-weight: bold">= </span>test_layout
<span style="color: blue; font-weight: bold">if </span><span style="color: red">'-f' </span><span style="color: blue; font-weight: bold">in </span>args <span style="color: blue; font-weight: bold">or </span><span style="color: red">'--full' </span><span style="color: blue; font-weight: bold">in </span>args<span style="font-weight: bold">:
</span>assault_plan <span style="font-weight: bold">= </span>make_full_assault_plan<span style="font-weight: bold">()
</span>layout <span style="font-weight: bold">= </span>dry_layout
<span style="color: blue; font-weight: bold">if </span><span style="color: red">'-w' </span><span style="color: blue; font-weight: bold">in </span>args <span style="color: blue; font-weight: bold">or </span><span style="color: red">'--water' </span><span style="color: blue; font-weight: bold">in </span>args<span style="font-weight: bold">:
</span>layout <span style="font-weight: bold">= </span>mixed_layout
<span style="color: blue; font-weight: bold">if </span><span style="color: red">'-i' </span><span style="color: blue; font-weight: bold">in </span>args <span style="color: blue; font-weight: bold">or </span><span style="color: red">'--insane' </span><span style="color: blue; font-weight: bold">in </span>args<span style="font-weight: bold">:
</span>assault_plan <span style="font-weight: bold">= </span>make_insane_assault_plan<span style="font-weight: bold">()
</span>AntColony<span style="font-weight: bold">(</span>strategy<span style="font-weight: bold">, </span>Hive<span style="font-weight: bold">(</span>assault_plan<span style="font-weight: bold">), </span>ant_types<span style="font-weight: bold">(), </span>layout<span style="font-weight: bold">).</span>simulate<span style="font-weight: bold">()
</span><span style="color: green; font-style: italic">###########
# Layouts #
###########
</span><span style="color: blue; font-weight: bold">def </span>mixed_layout<span style="font-weight: bold">(</span>queen<span style="font-weight: bold">, </span>register_place<span style="font-weight: bold">, </span>length<span style="font-weight: bold">=</span><span style="color: red">8</span><span style="font-weight: bold">, </span>tunnels<span style="font-weight: bold">=</span><span style="color: red">3</span><span style="font-weight: bold">, </span>moat_frequency<span style="font-weight: bold">=</span><span style="color: red">3</span><span style="font-weight: bold">):
</span><span style="color: darkred">"""Register Places with the colony."""
</span><span style="color: blue; font-weight: bold">for </span>tunnel <span style="color: blue; font-weight: bold">in </span>range<span style="font-weight: bold">(</span>tunnels<span style="font-weight: bold">):
</span>exit <span style="font-weight: bold">= </span>queen
<span style="color: blue; font-weight: bold">for </span>step <span style="color: blue; font-weight: bold">in </span>range<span style="font-weight: bold">(</span>length<span style="font-weight: bold">):
</span><span style="color: blue; font-weight: bold">if </span>moat_frequency <span style="font-weight: bold">!= </span><span style="color: red">0 </span><span style="color: blue; font-weight: bold">and </span><span style="font-weight: bold">(</span>step <span style="font-weight: bold">+ </span><span style="color: red">1</span><span style="font-weight: bold">) % </span>moat_frequency <span style="font-weight: bold">== </span><span style="color: red">0</span><span style="font-weight: bold">:
</span>exit <span style="font-weight: bold">= </span>Water<span style="font-weight: bold">(</span><span style="color: red">'water_{0}_{1}'</span><span style="font-weight: bold">.</span>format<span style="font-weight: bold">(</span>tunnel<span style="font-weight: bold">, </span>step<span style="font-weight: bold">), </span>exit<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">else</span><span style="font-weight: bold">:
</span>exit <span style="font-weight: bold">= </span>Place<span style="font-weight: bold">(</span><span style="color: red">'tunnel_{0}_{1}'</span><span style="font-weight: bold">.</span>format<span style="font-weight: bold">(</span>tunnel<span style="font-weight: bold">, </span>step<span style="font-weight: bold">), </span>exit<span style="font-weight: bold">)
</span>register_place<span style="font-weight: bold">(</span>exit<span style="font-weight: bold">, </span>step <span style="font-weight: bold">== </span>length <span style="font-weight: bold">- </span><span style="color: red">1</span><span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">def </span>test_layout<span style="font-weight: bold">(</span>queen<span style="font-weight: bold">, </span>register_place<span style="font-weight: bold">, </span>length<span style="font-weight: bold">=</span><span style="color: red">8</span><span style="font-weight: bold">, </span>tunnels<span style="font-weight: bold">=</span><span style="color: red">1</span><span style="font-weight: bold">):
</span>mixed_layout<span style="font-weight: bold">(</span>queen<span style="font-weight: bold">, </span>register_place<span style="font-weight: bold">, </span>length<span style="font-weight: bold">, </span>tunnels<span style="font-weight: bold">, </span><span style="color: red">0</span><span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">def </span>dry_layout<span style="font-weight: bold">(</span>queen<span style="font-weight: bold">, </span>register_place<span style="font-weight: bold">, </span>length<span style="font-weight: bold">=</span><span style="color: red">8</span><span style="font-weight: bold">, </span>tunnels<span style="font-weight: bold">=</span><span style="color: red">3</span><span style="font-weight: bold">):
</span>mixed_layout<span style="font-weight: bold">(</span>queen<span style="font-weight: bold">, </span>register_place<span style="font-weight: bold">, </span>length<span style="font-weight: bold">, </span>tunnels<span style="font-weight: bold">, </span><span style="color: red">0</span><span style="font-weight: bold">)
</span><span style="color: green; font-style: italic">#################
# Assault Plans #
#################
</span><span style="color: blue; font-weight: bold">class </span>AssaultPlan<span style="font-weight: bold">(</span>dict<span style="font-weight: bold">):
</span><span style="color: darkred">"""The Bees' plan of attack for the Colony. Attacks come in timed waves.
An AssaultPlan is a dictionary from times (int) to waves (list of Bees).
>>> AssaultPlan().add_wave(4, 2)
{4: [Bee(3, None), Bee(3, None)]}
"""
</span><span style="color: blue; font-weight: bold">def </span>__init__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>bee_armor<span style="font-weight: bold">=</span><span style="color: red">3</span><span style="font-weight: bold">):
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>bee_armor <span style="font-weight: bold">= </span>bee_armor
<span style="color: blue; font-weight: bold">def </span>add_wave<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>time<span style="font-weight: bold">, </span>count<span style="font-weight: bold">):
</span><span style="color: darkred">"""Add a wave at time with count Bees that have the specified armor."""
</span>bees <span style="font-weight: bold">= [</span>Bee<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>bee_armor<span style="font-weight: bold">) </span><span style="color: blue; font-weight: bold">for </span>_ <span style="color: blue; font-weight: bold">in </span>range<span style="font-weight: bold">(</span>count<span style="font-weight: bold">)]
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>setdefault<span style="font-weight: bold">(</span>time<span style="font-weight: bold">, []).</span>extend<span style="font-weight: bold">(</span>bees<span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">return </span><span style="color: blue">self
</span>@property
<span style="color: blue; font-weight: bold">def </span>all_bees<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
</span><span style="color: darkred">"""Place all Bees in the hive and return the list of Bees."""
</span><span style="color: blue; font-weight: bold">return </span><span style="font-weight: bold">[</span>bee <span style="color: blue; font-weight: bold">for </span>wave <span style="color: blue; font-weight: bold">in </span><span style="color: blue">self</span><span style="font-weight: bold">.</span>values<span style="font-weight: bold">() </span><span style="color: blue; font-weight: bold">for </span>bee <span style="color: blue; font-weight: bold">in </span>wave<span style="font-weight: bold">]
</span><span style="color: blue; font-weight: bold">def </span>make_test_assault_plan<span style="font-weight: bold">():
</span><span style="color: blue; font-weight: bold">return </span>AssaultPlan<span style="font-weight: bold">().</span>add_wave<span style="font-weight: bold">(</span><span style="color: red">2</span><span style="font-weight: bold">, </span><span style="color: red">1</span><span style="font-weight: bold">).</span>add_wave<span style="font-weight: bold">(</span><span style="color: red">3</span><span style="font-weight: bold">, </span><span style="color: red">1</span><span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">def </span>make_full_assault_plan<span style="font-weight: bold">():
</span>plan <span style="font-weight: bold">= </span>AssaultPlan<span style="font-weight: bold">().</span>add_wave<span style="font-weight: bold">(</span><span style="color: red">2</span><span style="font-weight: bold">, </span><span style="color: red">1</span><span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">for </span>time <span style="color: blue; font-weight: bold">in </span>range<span style="font-weight: bold">(</span><span style="color: red">3</span><span style="font-weight: bold">, </span><span style="color: red">15</span><span style="font-weight: bold">, </span><span style="color: red">2</span><span style="font-weight: bold">):
</span>plan<span style="font-weight: bold">.</span>add_wave<span style="font-weight: bold">(</span>time<span style="font-weight: bold">, </span><span style="color: red">1</span><span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">return </span>plan<span style="font-weight: bold">.</span>add_wave<span style="font-weight: bold">(</span><span style="color: red">15</span><span style="font-weight: bold">, </span><span style="color: red">8</span><span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">def </span>make_insane_assault_plan<span style="font-weight: bold">():
</span>plan <span style="font-weight: bold">= </span>AssaultPlan<span style="font-weight: bold">(</span><span style="color: red">4</span><span style="font-weight: bold">).</span>add_wave<span style="font-weight: bold">(</span><span style="color: red">1</span><span style="font-weight: bold">, </span><span style="color: red">2</span><span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">for </span>time <span style="color: blue; font-weight: bold">in </span>range<span style="font-weight: bold">(</span><span style="color: red">3</span><span style="font-weight: bold">, </span><span style="color: red">15</span><span style="font-weight: bold">):
</span>plan<span style="font-weight: bold">.</span>add_wave<span style="font-weight: bold">(</span>time<span style="font-weight: bold">, </span><span style="color: red">1</span><span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">return </span>plan<span style="font-weight: bold">.</span>add_wave<span style="font-weight: bold">(</span><span style="color: red">15</span><span style="font-weight: bold">, </span><span style="color: red">20</span><span style="font-weight: bold">)
</span><span style="color: green; font-style: italic">##############
# Extensions #
##############
</span><span style="color: blue; font-weight: bold">class </span>Water<span style="font-weight: bold">(</span>Place<span style="font-weight: bold">):
</span><span style="color: darkred">"""Water is a place that can only hold 'watersafe' insects."""
</span><span style="color: blue; font-weight: bold">def </span>add_insect<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>insect<span style="font-weight: bold">):
</span><span style="color: darkred">"""Add insect if it is watersafe, otherwise reduce its armor to 0."""
</span><span style="color: blue; font-weight: bold">print</span><span style="font-weight: bold">(</span><span style="color: red">'added'</span><span style="font-weight: bold">, </span>insect<span style="font-weight: bold">, </span>insect<span style="font-weight: bold">.</span>watersafe<span style="font-weight: bold">)
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: blue; font-weight: bold">class </span>FireAnt<span style="font-weight: bold">(</span>Ant<span style="font-weight: bold">):
</span><span style="color: darkred">"""FireAnt cooks any Bee in its Place when it expires."""
</span>name <span style="font-weight: bold">= </span><span style="color: red">'Fire'
</span>damage <span style="font-weight: bold">= </span><span style="color: red">3
"*** YOUR CODE HERE ***"
</span>implemented <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">False
def </span>reduce_armor<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>amount<span style="font-weight: bold">):
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: blue; font-weight: bold">class </span>LongThrower<span style="font-weight: bold">(</span>ThrowerAnt<span style="font-weight: bold">):
</span><span style="color: darkred">"""A ThrowerAnt that only throws leaves at Bees at least 3 places away."""
</span>name <span style="font-weight: bold">= </span><span style="color: red">'Long'
"*** YOUR CODE HERE ***"
</span>implemented <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">False
class </span>ShortThrower<span style="font-weight: bold">(</span>ThrowerAnt<span style="font-weight: bold">):
</span><span style="color: darkred">"""A ThrowerAnt that only throws leaves at Bees within 3 places."""
</span>name <span style="font-weight: bold">= </span><span style="color: red">'Short'
"*** YOUR CODE HERE ***"
</span>implemented <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">False
class </span>WallAnt<span style="font-weight: bold">(</span>Ant<span style="font-weight: bold">):
</span><span style="color: darkred">"""WallAnt is an Ant which has a large amount of armor."""
</span>name <span style="font-weight: bold">= </span><span style="color: red">'Wall'
"*** YOUR CODE HERE ***"
</span>implemented <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">False
def </span>__init__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span>Ant<span style="font-weight: bold">.</span>__init__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">class </span>NinjaAnt<span style="font-weight: bold">(</span>Ant<span style="font-weight: bold">):
</span><span style="color: darkred">"""NinjaAnt is an Ant which does not block the path and does 1 damage to
all Bees in the exact same Place."""
</span>name <span style="font-weight: bold">= </span><span style="color: red">'Ninja'
"*** YOUR CODE HERE ***"
</span>implemented <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">False
def </span>action<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>colony<span style="font-weight: bold">):
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: blue; font-weight: bold">class </span>ScubaThrower<span style="font-weight: bold">(</span>ThrowerAnt<span style="font-weight: bold">):
</span><span style="color: darkred">"""ScubaThrower is a ThrowerAnt which is watersafe."""
</span>name <span style="font-weight: bold">= </span><span style="color: red">'Scuba'
"*** YOUR CODE HERE ***"
</span>implemented <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">False
class </span>HungryAnt<span style="font-weight: bold">(</span>Ant<span style="font-weight: bold">):
</span><span style="color: darkred">"""HungryAnt will take three "turns" to eat a Bee in the same space as it.
While eating, the HungryAnt can't eat another Bee.
"""
</span>name <span style="font-weight: bold">= </span><span style="color: red">'Hungry'
"*** YOUR CODE HERE ***"
</span>implemented <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">False
def </span>__init__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
</span>Ant<span style="font-weight: bold">.</span>__init__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">)
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: blue; font-weight: bold">def </span>eat_bee<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>bee<span style="font-weight: bold">):
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: blue; font-weight: bold">def </span>action<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>colony<span style="font-weight: bold">):
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: blue; font-weight: bold">class </span>BodyguardAnt<span style="font-weight: bold">(</span>Ant<span style="font-weight: bold">):
</span><span style="color: darkred">"""BodyguardAnt provides protection to other Ants."""
</span>name <span style="font-weight: bold">= </span><span style="color: red">'Bodyguard'
"*** YOUR CODE HERE ***"
</span>implemented <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">False
def </span>__init__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
</span>Ant<span style="font-weight: bold">.</span>__init__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span><span style="color: red">2</span><span style="font-weight: bold">)
</span><span style="color: blue">self</span><span style="font-weight: bold">.</span>ant <span style="font-weight: bold">= </span><span style="color: blue">None </span><span style="color: green; font-style: italic"># The Ant hidden in this bodyguard
</span><span style="color: blue; font-weight: bold">def </span>contain_ant<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>ant<span style="font-weight: bold">):
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: blue; font-weight: bold">def </span>reduce_armor<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>amount<span style="font-weight: bold">):
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: blue; font-weight: bold">def </span>action<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>colony<span style="font-weight: bold">):
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: blue; font-weight: bold">class </span>QueenAnt<span style="font-weight: bold">(</span>ThrowerAnt<span style="font-weight: bold">):
</span><span style="color: darkred">"""The Queen of the colony. The game is over if a bee enters her place."""
</span>name <span style="font-weight: bold">= </span><span style="color: red">'Queen'
"*** YOUR CODE HERE ***"
</span>implemented <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">False
def </span>__init__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
</span>ThrowerAnt<span style="font-weight: bold">.</span>__init__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span><span style="color: red">1</span><span style="font-weight: bold">)
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: blue; font-weight: bold">def </span>action<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>colony<span style="font-weight: bold">):
</span><span style="color: darkred">"""A queen ant throws a leaf, but also doubles the damange of ants
behind her. Impostor queens do only one thing: die."""
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: blue; font-weight: bold">class </span>AntRemover<span style="font-weight: bold">(</span>Ant<span style="font-weight: bold">):
</span><span style="color: darkred">"""Allows the player to remove ants from the board in the GUI."""
</span>name <span style="font-weight: bold">= </span><span style="color: red">'Remover'
</span>implemented <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">True
def </span>__init__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
</span>Ant<span style="font-weight: bold">.</span>__init__<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span><span style="color: red">0</span><span style="font-weight: bold">)
</span><span style="color: green; font-style: italic">##################
# Status Effects #
##################
</span><span style="color: blue; font-weight: bold">def </span>make_slow<span style="font-weight: bold">(</span>action<span style="font-weight: bold">):
</span><span style="color: darkred">"""Return a new action method that calls action every other turn.
action -- An action method of some Bee
"""
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: blue; font-weight: bold">def </span>make_stun<span style="font-weight: bold">(</span>action<span style="font-weight: bold">):
</span><span style="color: darkred">"""Return a new action method that does nothing.
action -- An action method of some Bee
"""
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: blue; font-weight: bold">def </span>apply_effect<span style="font-weight: bold">(</span>effect<span style="font-weight: bold">, </span>bee<span style="font-weight: bold">, </span>duration<span style="font-weight: bold">):
</span><span style="color: darkred">"""Apply a status effect to a Bee that lasts for duration turns."""
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span><span style="color: blue; font-weight: bold">class </span>SlowThrower<span style="font-weight: bold">(</span>ThrowerAnt<span style="font-weight: bold">):
</span><span style="color: darkred">"""ThrowerAnt that causes Slow on Bees."""
</span>name <span style="font-weight: bold">= </span><span style="color: red">'Slow'
"*** YOUR CODE HERE ***"
</span>implemented <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">False
def </span>throw_at<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>target<span style="font-weight: bold">):
</span><span style="color: blue; font-weight: bold">if </span>target<span style="font-weight: bold">:
</span>apply_effect<span style="font-weight: bold">(</span>make_slow<span style="font-weight: bold">, </span>target<span style="font-weight: bold">, </span><span style="color: red">3</span><span style="font-weight: bold">)
</span><span style="color: blue; font-weight: bold">class </span>StunThrower<span style="font-weight: bold">(</span>ThrowerAnt<span style="font-weight: bold">):
</span><span style="color: darkred">"""ThrowerAnt that causes Stun on Bees."""
</span>name <span style="font-weight: bold">= </span><span style="color: red">'Stun'
"*** YOUR CODE HERE ***"
</span>implemented <span style="font-weight: bold">= </span><span style="color: blue; font-weight: bold">False
def </span>throw_at<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>target<span style="font-weight: bold">):
</span><span style="color: blue; font-weight: bold">if </span>target<span style="font-weight: bold">:
</span>apply_effect<span style="font-weight: bold">(</span>make_stun<span style="font-weight: bold">, </span>target<span style="font-weight: bold">, </span><span style="color: red">1</span><span style="font-weight: bold">)
</span>@main
<span style="color: blue; font-weight: bold">def </span>run<span style="font-weight: bold">(*</span>args<span style="font-weight: bold">):
</span>start_with_strategy<span style="font-weight: bold">(</span>args<span style="font-weight: bold">, </span>interactive_strategy<span style="font-weight: bold">)
</span>
</pre>
</body>
</html>