Note: Critter.cpp is completed Implement the class Tiger to match the class diag
ID: 3630460 • Letter: N
Question
Note: Critter.cpp is completed
Implement the class Tiger to match the class diagram in Figure 1 and model the behaviors described below:
A tiger looks like the letter `T' and always pounces in a ght. Tigers move in a random direction for three steps, and then walk in another random direction for three steps (for example, the tiger may move east, east, east, then south, south, south, then another random direction for three turns, and so on.) Remember, our simulator asks for each move one at a time, so your tiger won't be making all of these moves all at once.
Tiger.cpp
#include "Tiger.h"
// Write your definition for the methods in your Tiger class here,
// except for any methods that you wrote inlined into the class
// declaration.
#ifndef TIGER_H
#define TIGER_H
Tiger.h
#include "Feline.h"
// Write your class declaration for your Tiger class here.
/* Your constructor and destructor will be VERY simple, so you may
just want to write the constructor and destructor inline right into
your class declaration. */
// Don't forget the semicolon at the end of your class declaration!
#endif
Critter.cpp
#include "Critter.h"
Direction randomDirection() {
return (Direction)(rand() % NUM_DIRECTIONS);
}
Critter::Critter() {
alive = true;
mark = false;
neighbors = new char[5];
for(int i = 0; i < 5; i++) {
neighbors[i] = EMPTY_NEIGHBOR;
}
}
Critter::~Critter() {
delete [] neighbors;
}
char Critter::getChar() {
return '?';
}
// Returns the height of the game simulation world.
unsigned int Critter::getHeight() {
return height;
}
// Returns the animal that is 1 square in the given direction away
// from this animal. A blank space, " ", signifies an empty square.
char Critter::getNeighbor(Direction direction) {
return neighbors[direction];
}
// Returns the width of the game simulation world.
unsigned int Critter::getWidth() {
return width;
}
// Returns this animal's current x-coordinate.
unsigned int Critter::getX() {
return x;
}
// Returns this animal's current y-coordinate.
unsigned int Critter::getY() {
return y;
}
// Returns true if this animal is currently alive.
// This will return false if this animal has lost a fight and died.
bool Critter::isAlive() {
return alive;
}
// Sets whether or not this animal is currently alive.
// This method is called by the simulator and not by your animal itself.
void Critter::setAlive(bool alive) {
this->alive = alive;
}
// Sets the height of the game simulation world to be the given value,
// so that future calls to getHeight will return this value.
// This method is called by the simulator and not by your animal itself.
void Critter::setHeight(unsigned int height) {
this->height = height;
}
// Sets the neighbor of this animal in the given direction to be the given value,
// so that future calls to getNeighbor in that direction will return this value.
// This method is called by the simulator and not by your animal itself.
void Critter::setNeighbor(Direction direction, char value) {
neighbors[direction] = value;
}
// Sets the width of the game simulation world to be the given value.
// so that future calls to getWidth will return this value.
// This method is called by the simulator and not by your animal itself.
void Critter::setWidth(unsigned int width) {
this->width = width;
}
// Sets this animal's memory of its x-coordinate to be the given value.
// so that future calls to getX will return this value.
// This method is called by the simulator and not by your animal itself.
void Critter::setX(unsigned int x) {
this->x = x;
}
// Sets this animal's memory of its y-coordinate to be the given value.
// so that future calls to getY will return this value.
// This method is called by the simulator and not by your animal itself.
void Critter::setY(unsigned int y) {
this->y = y;
}
const char *fightString(Attack a) {
switch(a) {
case ROAR:
return "roar";
case POUNCE:
return "pounce";
case SCRATCH:
return "scratch";
default: // FORFEIT
return "forfeit";
}
}
const char *directionString(Direction d) {
switch(d) {
case NORTH:
return "north";
case SOUTH:
return "south";
case EAST:
return "east";
default: // WEST
return "west";
}
}
// used by your simple testing program to verify the objects
// behave as described
void Critter::printBehavior() {
std::cout << " symbol: " << getChar() << std::endl;
std::cout << " sample movements: ";
for(int i = 0; i < 6; i++) {
std::cout << directionString(getMove()) << " ";
}
std::cout << std::endl;
std::cout << " fighting a !: " << fightString(fight('!')) << std::endl;
std::cout << " fighting a L: " << fightString(fight('L')) << std::endl;
std::cout << " fighting a T: " << fightString(fight('T')) << std::endl;
std::cout << " fighting a B: " << fightString(fight('B')) << std::endl;
std::cout << " fighting a D: " << fightString(fight('D')) << std::endl;
}