3

I'm creating a 2d tile based sim game. I have a 2d array of gridSquares, which are accessed and changed from many different classes and methods. Should I pass the 2d array of gridSquares each time, or make it a global? Which is best practice?

I was thinking, would it be an option to create a class which just contains a set of variables which all classes could extend? Is that a good or bad idea / not good practice?

I'm still fairly new to java so I'm still learning lots!

Thanks in advance.

Rel

1
  • The answer + using it as a singleton! Apr 28, 2009 at 16:38

5 Answers 5

7

You should not be designing in terms of a data structure. Java's an object-oriented language. Try thinking about your problem as objects interacting. It's not a 2D array; it's a Board object. Build the behavior for manipulating its state into the problem and hide the fact that you happen to have chosen a 2D array.

I don't have all the details of a Board worked out, but it would start like this:

public class Board
{
    // This is what you're passing around now; Board hides it.
    // Square is the abstraction of a position on the Board
    private Square[][] grid;

    public Board(int nRows, int nCols)        
    {
       this.grid = new Square[nRows][];
       for (int i = 0; i < this.grid[i].length; ++i)
       {
           this.grid[i] = new Square[nCols];
       }
    }

    // Now add methods for adding Pieces to the Board, rules for moving them, etc.
} 
7
  • interesting. I have already created a class called GirdSquare for each of the squares. have a 2d array of GirdSquare's allows me to interact with the path finding algorithms with much more ease. Even if I had a Board object, I still need to access individual grid squares, and the best way I thought of doing this was using a 2d array, like grid references. Apr 25, 2009 at 13:41
  • Maybe, but a Board abstraction would hide the fact completely. For example, if I wrote a computer representation for Monopoly it might be better represented as a LinkedList of Monopoly properties, because you always traverse the board in that way. A Board abstraction would still be useful in that case; your 2D grid would not.
    – duffymo
    Apr 25, 2009 at 14:26
  • I am unsure what you mean by this. I am new to the idea of abstractions and interface classes! Maybe you could give me a bit more detail? What would a Board abstraction look like and how would I use it? Apr 25, 2009 at 22:49
  • Thanks for the example! Made it much more clear! This way I could also devise ways of keeping the way the grids are accessed for each level, but just have a change level method. :) Apr 27, 2009 at 10:15
  • As I understand, I would need to make this a singleton class. I'm researching atm, but if anyone finds an example of how to do such a thing and use singleton classes, please let me know. Apr 27, 2009 at 14:33
4

Pass them in constructors and hold them in member variables.

If you access them from too many places you probably have a design problem.

4
  • may I ask what you mean by "member variables"? Sorry I'm still rather new to java and programming. Apr 25, 2009 at 12:14
  • These are the per-object variables. Sometimes they are also called fields.
    – starblue
    Apr 25, 2009 at 13:14
  • Sorry I think I'm being really dim here. Could you give me an example please? Apr 25, 2009 at 13:36
  • oh sorry, I believe I understand now. like int or char. as opposed to objects Integer or String. Apr 25, 2009 at 13:45
2

If your certain your only going to ever have one '2d array of gridSquares' you could always use the singleton pattern, essentailly making it global.

There are arguements for and against this though, you may find one day you want to be able to preload maps, but the singleton gets in the way (cant create a second instance of a singleton).

5
  • hum. i may eventually extend it to have multiple floors. I guess that way would not be best for this situation Apr 25, 2009 at 13:57
  • Even if you have multiple floors (essentially going from 2d to 3d), there is still only one global game state. Hence a singleton would still work.
    – KarstenF
    Apr 26, 2009 at 0:00
  • Indeed. I will have to read up on singletons and how to use them. I have not dealt with them before or been taught. Thanks again Apr 27, 2009 at 10:16
  • can anyone point me to a good singleton pattern tutorial please? Apr 27, 2009 at 10:46
  • Google would discourage you for creating singletons: code.google.com/p/google-singleton-detector
    – duffymo
    Jul 21, 2011 at 13:25
1

If you absolutely need to, in your grid-containing class (A) declare the grid as public and then import the A class statically (import static A;). This will enable you to interact with the grid without extending the A class, at least.

Your code should not be accessing the grid from too many places. Consider re-factoring your code to avoid having to manipulate the grid from all over the place. Separate your concerns. And it's definitely not a good idea to use inheritance, like you mentioned.

1

What I would recommend it to make it static but create one definite class to read the data from it, say GridSquaresAcessor. In this class you write all the methods to access the array. Even better, make it a private static field of this class to avoid any other code to manipulate in a way not defined in this class.

In every class you need access to the array you can pass one GridSquaresAcessor as a parameter on the constructor and keep it stored in a local variable.

I personally don't like singletons because they make it very hard to test code...

If your code is multithreaded, be sure to make the 2D-array synchronized.

1
  • That was sort of what my idea was, except you worded it better. Anyone else think this is a good way of doing it? Apr 25, 2009 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.