Skip to content
Advertisement

How to call an 2D – Array which was iniciated in another class?

I made a minimal reduced example of my problem: The Maze class creates a 2D Boolean Array with the Method generateMaze() (The content of mazeArray is irrelevant in this example). The main-Thread from Walker calls that Method and thereby creates this mazeArray from the Maze-class.

I do not understand how I can call this Array in Walker.walk? Im affraid I have a knowledge gap Every hint is appreciated, Thank you very much.

JavaScript

}

JavaScript

}

Advertisement

Answer

Explanation

There are several basic OOP mistakes here.

First of all, why do you even create an instance of the Maze class when your generateMaze class is static and returns the maze as instance of boolean[][] instead of Maze. You probably intended to have the array as a field of the class instead and not access the array directly but via a maze instance.

Next, the walk method is non-static and part of Walker instances. So you would need to create an instance of that class and call the method on that instance.


Maze generation

You probably intended to do this instead:

JavaScript

with a call like

JavaScript

Constructor

Or even better, use a constructor:

JavaScript

And call it like this in your main:

JavaScript

Factory

You can still couple that with a factory method, if you really want. But the creation logic should be in a (possibly private) constructor nonetheless:

JavaScript

calling it like:

JavaScript

Walker

Now, you need an instance of the Walker class and call the method on that, giving it the maze you just generated:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement