@dhayden you got reported? :( I hope I didn't accidentally click that somehow...I actually checked to see if I could "unclick" it and I cannot...so maybe it wasn't me. Oh well, definitely nothing wrong with your answers...Hopefully it doesn't cause you any grief with posting and such.
@dutch, ah ok. When I saw
Organism* organismArray[400]
I thought it was a pointer to an array. But I see now that is not correct. Yeah, I am not sure why I have dynamically allocated it...I do know the size, and the size is fixed. So I'll probably update that.
I noticed that in your code you're doing something with overloading the << operator. I tried to figure out how to do this for multiple classes, and the answers looked very similar to your code, but I'll be honest...I don't really understand how it works. And frankly, I don't understand why it wouldn't work to just have a cout operator overloaded in both the Organism and Ant class...Shouldn't c++ recognize the types of arguments I'm passing in and run the appropriate function? If I pass in an Ant, why can it not run the ant overloaded cout function?
I'll probably keep with the syntax of a single bracket ([400]) since we've not seen the double bracket style allocation before, and I don't want to get points docked for that. I know in other languages that's used as a way to access multi-dimensional arrays. So array[i][x] would be the value of x at i.
I don't feel comfortable posting the project word for word since other students might be searching for answers, but here is a google doc with the instructions:
https://docs.google.com/document/d/12AqeOODT3q_hjQzn9Jcp5OAzRpaJ_F2RggbriJ5lbws/edit?usp=sharing
The organisms are supposed to move according to a turn-based style game, and do various things like breed (create a new organism), eat, or die. So I will have some delete statements, which I think I've handled. For instance, whenever an Ant is "born" I run the below code block.
I includes some other functions for calculating available/nearby spaces.
To be honest, I am not fully sure about how I approached this. I treated the board/grid as a single array, pre-filled with generic Organisms, and then overwritten as necessary. It makes sense to me, but I am sure there is a better way to conceptualize it. I handle out of bounds moves using modulo calculations based on the fixed size of the board.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
void Ant::breed(Organism**& array, int& AntCount) {
int currentPosition = this->getPosition();
int moveChoice = 0;
vector<int> openMoves;
getOpenMoves(array, currentPosition, openMoves);
if(openMoves.size() > 0) {
moveChoice = openMoves[rand() % openMoves.size()];
//delete the object that is there
cout << "*move choice" << array[moveChoice]->getPosition();
delete array[moveChoice];
//create an ant
Ant *ant = new Ant;
//put it in the new position
ant->setPosition(moveChoice, array);
cout << "*AntThatBreeds" << this->getPosition() <<"life: "<<this->getlifeCycles()<<endl;
cout << "*newAnt" << ant->getPosition() <<"life: "<<ant->getlifeCycles()<< endl;
AntCount++;
}
}
|
Anyway, I already a appreciate all your insight so far, and this is a lengthy response, so no pressure to respond to all my points (or any)! :) Thank you again.