-
April 5th, 2010, 10:02 AM
#1
Compare 2 objects in 2 different classes
i got 3 classes and i want to compare zombie class x and y with man class x and y
what would be the best way to do it? because i want to do a kinda collision check when zombie will collide with the man.
thanks
here is my code:
man.h
Code:
#pragma once
#include"RandomNumber.h"
class Man
{
private:
int x;
int y;
public:
Man(void);
~Man(void);
void set_x(int);
void set_y(int);
void movement(void);
void show_position(void);
};
man.cpp
Code:
#include<iostream>
#include<time.h>
#include<stdlib.h>
#include"RandomNumber.h"
#include"console.h"
#include"Man.h"
Man::Man(void)
{
int x = 0;
int y = 0;
}
void Man::set_x(int X)
{
x = X;
}
void Man::set_y(int Y)
{
y = Y;
}
void Man::movement(void)
{
int move = 0;
std::cin>> move;
if(move == 8)
{
y = y - 1;
}
else if(move == 2)
{
y = y + 1;
}
else if(move == 6)
{
x = x + 1;
}
else
{
x = x - 1;
}
}
void Man::show_position(void)
{
gotoxy(x,y);
{
std::cout<< "M";
}
}
Man::~Man(void)
{
}
zombie.h
Code:
#pragma once
#include"RandomNumber.h"
class Zombie
{
private:
int x;
int y;
RandomNumber Random;
public:
Zombie(void);
~Zombie(void);
void set_x(int);
void set_y(int);
int get_rand(int);
void movement(void);
void show_position(void);
};
zombie.cpp
Code:
#include<iostream>
#include<time.h>
#include<stdlib.h>
#include"RandomNumber.h"
#include"console.h"
#include"Zombie.h"
Zombie::Zombie(void)
{
int x = 0;
int y = 0;
}
void Zombie::set_x(int X)
{
x = X;
}
void Zombie::set_y(int Y)
{
y = Y;
}
int Zombie::get_rand(int upper)
{
return Random.rand_gen(upper);
}
void Zombie::movement(void)
{
int choice;
choice = get_rand(4);
if(choice == 0)
{
y = y - 1;
}
else if(choice == 1)
{
y = y + 1;
}
else if(choice == 2)
{
x = x + 1;
}
else
{
x = x - 1;
}
}
void Zombie::show_position(void)
{
gotoxy(x,y);
{
std::cout<< "Z";
}
}
Zombie::~Zombie(void)
{
}
-
April 5th, 2010, 10:42 AM
#2
Re: Compare 2 objects in 2 different classes
I'd probably just define a Coordinate or Point class holding the x and y. Then give each Man and each Zombie a Coordinate attribute, and then simply check if those are equal for the collision check.
-
April 5th, 2010, 01:33 PM
#3
Re: Compare 2 objects in 2 different classes
Originally Posted by Lindley
I'd probably just define a Coordinate or Point class holding the x and y. Then give each Man and each Zombie a Coordinate attribute, and then simply check if those are equal for the collision check.
so i should just make a new class which would deal with coordinates? it would have 2 variables x and y.
Would i then need my other two classes for the objects? because i will need 3 objects, man; and multiple instances of zombie and a hole.
-
April 5th, 2010, 02:09 PM
#4
Re: Compare 2 objects in 2 different classes
Originally Posted by Lindley
I'd probably just define a Coordinate or Point class holding the x and y. Then give each Man and each Zombie a Coordinate attribute, and then simply check if those are equal for the collision check.
I dont think this is good object oriented programming. Allow me to explain why i feel so?
Well, a co ordinate does not define a person object. So it does not belong there. The co ordinate here defines where the person is, relative to some point. For instance the person may be on a floor. So, the position should be defined with respect to one point on the floor or something like that.
Would like to hear your views on the same.
-
April 5th, 2010, 02:19 PM
#5
Re: Compare 2 objects in 2 different classes
Originally Posted by ChessMaster
I dont think this is good object oriented programming. Allow me to explain why i feel so?
Well, a co ordinate does not define a person object. So it does not belong there. The co ordinate here defines where the person is, relative to some point. For instance the person may be on a floor. So, the position should be defined with respect to one point on the floor or something like that.
Would like to hear your views on the same.
i think i understand you.
i am making a simple game, with a man (which you control) zombies which move randomly and holes which both man and zombies must avoid.
and the only thing i could come up with was what you saw above, i must agree i am quite bad at planning. I would really like some kind advice "/
-
April 5th, 2010, 02:27 PM
#6
Re: Compare 2 objects in 2 different classes
From what I have read about your problem.....
Well, you need a man cclass & a zombie class to do what they are supposed to do. If the men and zombies are in a room.....you can have an initial point and then positions where each one is. Question now is, who will keep track of the positions of the men & zombies. There is another class which is probably giving instructions to each of these objects. that class should probably keep track of who is where.
-
April 5th, 2010, 02:31 PM
#7
Re: Compare 2 objects in 2 different classes
Originally Posted by ChessMaster
I dont think this is good object oriented programming. Allow me to explain why i feel so?
Well, a co ordinate does not define a person object. So it does not belong there.
That depends on your point of view. You could say that a Man or Zombie has-a Coordinate, or you can say that a Map/Grid has-a Man or Zombie.
Originally Posted by ChessMaster
The co ordinate here defines where the person is, relative to some point. For instance the person may be on a floor. So, the position should be defined with respect to one point on the floor or something like that.
I do not see how this substantiates an objection to Lindley's suggestion.
-
April 5th, 2010, 02:35 PM
#8
Re: Compare 2 objects in 2 different classes
Originally Posted by laserlight
That depends on your point of view. You could say that a Man or Zombie has-a Coordinate, or you can say that a Map/Grid has-a Man or Zombie.
You could even say both at the same time, although keeping the two notions coordinated would be a bit of extra complexity.
The point is, these two things are equivalent:
Code:
struct Man
{
int x, y;
};
struct Man
{
Point position;
}
except that the latter better encapsulates the notion of a position in a single object rather than multiple ints, and is thus (in my view) a better design.
-
April 5th, 2010, 02:37 PM
#9
Re: Compare 2 objects in 2 different classes
Originally Posted by ChessMaster
From what I have read about your problem.....
Well, you need a man cclass & a zombie class to do what they are supposed to do. If the men and zombies are in a room.....you can have an initial point and then positions where each one is. Question now is, who will keep track of the positions of the men & zombies. There is another class which is probably giving instructions to each of these objects. that class should probably keep track of who is where.
so my code so far is ok? because i got random movement of zombie working just fine, using a loop in my main and the movement of the man "/
and your saying i should have a controller class?
-
April 5th, 2010, 02:41 PM
#10
Re: Compare 2 objects in 2 different classes
Using some kind of controller class is one option. It's certainly not the only one.
-
April 5th, 2010, 02:43 PM
#11
Re: Compare 2 objects in 2 different classes
Originally Posted by Lindley
Using some kind of controller class is one option. It's certainly not the only one.
hmmm i see.
I need to find the best way to check the collision between them all.
-
April 6th, 2010, 03:54 AM
#12
Re: Compare 2 objects in 2 different classes
can anyone help me or give me some suggestions how to approach this problem???
-
April 12th, 2010, 03:16 AM
#13
Re: Compare 2 objects in 2 different classes
so does anyone know how would i compare the x in zombie class with x in the man class?
-
April 12th, 2010, 03:37 AM
#14
Re: Compare 2 objects in 2 different classes
I'd say that both Man's and Zombie's are Body's and that a Body has-a position :
Code:
class Body
{
public:
bool CollidesWith( const Body& ) const;
protected:
// "move" functions ...
private:
int x_;
int y_;
};
class Man: public Body { /*...*/ };
class Zombie: public Body { /*...*/ };
int main()
{
Man a_man;
Zombie a_zombie;
if( a_man.CollidesWith( a_zombie ) )
{
// a_zombie.Eats( a_man );
}
}
-
April 12th, 2010, 03:51 AM
#15
Re: Compare 2 objects in 2 different classes
Originally Posted by Mariusmssj
so does anyone know how would i compare the x in zombie class with x in the man class?
The simplest way is to expose the x and y coordinates of both Man and Zombie via two accessor methods (like get_x and get_y).
You then write a free function called collisionCheck, like
Code:
bool collisionCheck(const Man& man, const Zombie& zombie) {
return man.get_x()==zombie.get_x() && man.get_y()==zombie.get_y();
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|