To implement fire ants in CS 61A, override the reduce_health method to first call the inherited method to reduce the fire ant's own health, then save the place before reducing health (since the ant may die and be removed), and finally iterate through all bees in that place to deal reflected damage: if the ant is dead, deal amount + damage attribute (default 3); if alive, deal only the amount.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
[CS 61A SU26] Ants Problem Getting Started
Added:hi welcome to the getting started video for problem five so we want to implement fire ant and fire ant does damage when it receives damage um more specifically if it's damaged by amount um it will also damage that same amount back to all of the bees that are currently in its place and we want to call this reflected damage um additionally if fire ant dies it does that amount of damage plus an additional amount of damage and this additional amount is specified by the damage attribute so it has a default value of three which we've already defined for you in the fire in class and we to implement fire ants we want to override insects reduce Health method so if we look at the insect reduced Health method um right here you'll see that it reduces the insect's Health by whatever amount is passed in so not only do we want to reduce our fire ants Health if we get damage but we will also want to reduce the health of all the bees in our place so that's why we want to override it because we want to do some additional damage or some additional work okay so like I said there's kind of two parts to our new reduced Health method that we want to create we want to reduce our own health and we want to reduce the health of all the bees in our place and luckily for us to reduce our own health we can already we already have like a really handy method that does that for us right this reduced Health method will reduce our own health um so we can call this which is very very useful so um our overridden method and that's what it says here should call the reduced Health method that is inherit inherited from our super class um our super class is actually ant and ant inheritance from its superclass insect and that was the class that I was just showing you that actually contains the reduced Health method we like okay so we want to call the inherited reduce method on the fire ant itself um and one tricky part about this is we don't want to call reduce Health on ourself and the reason we don't want to do that let me find it here is okay cool if we call reduced help on ourselves self dot reduce health um what's going to happen essentially is python is going to say okay let me call this reduced Health method and it calls this method specifically and we just continue in this infinite recursive Loop where we keep calling this method but we're not hitting any base cases no values are actually changing we don't get to any stopping conditions so that's why we want to call our inherited reduced Health method because that one is the one that will actually reduce the health of our insect or of articact so that is step one we want to reduce fire ants help Okay and like I said we do this by calling um the inherited method I think I spelled that wrong but we can ignore that um and this inherited method is an insect and some things you may have learned from lecture is that there's a bunch of cool ways to call an inherited method you can call it through the class um um so you could do like insect Dot um or you can use super up to you some pretty cool ways Okay cool so then we come to step two part two of this problem we also want to reduce the health of all the bees in our spot okay and this is going to be a little bit more tricky so let's worry about um how we want to do this okay okay so the first thing I want to worry about is I want to I know that I want to damage all the bees in my place so how can I access all of the bees in a given place well I guess I'll leave that to you to figure out but there's a very cool um method that we already have that will get us a list of all the bees are in place right so we can figure out what or I'll leave you to figure out what method that is but there's a pretty helpful method for you to use um but then we come to this like important note so what if our Aunt what if our fire ant dies if the ant loses all Health it's actually going to be removed from its place and that means that it won't have a place attribute so we wouldn't be able to do self.place because self the ant will have died it will have been removed from its place so we need to account for that right and one way to account for that is to save the aunt's place we can save all of the information before we actually reduce the fire ant's health and by save it I mean we're going to store the ants place in a variable and this way even if our fire ant dies and it gets removed from its place we can still access that place in whatever variable we choose to store it in up here so when we're getting the list of bees in our place we're going to want to refer to the place we stored up here because we don't know if our aunt is dead or not okay so we should have gotten the list of bees in all of our places by now okay we're going to move on to the next step so we need to figure out how much damage we actually want to deal and we want to make sure we deal that damage to every single beat in our place so we can iterate through all of our bees right because we were able to get a list of all all of our bees using a very hopeful method hopefully you were able to find okay so if we iterate through our bees there are two conditions right either a fire ant is dead in which case we want to deal damage or like we want to deal the amount of damage the um the fire ant received plus damage which is specified in like our damage attribute up here or our fire ant is still alive in which case we only want to deal amount of damage okay so let's let's like write out those two conditions um condition one our aunt is dead so how can we check if our aunt is dead what attribute can we use to check if our ant is dead or not I'll let you figure that out but there is a pretty helpful attribute we can use to check if Rod ant is dead or alive so if the ant is dead we want to deal um amount which is this amount right here how much damage the ant received plus damage which is specified in our damage attribute okay condition two ant is still alive and in this case we want to just deal amount we don't deal damage yet okay so just to run through our logic again pretty briefly um we want to store the ants place in a variable before we even deal damage because we don't know if our ant Will Survive that damage or not then we want to reduce the ants health and remember we do this by calling the inherited method of reduce Health this way we don't get stuck in a recursive Loop and we can do this by calling the insect class itself or by calling super up to you some very helpful methods from lecture the next thing we want to do is now that we've reduced our fire ants Health we actually want to reduce the bee's health so we can do this by getting a list of all of our bees we want to iterate through those bees um and then we come across our two conditions if the ant is dead we want to deal amount plus damage to each to that b otherwise if the ant is still alive we're only going to reduce the bees Health by amount okay and the other thing I want to clarify is how do we deal damage what method do we have that will reduce an insect's health I will let you figure that out and that should be the end of problem five so good luck with this problem um yeah and have fun
Related Videos

TOP 15 Data compression Interview Questions and Answers 2019 Part-2 | Data compression | Wisdom jobs
wisdomjobs
281 views•2019-06-28

CTS 158: 802.11w Management Frame Protection
ClearToSend
4K views•2019-02-04

NDSS 2019 Send Hardest Problems My Way: Probabilistic Path Prioritization for Hybrid Fuzzing
NDSSSymposium
496 views•2019-04-02

How realistic is Cities: Skylines?
CityBeautiful
159K views•2019-02-14

GUIs & TUIs: Choosing a User Interface for Your Python Project | Real Python Podcast
realpython
2K views•2025-04-04

The OSI Model - Explained by Example
hnasr
225K views•2019-05-12

Cloud Computing - Introduction
elithecomputerguy
98K views•2019-10-07

From Traveler's Dilemma to Dynamic Routing | Demystifying Networking
IITBombayJuly
5K views•2019-08-04
Trending

WOW! Judge TURNS THE TABLES on Trump in His OWN $10B LAWSUIT!!!
MeidasTouch
197K views•2026-07-23

Playstation NO DISC/NO BUY Fight Is Over...
DavidJaffeGames
4K views•2026-07-23

Steam and Xbox Just Dropped The Hammer On PlayStation
OhNoItsAlexx
9K views•2026-07-23

Americans Confused in Australia for 17 Minutes Straight
IWrocker
17K views•2026-07-23