Critters! Implement the class Bear to match the class diagram in Figure 1 and mo
ID: 3630459 • Letter: C
Question
Critters!
Implement the class Bear to match the class diagram in Figure 1 and model the behaviors described below:
A bear looks like the letter `B', always scratches in a ght, and always moves either north or west(randomly choosing which to do on any given turn.)
Here is the Bear.cpp and Bear.h (where the code should be added) and Critter.cpp is complete
Bear.cpp
#include "Bear.h"
// Write your definition for the methods in your Bear class here,
// except for any methods that you wrote inlined into the class
// declaration.
Bear.h
#ifndef BEAR_H
#define BEAR_H
#include "Critter.h"
// Write your class declaration for your Bear 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!
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;
}
Explanation / Answer
#include using namespace std; // forward-declaration to allow use in Iter class IntVector; class Iter { public: Iter (const IntVector* p_vec, int pos) : _pos( pos ) , _p_vec( p_vec ) { } // these three methods form the basis of an iterator for use with // a range-based for loop bool operator!= (const Iter& other) const { return _pos != other._pos; } // this method must be defined after the definition of IntVector // since it needs to use it int operator* () const; const Iter& operator++ () { ++_pos; // although not strictly necessary for a range-based for loop // following the normal convention of returning a value from // operator++ is a good idea. return *this; } private: int _pos; const IntVector *_p_vec; }; class IntVector { public: IntVector () { } int get (int col) const { return _data[ col ]; } Iter begin () const { return Iter( this, 0 ); } Iter end () const { return Iter( this, 100 ); } void set (int index, int val) { _data[ index ] = val; } private: int _data[ 100 ]; }; int Iter::operator* () const { return _p_vec->get( _pos ); } // sample usage of the range-based for loop on IntVector int main() { IntVector v; for ( int i = 0; i < 100; i++ ) { v.set( i , i ); } for ( int i : v ) { coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.