Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Critters 1 of 5 CS 132, Spring 2021 Programming Project #5: Critters (20 points) Due: Thursday, February 24, 2022, 11:59 PM Thanks to Marty Stepp and Stuart Reges for parts of this project This...

1 answer below »

Critters
1 of 5
CS 132, Spring 2021
Programming Project #5: Critters (20 points)
Due: Thursday, Fe
uary 24, 2022, 11:59 PM
Thanks to Marty Stepp and Stuart Reges for parts of this project
This assignment focuses on classes and objects. Turn in Ant.h, Ant.cpp, Bird.h, Bird.cpp, Hippo.h,
Hippo.cpp, Vulture.h, Vulture.cpp, Cat.h, and Cat.cpp. Download the zip of the supporting files from the
course web site. Run main.cpp to start the simulation.
Program Behavior:
You are provided with several client program classes that implement a
graphical simulation of a 2D world of animals. You will write classes that
define the behavior of those animals. Animals move and behave in different
ways. Your classes will define the unique behaviors for each animal.
The critter world is divided into cells with integer coordinates. The world is
60 cells wide and 50 cells tall. The upper-left cell has coordinates (0, 0); x
increases to the right and y increases downward.
Movement
On each round of the simulation, the simulator asks each critter object which direction it wants to move. Each round a
critter can move one square north, south, east, west, or stay at its cu
ent location ("center"). The world has a finite size,
ut it wraps around in all four directions (for example, moving east from the right edge
ings you back to the left edge).
This program will be confusing at first, because you do not write its main function; your code is not in control of the
overall execution. Instead, your objects are part of a larger system. You might want your critters make several moves at
once using a loop, but you can't. The only way a critter moves is to wait for the simulator to ask it for a single move and
eturn that move. This experience can be frustrating, but it is a good way to practice object-oriented programming.
Fighting/Mating
As the simulation runs, animals may collide by moving onto the same location. When two animals collide, if they are
from different species, they fight. The winning animal survives and the losing animal is removed from the game. Each
animal chooses one of ROAR, POUNCE, or SCRATCH. Each attack is strong against one other (e.g. roar beats scratch) and
weak against another (roar loses to pounce). The following table summarizes the choices and which animal will win in
each case. To remember which beats which, notice that the starting letters of "Roar, Pounce, Scratch" match those of
"Rock, Paper, Scissors." If the animals make the same choice, the winner is chosen at random.
Critter #2
ROAR POUNCE SCRATCH
Critter #1 ROAR random winner #2 wins #1 wins
POUNCE #1 wins random winner #2 wins
SCRATCH #2 wins #1 wins random winner
If two animals of the same species collide, they "mate" to produce a baby. Animals are vulnerable to attack while mating:
any other animal that collides with them will defeat them. An animal can mate only once during its lifetime.
Eating
The simulation world also contains food (represented by the period character, ".") for the animals to eat. There are
pieces of food on the world initially, and new food slowly grows into the world over time. As an animal moves, it may
encounter food, in which case the simulator will ask your animal whether it wants to eat it. Different kinds of animals
have different eating behavior; some always eat, and others only eat under certain conditions.
Every time one class of animals eats a few pieces of food, that animal will be put to "sleep" by the simulator for a small
amount of time. While asleep, animals cannot move, and if they enter a fight with another animal, they will always lose.
Scoring
The simulator keeps a score for each class of animal, shown on the right side of the screen. A class's score is based on
how many animals of that class are alive, how much food they have eaten, and how many other animals they have killed.
2 of 5
Provided Files:
Each class you write will extend a superclass named Critter. This is an example of inheritance, as discussed in class.
Inheritance makes it easier for our code to talk to your critter classes, and it helps us be sure that all your animal classes
will implement all the member functions we need. Your class headers should indicate the inheritance by writing :
public Critter, like the following:
class Ant : public Critter { ...
The Critter class contains the following member functions, some / all of which you must write in each of your classes:
• bool eat()
When your animal encounters food, our code calls this on it to ask whether it wants to eat (true) or not (false).
• Attack fight(string opponent)
When two animals move onto the same square of the grid, they fight. When they collide, our code calls this on
each animal to ask it what kind of attack it wants to use in a fight with the given opponent.
• string getColor()
Every time the board updates, our code calls this on your animal to ask it what color it wants to be drawn with.
• Direction getMove()
Every time the board updates, our code calls this on your animal to ask it which way it wants to move.
• string getType() const
Every time the board updates, our code calls this on your animal to ask what its type name is.
• string toString()
Every time the board updates, our code calls this on your animal to ask what letter it should be drawn as.
Just by writing : public Critter as shown above, you receive a default version of all of these member functions
except getType. The default behavior is to never eat, to always forfeit fights, to use the color black, to always stand still
(a move of CENTER), and a toString of "?". If you don't want this default, rewrite (ove
ide) the member functions in
your class with your own behavior.
For example, below is a critter class Stone. A Stone is displayed with the letter S, is gray in color, never moves or eats,
and always roars in a fight. Your classes will look like this class, except with member variables, a constructor, and more
sophisticated code. The Stone does not need an eat or getMove member function; it uses the default behavior for those
operations.
#ifndef _STONE_H
#define _STONE_H

