PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/3 - Ants/ants.html

https://bitbucket.org/wesleyto/cs61a
HTML | 704 lines | 570 code | 134 blank | 0 comment | 0 complexity | 1a6872922f2aaf81c6f0e4615aad77a1 MD5 | raw file
  1. <html>
  2. <head>
  3. <link href="css/assignments.css" rel="stylesheet" type="text/css">
  4. <title>Proj03</title>
  5. </head>
  6. <body>
  7. <h2>Project 3: Ants Vs. SomeBees</h2>
  8. <blockquote>
  9. <center>
  10. <img src="img/ants_vs_bees.png" height="200px" />
  11. </center>
  12. <p><cite><center>The bees are coming!<br>
  13. Create a better soldier<br>
  14. With inherit-ants.</center></cite></p>
  15. </blockquote>
  16. <h3>Introduction</h3>
  17. <p>In this project, you will create a <a
  18. href="https://secure.wikimedia.org/wikipedia/en/wiki/Tower_defense">tower
  19. defense</a> game called Ants Vs. SomeBees. As the ant queen, you populate
  20. your colony with the bravest ants you can muster. Your ants must protect their
  21. queen from the evil bees that invade your territory. Irritate the bees enough
  22. by throwing leaves at them, and they will be vanquished. Fail to pester the
  23. airborne intruders adequately, and your queen will succumb to the bees' wrath.
  24. This game is inspired by PopCap Games' <a
  25. href="http://www.popcap.com/games/pvz/web">Plants Vs. Zombies</a> &reg;.</p>
  26. <p>This project combines functional and object-oriented programming paradigms,
  27. focusing on the material from Chapter 2.5 of the lecture notes. The project
  28. also involves understanding, extending, and testing a large program with many
  29. related parts.</p>
  30. <p>This project includes several files, but all of your changes will be made to
  31. the first two. You can download all of the project code as a <a
  32. href="proj03.zip">zip archive</a>.</p>
  33. <table cellpadding="10">
  34. <tr>
  35. <td><code><a href="ants.py.html">ants.py</a></code></td>
  36. <td>The game logic of Ants Vs. SomeBees.</td>
  37. </tr>
  38. <tr>
  39. <td><code><a href="written.txt">written.txt</a></code></td>
  40. <td>A place to answer the written problems in the project.</td>
  41. </tr>
  42. <tr>
  43. <td><code><a href="tests.py.html">tests.py</a></code></td>
  44. <td>A series of <i>unit tests</i> to test various parts of your
  45. project.</td>
  46. </tr>
  47. <tr>
  48. <td><code><a href="ants_gui.py.html">ants_gui.py</a></code></td>
  49. <td>Graphics for Ants Vs. SomeBees.</td>
  50. </tr>
  51. <tr>
  52. <td><code><a href="graphics.py.html">graphics.py</a></code></td>
  53. <td>General functions for displaying simple two-dimensional animations.</td>
  54. </tr>
  55. <tr>
  56. <td><code><a href="ucb.py.html">ucb.py</a></code></td>
  57. <td>Utility functions for 61A.</td>
  58. </tr>
  59. </table>
  60. <h3>Logistics</h3>
  61. <p>This is a two-week project. You'll work in a team of two people, person A
  62. and person B. In each part, you will do some of the work separately and some
  63. together with your partner. For example, if a problem is marked A1, then it is
  64. a solo problem for person A. Both partners should read, think about, and
  65. understand the solution to all questions.</p>
  66. <p>Start early! The amount of time it takes to complete a project (or any
  67. program) is unpredictable. Ask for help early and often -- the TAs and lab
  68. assistants are here to help.</p>
  69. <p>In the end, you and your partner will submit one project. Person-specific
  70. problems are graded individually and do not affect your partner's score. There
  71. are 25 possible points for each person. If you choose to work alone, you must
  72. complete the entire project, including all of the questions posed to both
  73. partners.</p>
  74. <p>The only two files that you are required to submit are
  75. <code><a href="ants.py.html">ants.py</a></code> and <code><a href="written.txt">written.txt</a></code>. You do not need to
  76. modify any other files in order to complete the project. To submit the project,
  77. change to the directory where the files are located and run <code>submit
  78. proj3</code>.</p>
  79. <p><b>This project is due by 11:59 PM on Tuesday, July 24th.</b></p>
  80. <h3>Core Concepts</h3>
  81. <p>A game of Ants Vs. SomeBees consists of a series of turns. In each turn, new
  82. bees may enter the ant colony. Then, new ants are placed. Finally, all insects
  83. (ants, then bees) take individual actions: bees sting ants, and ants throw
  84. leaves at bees. The game ends either when a bee reaches the ant queen (you
  85. lose), or the entire bee flotilla has been vanquished (you win).</p>
  86. <p><b>The Colony</b>. The colony consists of several places that are chained
  87. together. The <code>exit</code> of each <code>Place</code> leads to another
  88. <code>Place</code>.</p>
  89. <p><b>Placing Ants</b>. There are two constraints that limit ant production.
  90. Placing an ant uses up some amount of the colony's food, a different amount for
  91. each type of ant. Also, only one ant can occupy each <code>Place</code>.</p>
  92. <p><b>Bees</b>. When it is time to act, a bee either moves to the
  93. <code>exit</code> of its current <code>Place</code> if no ant blocks its path,
  94. or stings an ant that blocks its path.</p>
  95. <p><b>Ants</b>. Each type of ant takes a different action and requires a
  96. different amount of food to place. The two most basic ant types are the
  97. <code>HarvesterAnt</code>, which adds one food to the colony during each turn,
  98. and the <code>ThrowerAnt</code>, which throws a leaf at a bee each turn.</p>
  99. <h3>The Code</h3>
  100. <p>Most concepts in the game have a corresponding class that encapsulates the
  101. logic for that concept. For instance, a <code>Place</code> in the colony holds
  102. insects and connects to other places. A <code>Bee</code> stings ants and
  103. advances through exits.</p>
  104. <p>The game can be run in two modes: as a text-based game or using a graphical
  105. user interface (GUI). The game logic is the same in either case, but the GUI
  106. enforces a turn time limit that makes playing the game more exciting. The
  107. text-based interface is provided for debugging and development.</p>
  108. <p>The files are separated according to these two modes. <code><a href="ants.py.html">ants.py</a></code>
  109. knows nothing of graphics or turn time limits. All graphical elements are
  110. specified in <code><a href="ants_gui.py.html">ants_gui.py</a></code> and <code><a href="graphics.py.html">graphics.py</a></code>. It is
  111. possible to complete this project without ever reading the graphics files.</p>
  112. <p>To start a text-based game, run</p>
  113. <pre>python3 ants.py</pre>
  114. <p>To start a graphical game, run</p>
  115. <pre>python3 ants_gui.py</pre>
  116. <p>When you start the graphical version, a new window should appear:</p>
  117. <center>
  118. <img src="img/gui_explanation.png" style="border: 1px solid #000;"/>
  119. </center>
  120. <p>In the starter implementation, you have unlimited food and your ants only
  121. throw leaves at bees in their current <code>Place</code>. Try playing a game
  122. anyway! You'll need to place a lot of <code>ThrowerAnt</code>s (the second
  123. type) in order to keep the bees from reaching your queen.</p>
  124. <p>You have also been provided a testing file <code><a href="tests.py.html">tests.py</a></code> that runs a
  125. series of unit tests for the project. To test your project, you can run</p>
  126. <pre>python3 tests.py -v</pre>
  127. <p>This command runs all of the unit tests along with any doctests in ants.py.
  128. The optional <code>-v</code> generates more verbose output. If you would like
  129. to learn more about Python's built-in unit testing framework, read the
  130. documentation on the <a
  131. href="http://docs.python.org/py3k/library/unittest.html">unittest module</a>.
  132. Most problems have associated tests. Make sure that the tests for each problem
  133. pass before moving on.</p>
  134. <h4>UNIX Crash Course</h4>
  135. <p>Since the unittests generate a lot of output, knowing a few Unix tricks will
  136. come in handy. The complete
  137. <a href="http://www.ee.surrey.ac.uk/Teaching/Unix/index.html">UNIX tutorial</a>
  138. is a really great resource. For the unittests in this project,
  139. <a href="http://www.ee.surrey.ac.uk/Teaching/Unix/unix3.html">redirection and piping</a>
  140. will be especially useful.</p>
  141. <p>You can use piping to "page" through the output of the tests:</p>
  142. <pre>python3 tests.py -v | less</pre>
  143. <p>This will show one page of the output at a time. Press the spacebar to view
  144. the next page, "d" (down) to scroll down, and "b" (back) to scroll up. Once
  145. you get to the end of the file, you'll exit automatically. However, you can
  146. exit the pager at any time by typing "q" (quit).</p>
  147. <p>You can also redirect the output into a file to view at your leisure:</p>
  148. <pre>python3 tests.py -v > file_name</pre>
  149. <p>Then you can use any text editor you like to view the output of the tests.
  150. A word of caution: redirecting overwrites your file. If you only want to add
  151. to your file, use two "arrows."</p>
  152. <pre>python3 tests.py -v >> file_name</pre>
  153. <h3>Phase 1</h3>
  154. <h4>Both Partners</h4>
  155. <p><b>Problem 1</b> (3 pts). Answer the following written questions in the file
  156. called <code>written.txt</code> after you have read the <i>entire</i>
  157. <code><a href="ants.py.html">ants.py</a></code> file.</p>
  158. <ol>
  159. <li>Which method in which class runs a game? (the answer is not
  160. <code>run</code>, because <code>run</code> is not a method)!</li>
  161. <li>The <code>Hive</code>, a subclass of <code>Place</code>, is the starting
  162. location of the bees. Unlike most instances of <code>Place</code>, the
  163. <code>Hive</code> class does not have an <code>exit</code>. Explain how and
  164. when <code>Bee</code>s leave the <code>Hive</code>.</li>
  165. <li>Explain the mechanism in the code by which the places in the colony are
  166. laid out. How do you modify the code to produce more places?</li>
  167. <li>What is the significance of an <code>Insect</code>'s <code>armor</code>
  168. attribute? What happens when <code>armor</code> reaches 0?</li>
  169. </ol>
  170. <p><b>Problem 2</b> (2 pts). Add food costs and implement harvesters.
  171. Currently, there is no cost for deploying any type of <code>Ant</code>, and so
  172. there is no challenge to the game. You'll notice that <code>Ant</code> starts
  173. out with a base <code>food_cost</code> of 0. Override this value in each of the
  174. subclasses listed below with the correct costs.</p>
  175. <table class="ant">
  176. <tr>
  177. <td><b>Class</b></td>
  178. <td><b>Food</b></td>
  179. <td><b>Armor</b></td>
  180. </tr>
  181. <tr>
  182. <td><img src="img/ant_harvester.gif" /> <br> <code>HarvesterAnt</code></td>
  183. <td>2</td>
  184. <td>1</td>
  185. </tr>
  186. <tr>
  187. <td><img src="img/ant_thrower.gif" /> <br> <code>ThrowerAnt</code></td>
  188. <td>4</td>
  189. <td>1</td>
  190. </td>
  191. </table>
  192. <p>Now there's no way to gather more food! To fix this issue, implement the
  193. <code>HarvesterAnt</code> class. A <code>HarvesterAnt</code> is a type of
  194. <code>Ant</code> that adds one food to the <code>colony.food</code> total as its
  195. <code>action</code>.</p>
  196. <p>Try playing the game again. Once you have placed a
  197. <code>HarvesterAnt</code>, you should accumulate food each turn. Vanquishing
  198. the bees using the default game setup is now possible, but should be
  199. challenging.</p>
  200. <p><b>Problem 3</b> (2 pts). Add code to the <code>Place</code> constructor
  201. that tracks entrances. Right now, a <code>Place</code> keeps track only of its
  202. <code>exit</code>. We would like a <code>Place</code> to keep track of its
  203. entrance as well. A <code>Place</code> needs to track only one
  204. <code>entrance</code>.</p>
  205. <p>However, simply passing an entrance to a <code>Place</code> constructor will
  206. be problematic; we will need to have both the exit and the entrance before we
  207. can create a <code>Place</code>! (It's a <a
  208. href="https://secure.wikimedia.org/wikipedia/en/wiki/Chicken_or_the_egg">chicken
  209. or the egg</a> problem.) To get around this problem, we will keep track of
  210. entrances in the following way instead. The <code>Place</code> constructor
  211. should specify that:</p>
  212. <ul>
  213. <li>A newly created <code>Place</code> always starts with its
  214. <code>entrance</code> as <code>None</code>.</li>
  215. <li>If the <code>Place</code> has an <code>exit</code>, then the
  216. <code>exit</code>'s <code>entrance</code> is set to that
  217. <code>Place</code>.</li>
  218. </ul>
  219. <h4>Person A</h4>
  220. <p><b>Problem A4</b> (2 pts). Add water to the colony. Currently there are
  221. only two types of places, the <code>Hive</code> and a basic <code>Place</code>.
  222. To make things more interesting, we're going to create a new type of
  223. <code>Place</code> called <code>Water</code>.</p>
  224. <p>Only an ant that is <code>watersafe</code> can be deployed to a
  225. <code>Water</code> place. In order to determine whether an <code>Insect</code>
  226. is <code>watersafe</code>, add a new attribute to the <code>Insect</code> class
  227. named <code>watersafe</code> that is <code>False</code> by default. Since bees
  228. can fly, make their <code>watersafe</code> attribute <code>True</code>,
  229. overriding the default.</p>
  230. <p>Now, implement the <code>add_insect</code> method for <code>Water</code>
  231. that, in addition to doing the same thing as a <code>Place</code> would do,
  232. immediately kills the added insect if it is not <code>watersafe</code> by
  233. modifying the <code>Insect</code>'s <code>armor</code>. <b>Do not</b> copy and
  234. paste code. Try to use methods that have already been defined and make use of
  235. inheritance to reuse the functionality of the <code>Place</code> class.</p>
  236. <p>Once you've finished this problem, play a game that includes water. To
  237. access the <code>mixed_layout</code> that includes water, add the
  238. <code>--water</code> option (or <code>-w</code> for short) when you start the
  239. game.</p>
  240. <pre>python3 ants_gui.py --water</pre>
  241. <p><b>Problem A5</b> (3 pts). Implement the <code>FireAnt</code>. A
  242. <code>FireAnt</code> has a special <code>reduce_armor</code> method that, when
  243. the <code>FireAnt</code>'s armor reaches zero or lower, will reduce the armor of
  244. <i>all</i> <code>Bee</code>s in the same <code>Place</code> as the
  245. <code>FireAnt</code> by <code>3</code> (a fiery end indeed).</p>
  246. <table class="ant">
  247. <tr>
  248. <td><b>Class</b></td>
  249. <td><b>Food</b></td>
  250. <td><b>Armor</b></td>
  251. </tr>
  252. <tr>
  253. <td><img src="img/ant_fire.gif" /> <br> <code>FireAnt</code></td>
  254. <td>4</td>
  255. <td>1</td>
  256. </td>
  257. </table>
  258. <p><i>Hint:</i> If you iterate over a list, but change the contents of that list
  259. at the same time, you may not see all the elements. As the <a
  260. href="http://docs.python.org/py3k/tutorial/controlflow.html#for-statements">Python
  261. tutorial</a> suggests, "If you need to modify the list you are iterating over,
  262. you must iterate over a copy." Remember that damaging a bee may cause it to be
  263. removed from its place.</p>
  264. <p>Once you've finished implementing the <code>FireAnt</code>, give it an
  265. instance attribute <code>implemented</code> with the value <code>True</code>.
  266. This attribute tells the game that you've added a new type of
  267. <code>Ant</code>.</p>
  268. <p>After implementing <code>FireAnt</code>, be sure to test your program by
  269. playing a game or two! A <code>FireAnt</code> should destroy any co-located
  270. Bees when it dies.</p>
  271. <h4>Person B</h4>
  272. <p><b>Problem B4</b> (2 pts). Implement the <code>nearest_bee</code> method for
  273. the <code>ThrowerAnt</code> class. In order for a <code>ThrowerAnt</code> to
  274. attack, it must know which bee it should hit. The provided implementation will
  275. only hit bees in the same <code>Place</code>. Your job is to fix it so that a
  276. <code>ThrowerAnt</code> will <code>throw_at</code> the nearest bee in front of
  277. it that is not still in the <code>Hive</code>.</p>
  278. <p>The <code>nearest_bee</code> method returns a random <code>Bee</code> from
  279. the nearest place that contains bees. Places are inspected in order by
  280. following their <code>entrance</code> attributes.</p>
  281. <ul>
  282. <li>Start from the current <code>Place</code> of the
  283. <code>ThrowerAnt</code>.</li>
  284. <li>For each place, return a random bee if there is any, or consider the next
  285. place that is stored as the current place's <code>entrance</code>.</li>
  286. </ul>
  287. <p>After implementing <code>nearest_bee</code>, a <code>ThrowerAnt</code> should
  288. be able to <code>throw_at</code> a <code>Bee</code> in front of it that is not
  289. still in the <code>Hive</code>. Make sure that your ants do the right
  290. thing!</p>
  291. <p><b>Problem B5</b> (3 pts). Now that the <code>ThrowerAnt</code> has been
  292. completed, implement two subclasses of <code>ThrowerAnt</code>.</p>
  293. <ul>
  294. <li>The <code>LongThrower</code> can only <code>throw_at</code> a
  295. <code>Bee</code> that is found after following at least 4
  296. <code>entrance</code> transitions. So the <code>LongThrower</code> can't hit
  297. <code>Bee</code>s that are in the same <code>Place</code> as it or the first 3
  298. <code>Place</code>s in front of it.</li>
  299. <li>The <code>ShortThrower</code> can only <code>throw_at</code> a
  300. <code>Bee</code> that is found after following at most 2 <code>entrance</code>
  301. transitions. So the <code>ShortThrower</code> can only hit <code>Bee</code>s
  302. in the same <code>Place</code> as it and 2 <code>Place</code>s in front of
  303. it.</li>
  304. </ul>
  305. <p>Neither of these specialized throwers can <code>throw_at</code> a
  306. <code>Bee</code> that is exactly 3 <code>Place</code>s away. Placing a single
  307. one of these (and no other ants) should never win a default game.</p>
  308. <table class="ant">
  309. <tr>
  310. <td><b>Class</b></td>
  311. <td><b>Food</b></td>
  312. <td><b>Armor</b></td>
  313. </tr>
  314. <tr>
  315. <td><img src="img/ant_shortthrower.gif" /> <br>
  316. <code>ShortThrower</code></td>
  317. <td>3</td>
  318. <td>1</td>
  319. </tr>
  320. <tr>
  321. <td><img src="img/ant_longthrower.gif" /> <br> <code>LongThrower</code></td>
  322. <td>3</td>
  323. <td>1</td>
  324. </tr>
  325. </table>
  326. <p>To implement these behaviors, modify the <code>nearest_bee</code> method to
  327. reference <code>min_range</code> and <code>max_range</code> attributes, and only
  328. return a bee that is in range.</p>
  329. <p>For the base class, <code>ThrowerAnt</code>, set <code>min_range</code> to 0
  330. and <code>max_range</code> to 10. Then, implement the subclasses
  331. <code>LongThrower</code> and <code>ShortThrower</code> with appropriately
  332. constrained ranges and correct food costs.</p>
  333. <p>Set the <code>implemented</code> class attribute of <code>LongThrower</code>
  334. and <code>ShortThrower</code> to <code>True</code>.</p>
  335. <p>Try playing a game with your newly implemented ants. Be sure that they do
  336. what you expect them to! You can try running <code><a href="ants_gui.py.html">ants_gui.py</a></code> with the
  337. <code>--full</code> option to go up against a full swarm of bees in a
  338. multi-tunnel layout, and add <code>--insane</code> if you want a real challenge!
  339. If the bees are too numerous to vanquish, you might need to create some new ants
  340. in Phase 2.</p>
  341. <h3>Phase 2</h3>
  342. <h4>Person A</h4>
  343. <p><b>Problem A6</b> (1 pts). We are going to add some protection to our
  344. glorious <code>AntColony</code> by implementing the <code>WallAnt</code>, which
  345. is an ant that does nothing each turn (already the default <code>action</code>
  346. of the <code>Ant</code> class). A <code>WallAnt</code> is useful because it has
  347. a large <code>armor</code> value.</p>
  348. <table class="ant">
  349. <tr>
  350. <td><b>Class</b></td>
  351. <td><b>Food</b></td>
  352. <td><b>Armor</b></td>
  353. </tr>
  354. <tr>
  355. <td><img src="img/ant_wall.gif" /> <br> <code>WallAnt</code></td>
  356. <td>4</td>
  357. <td>4</td>
  358. </td>
  359. </table>
  360. <p><b>Problem A7</b> (3 pts). Implement the <code>NinjaAnt</code>, which
  361. damages all <code>Bee</code>s that pass by, but is never seen.</p>
  362. <table class="ant">
  363. <tr>
  364. <td><b>Class</b></td>
  365. <td><b>Food</b></td>
  366. <td><b>Armor</b></td>
  367. </tr>
  368. <tr>
  369. <td><img src="img/ant_ninja.gif" /> <br> <code>NinjaAnt</code></td>
  370. <td>6</td>
  371. <td>1</td>
  372. </td>
  373. </table>
  374. <p>A <code>NinjaAnt</code> is not able to be attacked by a <code>Bee</code>
  375. because it is hidden, nor does it block the path of a <code>Bee</code> that
  376. flies by. To implement this behavior, first modify the <code>Ant</code> class
  377. to include a new class attribute <code>blocks_path</code> that is
  378. <code>True</code> by default. Set the value of <code>blocks_path</code> to
  379. <code>False</code> in the <code>NinjaAnt</code> class.</p>
  380. <p>Second, modify the <code>Bee</code>'s method <code>blocked</code> to return
  381. <code>False</code> if either there is no <code>Ant</code> in the
  382. <code>Bee</code>'s <code>place</code> or if there is an <code>Ant</code>, but
  383. its <code>blocks_path</code> attribute is <code>False</code>. Now
  384. <code>Bee</code>s will just fly past <code>NinjaAnt</code>s.</p>
  385. <p>Finally, we want to make the <code>NinjaAnt</code> damage all
  386. <code>Bee</code>'s that fly past. Implement the <code>action</code> method in
  387. <code>NinjaAnt</code> to reduce the armor of all <code>Bee</code>s in the same
  388. <code>place</code> as the <code>NinjaAnt</code> by 1, overriding the default
  389. action method inherited from <code>Ant</code>.</p>
  390. <p>For a challenge, try to win a default game using only
  391. <code>HarversterAnt</code> and <code>NinjaAnt</code>.</p>
  392. <h4>Person B</h4>
  393. <p><b>Problem B6</b> (1 pts). Currently there are no ants that can be placed on
  394. <code>Water</code>. Implement the <code>ScubaThrower</code>, which is a
  395. subclass of <code>ThrowerAnt</code> that is more costly and
  396. <code>watersafe</code>, but otherwise identical to its base class.</p>
  397. <table class="ant">
  398. <tr>
  399. <td><b>Class</b></td>
  400. <td><b>Food</b></td>
  401. <td><b>Armor</b></td>
  402. </tr>
  403. <tr>
  404. <td><img src="img/ant_scuba.gif" /> <br> <code>ScubaThrower</code></td>
  405. <td>5</td>
  406. <td>1</td>
  407. </tr>
  408. </table>
  409. <p>Placing a <code>ScubaAnt</code> in <code>Water</code> should not cause it to
  410. die.</p>
  411. <p><b>Problem B7</b> (3 pts). We will now implement the new offensive unit
  412. called the <code>HungryAnt</code>, which will eat a random <code>Bee</code> from
  413. its <code>place</code>, instantly killing the <code>Bee</code>. After eating a
  414. <code>Bee</code>, it must spend 3 turns digesting before eating again.</p>
  415. <table class="ant">
  416. <tr>
  417. <td><b>Class</b></td>
  418. <td><b>Food</b></td>
  419. <td><b>Armor</b></td>
  420. </tr>
  421. <tr>
  422. <td><img src="img/ant_hungry.gif" /> <br> <code>HungryAnt</code></td>
  423. <td>4</td>
  424. <td>1</td>
  425. </td>
  426. </table>
  427. <p>To implement, give <code>HungryAnt</code> a <code>time_to_digest</code> class
  428. attribute that holds the number of turns that it takes all
  429. <code>HungryAnt</code>s to digest (default to 3). Also, give each
  430. <code>HungryAnt</code> an instance attribute <code>digesting</code> that counts
  431. the number of turns it has left to digest (default is 0, since it hasn't eaten
  432. anything at the beginning).</p>
  433. <p>Now we implement the <code>action</code> method of the <code>HungryAnt</code>
  434. to check if it's digesting; if so, decrement its <code>digesting</code> counter.
  435. Otherwise, eat a random <code>Bee</code> in its <code>place</code> (killing the
  436. <code>Bee</code> and restarting the <code>digesting</code> timer).</p>
  437. <h4>Both Partners</h4>
  438. <p><b>Problem 8</b> (5 pts). Implement the <code>BushAnt</code>. Right now,
  439. our ants are quite frail. We'd like to provide a way to help them last longer
  440. against the onslaught of the bees. Enter the <code>BushAnt</code>.</p>
  441. <table class="ant">
  442. <tr>
  443. <td><b>Class</b></td>
  444. <td><b>Food</b></td>
  445. <td><b>Armor</b></td>
  446. </tr>
  447. <tr>
  448. <td><img src="img/ant_weeds.gif" /> <br> <code>BushAnt</code></td>
  449. <td>4</td>
  450. <td>2</td>
  451. </tr>
  452. </table>
  453. <p>A <code>BushAnt</code> differs from a normal <code>Ant</code> because it can
  454. occupy the same <code>Place</code> as another ant. When a <code>BushAnt</code>
  455. is added to the same <code>Place</code> as another ant, it shields the other ant
  456. and protects it from damage. Attacks should damage the <code>BushAnt</code>
  457. first and only hurt the protected ant after the <code>BushAnt</code> has
  458. perished.</p>
  459. <p>A <code>BushAnt</code> has an instance attribute <code>ant</code> that stores
  460. the ant contained within the bush. It should start off as <code>None</code>,
  461. indicating that no ant is currently being protected. Give <code>BushAnt</code>
  462. a <code>contain_ant</code> method that takes an <code>Ant</code> argument and
  463. sets the <code>ant</code> instance attribute to that argument.</p>
  464. <p>Now, change your program so that a <code>BushAnt</code> and another
  465. <code>Ant</code> can simultaneously occupy the same <code>Place</code>:</p>
  466. <ol>
  467. <li>Add an <code>Ant.container</code> class attribute that indicates whether
  468. an ant can contain another. For all <code>Ant</code>s except
  469. <code>BushAnt</code>, <code>container</code> should be <code>False</code>.
  470. The <code>BushAnt.container</code> attribute should be <code>True</code>.</li>
  471. <li>We also need to give <code>Ant</code>s a new method,
  472. <code>can_contain</code>, that takes an <code>other</code> ant as an argument
  473. and returns <code>True</code> if and only if:
  474. <ol>
  475. <li>This ant is a container.</li>
  476. <li>This ant does not already contain another ant.</li>
  477. <li>The other ant is not a container.</li>
  478. </ol>
  479. </li>
  480. <li>Right now, if we attempt to put a second ant in a <code>Place</code>, the
  481. <code>add_insect</code> method of the <code>Place</code> class will
  482. immediately cause an error. Change <code>add_insect</code> so that the
  483. <code>Place</code> contains the container ant and the container ant contains
  484. the other ant:
  485. <ul>
  486. <li>If the <code>Ant</code> currently occupying this <code>Place</code>
  487. can contain the <code>Ant</code> we are trying to add, then simply tell it
  488. to do so.</li>
  489. <li>If the <code>Ant</code> we are trying to add can contain the
  490. <code>Ant</code> currently occupying this <code>Place</code>, then have it
  491. do so <em>and</em> set this <code>Place</code>'s ant to be the newly added
  492. <code>Ant</code>.</li>
  493. <li>If neither <code>Ant</code> can contain the other, then raise the same
  494. assertion error as before.</li>
  495. </ul>
  496. </li>
  497. </ol>
  498. <p>Almost done! Just a few more things to do. </p>
  499. <ol>
  500. <li>When a <code>BushAnt</code> perishes, we need to make sure the ant it
  501. currently contains (if it contains one) takes the <code>BushAnt</code>'s
  502. place. Override the <code>reduce_armor</code> method so that, if the
  503. <code>BushAnt</code> perishes, it will set its place's ant to be the ant it
  504. currently contains. (Remember to use inheritance!)</li>
  505. <li>The last step is to make sure that ants that are contained by
  506. <code>BushAnt</code>s still perform their action. Override the
  507. <code>action</code> method for <code>BushAnt</code> accordingly.</li>
  508. </ol>
  509. <p><b>Problem 9</b> (4 pts). Implement two final thrower ants that do no
  510. damage, but instead replace the <code>action</code> method of a <code>Bee</code>
  511. instance that they <code>throw_at</code> with a new method that alters the
  512. <code>Bee</code>'s behavior for some duration.</p>
  513. <p>We will be implementing two new ants that subclass <code>ThrowerAnt</code>.</p>
  514. <ul>
  515. <li><code>SlowThrower</code> applies a slow effect for 3 turns.</li>
  516. <li><code>StunThrower</code> applies a stun effect for 1 turn.</li>
  517. </ul>
  518. <table class="ant">
  519. <tr>
  520. <td><b>Class</b></td>
  521. <td><b>Food</b></td>
  522. <td><b>Armor</b></td>
  523. </tr>
  524. <tr>
  525. <td><img src="img/ant_freeze.gif" /> <br> <code>SlowThrower</code></td>
  526. <td>4</td>
  527. <td>1</td>
  528. </tr>
  529. <tr>
  530. <td><img src="img/ant_stun.gif" /> <br> <code>StunThrower</code></td>
  531. <td>6</td>
  532. <td>1</td>
  533. </tr>
  534. </table>
  535. <p>In order to complete the implementations of these two ants, you will need to
  536. set their class attributes appropriately and implement the following three
  537. functions:</p>
  538. <ul>
  539. <li>
  540. <code>make_slow</code> takes an <code>action</code> method and returns a new
  541. <code>action</code> method which performs the original action on turns where
  542. <code>colony.time</code> is even and does nothing on other turns.
  543. </li>
  544. <li>
  545. <code>make_stun</code> takes an <code>action</code> method and returns a new
  546. <code>action</code> method which does nothing.
  547. </li>
  548. <li>
  549. <code>apply_effect</code> takes an <code>effect</code> (either
  550. <code>make_slow</code> or <code>make_stun</code>), a <code>bee</code>, and a
  551. <code>duration</code>. It then takes the bee's original action along with the
  552. "affected action" (the result of calling <code>effect</code> on the original
  553. action) and replaces the bee's action with a new action method that will call
  554. the affected action for <code>duration</code> turns and then will go back to
  555. calling the original action every turn.
  556. </li>
  557. </ul>
  558. <p>Make sure to test your code! Your code should be able to apply multiple
  559. effects on a target (each new effect applies on top of whatever action method the
  560. bee already has at that point, and the target returns to the previous action
  561. when the new one runs out).</p>
  562. <p><strong>You are now done with the project!</strong>. If you weren't able to
  563. vanquish the bees' insane-mode assault plan before, do your new ants help? Add
  564. some water or design your own layout to keep things interesting.</p>
  565. <p>Feel free to design additional ants, layouts, and assault plans and post them
  566. to Piazza.</p>
  567. <p><b>Acknowledgements:</b> Tom Magrino and Eric Tzeng developed this project
  568. with John DeNero. Jessica Wan contributed the artwork.</p>
  569. </body>
  570. </html>