CritterInfo

Now that we’ve implemented Mouse and Tiger, we are going to move on to some more complex critters.

Some of these critters rely on the behavior of their opponent to determine how to fight. Every time a critter’s fight() method is called, it is passed a parameter opp_info of type CritterInfo which provides useful information about the current opponent.

About CritterInfo

CritterInfo is a class, which provides getters and other helper methods. Some getters that you might want to use are:

  • get_char() - which returns the critter’s display character
  • get_color() - which returns the critter’s display color

You might be asking why fight() provides a CritterInfo object rather than an object of type Tiger, Mouse, etc. There are two reasons: (1) this makes it simpler for a critter to handle any type of opponent and (2) objects are mutable, so giving direct access to a Critter object could allow someone to accidentally, or maliciously, change instance variables that could break the simulation.

Using CritterInfo

Now that we know about CritterInfo, let’s imagine using it. For instance, inside a critter’s fight() method, you can call opp_info.get_char() to get the opponent critter’s display character and opp_info.get_color() to get its color.

Note that a every time a critter’s get_move() method is called, it is also passed a parameter self_info also of type CritterInfo—this time, the information being passed in is about the current (self) critter, not of an opponent. Once again, some of the information (for example, knowing what neighbors you have in your immediate vicinity) may help your critter make better movement choices (i.e., avoidance or attack).