I need to make the bugs and ants move randomly throughout the grid. Will this allow for them to move randomly? Or am I simplifying the problem. Here is the code.
Looking at the snippet you provided, it looks like there is a 50% chance a bug or ant will move per call to World::one_step(). Now, that's given that your grid::move() function actually moves the bugs.
Well I need it to traverse the whole grid and have all organisms on the grid move for every time one_step is called. And move() function does work because I have a program that allows all the bugs to move, and then all the ants to move in one stimulation (i.e. calling one_step). Now I'm trying to edit so they move randomly in one stimulation
I don't see a direct problem with what you've posted. I'm assuming that you have called srand() once at the beginning of your program.
Does the move() member function reposition the critter to a random nearby open field?
I think this could certainly use some polishing and design changes, to be honest.
*EDIT* If you need every organism to move every time one_step() is called, then what's the deal with lines 16, 17, 18 and 30?
Yes, the move() function moves the critter to a random nearby field - either up, down, left, or right.
And with your edit, are you saying I wouldn't even need the random? I could just check to see if the spot isn't null and if it's a bug or ant, and move it if it is?
Basically, if you want every bug and ant to move, you don't need the random stuff. The way you have it set up now, every time the function is called, each bug and ant has a 50% chance to move.
for(int j=0; j<WORLDSIZE; j++) {
//check to see if type doodlebug
if((grid[i][j]!=NULL) && (grid[i][j]->get_type()==DOODLEBUG)) {
//check to see if it hasn't moved
if(grid[i][j]->moved==false) {
//set moved to true
grid[i][j]->moved=true;
//call move function
grid[i][j]->move();
}
}
//check to see if type ant
if((grid[i][j]!=NULL) && (grid[i][j]->get_type()==ANT)) {
//check to see if it hasn't moved
if(grid[i][j]->moved==false) {
//set moved to true
grid[i][j]->moved=true;
//call move function
grid[i][j]->move();
}
}
}
Correct, it's the same exact way you had it before, except now, every time your code encounters a bug or ant it will move, before there was only a 50% chance the bug or ant the code encountered would move.