MCS 275 Spring 2021
Emily Dumas
Finish our robot simulation class hierarchy
Discuss more OOP theory & practice
PatrolBot
walks back and forth.WanderBot
walks about randomly.DestructBot
sits in one place for a while and then self-destructs.PatrolBot
walks back and forth.WanderBot
walks about randomly.DestructBot
sits in one place for a while and then self-destructs.Attributes declared in the class definition, outside of any method, are class attributes.
Class attributes are shared by every instance of the class. Often used for constants.
Contrast with the instance attributes we have used thus far (e.g. self.x = 1
in constructor) which exist separately for each instance.
Beyond adding more robot types, how might me improve or extend the simulation?
Might create a class Arena
that manages the list of bots and the space in which they move. Would have a single .update()
method that updates all bots.
Arena
could have a metthod to render itself as a string for display (or as a PNG, HTML, ...).
If we wanted to add robot interaction or movement constraints
then the Bot
class would need a way to access information about its surroundings.
We might make a parent Arena
a required argument to the Bot
constructor.
Bot.update()
could call methods of Arena
to learn about other robots, movement limits, etc.
e.g. in Bot.update()
:
self.arena.bots_visible_from(self.position,self.sight_range)