#include "Critter.h"

class Stone : public Critter {
public:
Stone();
virtual Attack fight(std::string) ove
ide;
virtual std::string getColor() ove
ide;
virtual std::string getType() const ove
ide;
virtual std::string toString() ove
ide;
};

#endif
_STONE_H
NOTE: You are not necessarily required to write : public Critter on every single animal class you write. If you
find that two animal classes are very similar to each other, you should have one extend the other to reduce redundancy.
Running the Simulator:
When you press the Go button, it begins a series of turns. On each turn, the simulator does the following for each animal:
• move the animal once (calling its getMove member function), in random order
3 of 5
• if the animal has moved onto an occupied square, fight! (call both animals' fight member functions) or mate
• if the animal has moved onto food, ask it if it wants to eat (call the animal's eat member functions)
After moving all animals, the simulator redraws the screen, asking each animal for its toString and getColor values.
It can be difficult to test and debug with many animals. We suggest adjusting the initial settings to use a smaller world
and fewer animals. You will find these settings in Nursery.cpp. There is also a Debug checkbox that, when checked,
prints console output about the game behavior.
Critter Classes:
The following are the five animal classes to implement. Each has one constructor that accepts exactly the parameter(s) in
the table. For random moves, each choice must be equally likely; use rand().
1. Ant
constructor Ant(bool walkSouth)
color red
eating behavior always returns true
fighting behavior always scratch
movement if the Ant was constructed with a walkSouth value of true, then
alternates between south and east in a zigzag (S, E, S, E, ...); otherwise,
if the Ant was constructed with a walkSouth value of false, then
alternates between north and east in a zigzag (N, E, N, E, ...)
toString "%" (percent)
2. Bird
constructor Bird()
color blue
eating behavior never eats (always returns false)
fighting behavior roars if the opponent looks like an Ant ("%"); otherwise pounces
movement a clockwise square: first goes north 3 times, then east 3 times,
then south 3 times, then west 3 times, then repeats
toString "^" (caret) if the bird's last move was north or it has not moved;
">" (greater-than) if the bird's last move was east;
"V" (uppercase letter v) if the bird's last move was south;
"<" (less-than) if the bird's last move was west
3. Hippo
constructor Hippo(int hunger)
color gray if the hippo is still hungry (if eat would return true); otherwise white
eating behavior returns true the first hunger times it is called, and false after that
fighting behavior if this Hippo is hungry (if eat would return true), then scratches; else pounces
movement moves 5 steps in a random direction (north, south, east, or west)
Answered 4 days After Feb 19, 2022

Solution

Chirag answered on Feb 24 2022
106 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here