I'm working on an assignment in my computer class, and I'm having a hard time with one section of the code. I will post the assignment guidelines (the bolded part is the code I'm having issues with):
You are going to create a simulated ant colony, in which the user willtake the role of the queen who's duty it will be to manage the healthof the colony. The simulation will proceed in a turn-based fashion,where each turn the user can choose to spend precious food resourceson more ants.
Begin by creating a python file Ants.py with two classes:
Colony
andAnt
.The
Colony
class will have a list of worker ants (initially empty), anamount of food (initially 10), and 3 methods:
breedWorker()
step()
purge()
breedWorker()
creates a newAnt
and adds it to the colony's list ofworker ants. Creating a worker costs 5 of the colony's food. If thereis insufficient food, a message should be printed to the user and noant should be created.
step()
handles the automatic behaviour of the colony for each turn.Namely, this method will control the behaviour of each ant in thecolony. Each turn each ant should eat one food from the colony's foodsupply. If there is not enough food, the ant will lose one point ofhealth instead. Each ant will then forage for food (described below),and any food found will be added back into the colony's food supply.
purge()
scans the list of worker ants, and any ants with 0 health are removed from the list. For each dead ant add 1 to the food supply,because ants recycle!I'm having trouble with understanding how to make sure that all the ants in the list have health, and I'm unsure about how to delete the ants with zero health from the list.
The
Ant
class will have
- Health (initially set to 10)
- A
forage()
method
forage()
determines randomly the luck an ant has while out searchingfor food each turn.
- There is a 5% chance that the ant dies in a horrible accident (health= 0, return no food)
- There is a 40% chance that the ant finds food. The amount found should be a random number between 1 and 5.
- There is a50% chance that the ant finds no food.
- And there is a 5% chance thatthe ant finds sweet nectar! Her health is replenished (i.e., set to10) and she brings back 10 food to the colony! The results of eachant's foraging should be both printed for the user and returned.
Finally, you will need to write a
main()
method to start thesimulation and repeatedly prompt the user for their choices. Each turnthe user should have the option to breed a new worker (for a cost of 5food), or do nothing (no new ants). Your code should check the user'sinput for errors and ask again if it was invalid. Following the user'saction (breed or not), the colony will take one 'step' as describedabove, and lastly, all dead ants should be purged from the colony.Simulation should repeat like this until the colony is out of bothants and enough food to make more ants.
This is my code:
import randomclass Colony(object):def __init__(self):Colony.food = 10Colony.numWorkerAnts = 0Colony.workerAnts = [] def breedWorker(self): print ("Breeding worker...")Colony.numWorkerAnts += 1Colony.food -= 5Colony.workerAnts.append(1)if (Colony.food < 5):print ("Sorry, you do not have enough food to breed a new worker ant!")def step(self):for i in range(0,len(Colony.workerAnts)):Colony.food -= 1class Ant(object):def __init__self(self):Ant.health = 10def forage(self):foodFound = random.random()if (foodFound <= 0.05):print("Ant has died in an accident!")Ant.health = 0 if (foodFound > 0.05 and foodFound < 0.40):amountFood = random.randint(1,5)print("Ant finds "+str(amountFood)+" food.") Colony.food += amountFood if (foodFound >= 0.40 and foodFound < 0.95):print("Ant worker returns empty handed.")if (foodFound >= 0.95):print("Your ant finds sweet nectar! Ant returns "+str(amountFood)+" to the colony and health is restored to "+str(self.health)+"!")amountFood = 10Ant.health = 10 Colony.food = amountFood + Colony.fooddef main():colony = Colony()ant = Ant()while (Colony.numWorkerAnts >= 1 or Colony.food >= 5):print ("Your colony has "+str(Colony.numWorkerAnts)+" ants and "+str(Colony.food)+" food, Your Majesty.")print ("What would you like to do?")print ("0. Do nothing")print ("1. Breed worker (costs 5 food)")prompt = '> 'choice = int(raw_input(prompt))if (choice == 1):colony.breedWorker()colony.step()for i in range(0, len(Colony.workerAnts)):ant.forage() else:for i in range(0, len(Colony.workerAnts)):ant.forage() colony.step()
main()