Code:
#inclde "rabbit.h"
#include <cstdlib>
void Rabbit::grow(Grid<GridCell*>& cells)
{
return;
}
/* If there are carrots, in a cell diagonally from the bunny it gets eaten, irregardless of the turn
*/
void Rabbit::eat(Grid<GridCell*>& cells, int x, int y)
{
int r1, r2, c1, c2;
if(x == 0)
{
r1 = cells.getRows()-1;
}
else
{
r1 = row-1;
}
if(x == cells.getRows()-1)
{
r2 = 0;
}
else
{
r2 = row+1;
}
if(y == 0)
{
c1 = cells.getCols()-1;
}
else
{
c1 = col-1;
}
if(y == cells.getCols()-1)
{
c2 = 0;
}
else
{
c2 = col+1;
}
if(cell->getType()==creature2)
{
if(cells[r1][c1]->getType() == creature1)
{
cells[r1][c1]->setType(DEAD);
food++;
}
if(cells[r1][c2]->getType() == creature1)
{
cells[r1][c2]->setType(DEAD);
food++;
}
if(cells[r2][c1]->getType() == creature1)
{
cells[r2][c1]->setType(DEAD);
food++;
}
else if(cells[r2][col]->getType() == creature1)
{
cells[r2][col]->setType(DEAD);
food++;
}
if(food == 0 && c== 7)
{
cell->setType(DEAD);
// if the creature hasn't eaten for 7 rounds, the creature dies;
}
}
if(c==7) c=0;
}
/*
If two rabbits are neighboours in the left side or the right side of the bunny and the counter is 3 it randomly reproduces two bunnies, in the grid.
*/
void Rabbit::reproduce(Grid<GridCell*>&cells,int x, int y)
{
int r1, r2, c1, c2;
if(x == 0)
{
r1 = cells.getRows()-1;
}
else
{
r1 = row-1;
}
if(x == cells.getRows()-1)
{
r2 = 0;
}
else
{
r2 = row+1;
}
if(y == 0)
{
c1 = cells.getCols()-1;
}
else
{
c1 = col-1;
}
if(y == cells.getCols()-1)
{
c2 = 0;
}
else
{
c2 = col+1;
}
if(cell->getType()==creature2)
{
if(c==3)
{
if(cells[x][c1]->getType() == creature2 || cells[x][c2]->getType() == creature2)
{
for(int i=0; i<2; i++)
{
int col = rand()% cells.getCols();
int row = rand()%cells.getRows(); //randomizes the locations
GridCell *cell = cells[row][col];
if(cell->getType() == DEAD)
{
cell->setType(creature2);
}
else
{
i--;
}
}
}
}
}
if(c==3) c=0;
}
void Rabbit::counter()
{
c++;
}
void Rabbit::neighboors(Grid<GridCell*> celula)
{
return;
}
/*
If there's an empty cell above the rabbit the rabbit moves that way. If not the bunny then checks
downard. If there's not an empty cell down the bunny checks the left, and if there's not an empty cell the
rabbit checks the right.
*/
void Rabbit::move(Grid<GridCell*>&cells,int x, int y)
{
int r1, r2, c1, c2;
if(x == 0)
{
r1 = cells.getRows()-1;
}
else
{
r1 = row-1;
}
if(x == cells.getRows()-1)
{
r2 = 0;
}
else
{
r2 = row+1;
}
if(y == 0)
{
c1 = cells.getCols()-1;
}
else
{
c1 = col-1;
}
if(y == cells.getCols()-1)
{
c2 = 0;
}
else
{
c2 = col+1;
}
if(cell->getType()==creature2)
{
if(cells[r1][y]->getType() == DEAD)
{
Gridcell * cell
cells[r1][y]->setType(creature2);
cell->setType(DEAD);
break;
}
else if(cells[r2][y]->getType() == DEAD)
{
cells[r2][y]->setType(creature2);
cell->setType(DEAD);
break;
}
else if(cells[x][c1]->getType() == DEAD)
{
cells[x][c1]->setType(DEAD);
cell->setType(DEAD);
break;
}
else if(cells[x][c2]->getType() == DEAD)
{
cells[x][c2]->setType(DEAD);
cell->setType(DEAD);
break;
}
else
{
cell->setType(creature2);
}
}
}