Interactions between two classes

Hello,

I am a beginner in C++ and I am trying to implement a little game.

I have a class maze (which is basically a matrix filled with 0's and 1's that say if the element of the matrix is empty or filled with a wall)
1
2
3
4
5
6
7
8
9
10
11
12
class Maze{
	public:
		Maze(int m, int n);
		~Maze(void);
	   int getX();
	   int getY();
	   void initialize(double p);
	   blitz::Array<bool,2> getM();
	protected:
		blitz::Array<bool,2> M;
		bool generate_bool(double p);
	};


A class Mouse
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Mouse{
		public:
		Mouse();
		Mouse(int posx,int posy);
		~Mouse();
		int getx();
		int gety();
		void set(int _x,int _y);
		
	protected:
		int x;
		int y;
			
	};


My idea would now be to implement a method in the Mouse function called move, that would take a number between 1 and 4 as a parameter (1 = letft, 2 = top, 3=right, 4=bottom) and returns true if the move was possible and false if the move was impossible.

As my maze does not change during the game, I thought of a method like this:
bool move(Maze& m){
....}
so that I do not copy m each time. However, there may be a clever way of doing that if I could directly access m from the class Mouse (I once heard about friend classes, but I do not know that much about it)

Thank you in advance for your help

Best
myssa25
i prefer to create a mouse object inside of the maze object because the mouse exists inside of the maze. but there are a lot of other ways to do the same thing. i would literally make a mouse in your maze.
I personally think it's weird to let a maze create a mouse. In fact, the maze probably doesn't even need to know mouses exist. While a mouse also shouldn't create a labyrinth, it should have some sense of position, so it should keep a reference (read pointer, you can't rebind C++ references so they don't work that well in this situation) to a labyrinth instead of passing a reference every time the mouse moves (after all, a position makes no sense without some reference frame).

Then you could have a game class to tie the two together.
@ui uiho: I thought about this, but I would prefer to keep two different objects

@hanst99: And how would you keep this pointer?
In an instance member of Mouse. Like your protected x and y (which should be private btw) in your Mouse class.
@ hanst99: OK I see. I always put protected because as long as you do not do inheritance, it is exactly the same as private, isn't it ?
Pretty much, but since you want private you might as well write private.
+1 hanst.
Topic archived. No new replies allowed.