Solved

How do I get objects to interact?

Posted on 1998-01-13
4
173 Views
Last Modified: 2010-04-10
Hi, My question is...
I have 2 classes. CRock and CPerson. How do I detect when the person object hits a rock object?
(I would like a OO solution please)
Ive included the sample code to make things clearer.

#include <stdio.h>
#include <iostream.h">

class CPerson
{
private:
      int x,y;//location
public:
      CPerson()
      {
            x=0;
            y=0;
      }
      ~CPerson(){}
      void move(int nChar);
      void show();
};

void CPerson::show()
{
      printf("person[%d,%d]\n",x,y);
}

void CPerson::move(int nChar)
{
      int oldx=x;
      int oldy=y;
      switch(nChar)
      {
      case 1: x++;  break; //right
      case 2: x--;  break; //left
      case 3: y++; break; //down
      case 4: y--; break; //up
      default : cout<<"Unknown\n";
      }
      printf("[%d,%d]",x,y);
}

/////////////////////////////////////////////////////////
class CRock
{
private:
      int x,y; //location
public:
      CRock(int X, int Y):x(X),y(Y){}
      ~CRock(){}
      void show();
//      void move(int nChar);
};

void CRock::show()
{
      printf("rock[%d,%d]\n",x,y);
}

///////////////////////////////////////////////////////////
main()
{
      CRock rock(1,0);
      CPerson person;
      person.show();
      rock.show();


      person.move(1);

      return 0;
}
0
Comment
Question by:yakky
  • 2
4 Comments
 
LVL 5

Expert Comment

by:yonat
ID: 1178223
You need to have a CTerrain that knows about all the CRock instances.CPerson should query the CTerain about his new location when he moves. The CTerrain is either a singleton, or there is a global CTerrain object.

0
 

Author Comment

by:yakky
ID: 1178224
Ok so how do I query CTerain from CPerson?
0
 
LVL 32

Accepted Solution

by:
jhance earned 50 total points
ID: 1178225
I'd do it like this with a base class for the two objects and a class that can check for a collision:

#include <iostream.h>

class CThing
{
 protected:
  int x, y;

 public:
  CThing()
  {
   x=0;
   y=0;
  }

  ~CThing()
  {
  }

  virtual void move(int nChar)
  {
   switch(nChar){
    case 1: x++; break;
    case 2: x--; break;
    case 3: y++; break;
    case 4: y--; break;
    default: cout << "ERROR" << endl;
   }
   show();
  }

  virtual void show(void){
    cout << "thing: " << x << " : " << y << endl;
  }

  virtual bool collision(CThing &ouch){
   if(ouch.x == x && ouch.y == y){
    cout << "collision!" << endl;
    return true;
   }
   else{
    return false;
   }
  }
};

class CPerson : public CThing
{
 public:
   void show(void){
    cout << "person: " << x << " : " << y << endl;
   };
};

class CRock : public CThing
{
 public:
   void show(void){
    cout << "rock: " << x << " : " << y << endl;
   }
};

void main()
{
 CPerson person;
 CRock rock;

 person.show();
 rock.show();
 person.collision(rock);

 person.move(1);
 person.collision(rock);

 person.move(2);
 person.collision(rock);
}

0
 

Author Comment

by:yakky
ID: 1178226
Its certainly a good solution.
Thanks
Ian
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

805 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question