Thread: help with the grid

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    4

    Question help with the grid

    i'm trying to stimulate a predator-prey relationship. i have a super class from which 2 other derived classes derive from. i think my functions are right, but i can't update the grid. i have looked at the code for the past two days, but still have no idea what to do

  2. #2
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    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);
    		}
    	}
    }

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    4
    Here's my gridwindow also.
    Code:
    void GridWindow::gameLife()
    {
    
    	Carrots *cenoura= new Carrots(); // creates pointers to all the classes
    	Rabbit *coelho= new Rabbit();
    	Snake *cobra=new Snake();
    	for(unsigned int row=0; row < cells.getRows(); row++)
    	{
    		//loop through all the cells in the current column
    		for(unsigned int col=0; col < cells.getCols(); col++)
    		{
    			//grab the current cell & set its value to empty
    			GridCell *cell = cells[row][col];
    			criatura.push_back(cenoura); // pushes back into the vector
    			criatura.push_back(coelho);
    			criatura.push_back(cobra);
    
    			for(int i=0; i<criatura.size();i++)
    			{
    				criatura[i]->grow(cells);	
    				criatura[i]->eat(cells,row,col);
    				criatura[i]->move(cells,row,col)
    				criatura[i]->reproduce(cells,row,col);
    				criatura[i]->counter();
    			}
    		}
    	}
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another hard C problem
    By alexbcg in forum C Programming
    Replies: 12
    Last Post: 11-23-2010, 08:03 AM
  2. More Windows Trouble
    By samGwilliam in forum Windows Programming
    Replies: 9
    Last Post: 04-12-2005, 10:02 AM
  3. Cleaning up a Grid of Memory
    By Epo in forum C++ Programming
    Replies: 9
    Last Post: 02-25-2005, 10:23 AM
  4. Find a word in a 2d grid
    By The_Kingpin in forum C++ Programming
    Replies: 2
    Last Post: 02-24-2005, 05:38 PM
  5. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM