Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,353 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,112 people online right now. Registration is fast and FREE... Join Now!




getting objects to move around a grid

 
Reply to this topicStart new topic

getting objects to move around a grid

link129115
post 30 Aug, 2008 - 10:07 AM
Post #1


New D.I.C Head

*
Joined: 26 Aug, 2008
Posts: 8

I am trying to create a basic predator-prey simulation and right now I am trying to get pointers to move around the grid and I am totally confused on how to get this to work.

Ants are O's and doodlebugs are X's. The ant randomly tries to move up, right, down, or left. I use rand()%4 to get 0, 1, 2, or 3 to choose the direction of movement up, right, down, left. If the neighboring cell in the selected direction is occupied or would move the ant off the grid, then the ant stays in the current cell. The doodlebug will move to an adjacent cell containing an ant and eat the ant. If more than one adjacent cell contain ants, the doodlebug will favor the direction in the order of up, right, down, and left. If no ants are available in adjacent cells, the doodlebug moves according to the same rules as the ants. Also, a doodlebug cannot eat other doodlebugs.
Any help would be appreciated.


Here is what I have so far:

CODE

//this is the header file:

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cmath>
#include <cctype>
using namespace std;


class Organism;
class Map
{  
  public:
    void gridMap();
    void printGrid();
    Organism *grid[20][20];
};

class Organism
{
  public:
    virtual void print()=0;
    virtual int type()= 0;
    void move(int,int);
    virtual void act(int, int) = 0;
};

class Ant: public Organism
{
  public:
    int type(){return 0;}
    void print(){cout<<'O';}
    void act(int, int);
};

class Doodlebug: public Organism
{
  public:
    int type(){return 1;}
    void print(){cout<< 'X';}
    void act(int, int);
};

//This is the member function definitions file:

#include "organism.h"


void Map::gridMap()//initialize the grid
{
  fstream fin;

  int next, r, c;
  for(int i = 0; i < 20; i++){
    for(int j = 0; j<20; j++){
     grid[i][j] = NULL;
    }
  }
  fin.open("ants.txt");
  if(fin.fail()){
    cout << "Input file opening failed.\n";
    exit(1);
  }
  while(fin>>next){
    r = next;
    fin>>next;
    c = next;
   Ant *ant = new Ant;
    grid[c][r] = ant;
  }
  fin.close();
  fin.open("dbs.txt");
  if(fin.fail()){
    cout << "Input file opening failed.\n";
    exit(1);
  }
  while(fin>>next){
    r = next;
    fin>>next;
    c = next;
    Doodlebug *db = new Doodlebug;
    grid[c][r] = db;
  }
  fin.close();
}


void Map::printGrid()
{
  for(int i = 19; i >= 0; i--){
    for(int j=0;j < 20; j++){
      if(grid[i][j] == NULL){
         cout << ' ';
      }
      else
        grid[i][j]->print();
    }
   cout<<endl;
  }
}


void Ant::act(int i, int j)
{
     move(i, j);
      //if(bredcount == 3){
     //   breed(i, j);
     //   bredcount=0;
     // }
}


void Doodlebug::act(int i, int j)
{      
    //  eat(i, j);  
   //   if(!eaten){
        move(i, j);    
   //  }
  //   if(starvecount == 3){
   //     starve(i,j);
    //  }
     // if(bredcount == 8){
      //   breed(i, j);
    //  }  
}      


void Organism::move(int i, int j)
{
  Organism *org[20][20];
  Organism *temp[20][20];
  int direction = rand()%4;

  switch(direction){
     case 0:
            if(i > 0 && (org[i-1][j]->type() != 1 || org[i-1][j]->type() != 0)){
             temp[i][j] = org[i][j];
             org[i-1][j] = temp[i][j];
             org[i][j] = NULL;
         // bredcount++;
           }
           break;
     case 1:
           if(j < 20 && (org[i][j+1]->type() != 0 || org[i][j+1]->type() != 1)){
             temp[i][j] = org[i][j];
             org[i][j+1] = temp[i][j];
             org[i][j] = NULL;
        //  bredcount++;
           }
           break;
     case 2:
           if(j < 20 && (org[i+1][j]->type() != 0 || org[i+1][j]->type() != 1)){
             temp[i][j] = org[i][j];
             org[i+1][j] = temp[i][j];
             org[i][j] = NULL;
       // bredcount++;      
           }
           break;
     case 3:
           if(i > 0 && (org[i][j-1]->type() != 0 || org[i][j-1]->type() != 1)){
             temp[i][j] = org[i][j];
             org[i][j-1] = temp[i][j];
             org[i][j] = NULL;
       //  bredcount++;
           }
           break;
     default:
       // bredcount++;
           break;
     }  
}


//this is the main file:

#include "organism.h"

int main()
{
  srand(1);
  Map mapobject;
  Ant antobject;
  Doodlebug dbobject;
Organism *optr[20][20];
  char next;
  int i, j;
mapobject.gridMap();
cout<<optr[1][1]<<endl;
while(next != 'q'){
    mapobject.printGrid();
    cout<<"if got to the for"<<endl;
     for( i = 0; i < 20; i++){
       for( j = 0; j < 20; j++){
       if(optr[i][j] != NULL){
         if(optr[i][j]->type() == 0){
           cout<<optr[i][j]<<endl;
           optr[i][j]->act(i, j);
         }
         if(optr[i][j]->type() == 1){
           optr[i][j]->act(i, j);
         }
        }
       }
     }
     cout << ">";
     cin >> next;  
}
}



User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 04:03AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month