Question: C++ I have a creature base class, and 2 derived class from it- ant and doodlebug I also have a gameboard class which has a

C++

I have a creature base class, and 2 derived class from it- ant and doodlebug

I also have a gameboard class which has a 2d array where each element is either one of the 2 creatures or Null

If inside a function I want to move a creature across the gameboard, how would I go about doing so? and How would I check which creature is in which spot?

(I already made the function in each of the creature classes int ant::getType() { return 1) and doodlebug::getType{return 2) with the virtual function virtual int creature::getType() = 0; ) I also made copy constructor, assignment constructor and destrucor in creature class.

Example- I want to move an ant right.

Right now this is what I have and It is not working, what is the correct way to do this?

Code:

board::moveAntRight() {

if(this->grid[x][y] != NULL) {

if(this->grid[x][y]->getType() == 1) {

ant* thisAnt = (ant*)grid[x][y];

if(this->grid[x+1][y]==NULL) {

//ant moves right

grid[x+1][y] = thisAnt;

ant* newAnt = thisAnt;

delete thisAnt;

newAnt->turnsMoved++;

}

}

}

What am I doing wrong and what is the correct way to do this? Thanks

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!