I am working on a puzzle game with a 6 by 6 grid of storage tanks that are connected to up to 4 tanks adjacent to them via pipes. The gameplay is concerned with combining what's in a tank with an adjacent tank via the pipe than interconnects them.

Right now I store the tanks in a 6x6 array, vertical pipes in a 5x6 array, and horizontal pipes in a 6x5 array. Each tank has a reference to the object that contains both tanks and pipes and when a pipe needs to be animated filling with liquid, the selected tank just calls a method on the container object telling it to animate the pipes it is connected to (subtract 1 from row or column to find connected pipes).

This feels like the wrong way of doing it, as I've also considered just giving each tank references to the pipes connected to it to access directly.

  • \$\begingroup\$ A picture speaks a thousand words. If you can provide a screenshot of the type of thing you're after, I think it would greatly help this question getting some more answers. \$\endgroup\$ – Ray Dey Nov 20 '11 at 15:48

You can easily replace your array architecture with an object-based approach where tanks have pointers to there adjacent pipes and the pipes know about their start and end object.

What you're doing here is modelling a graph. The tanks are the graphs nodes (vertices) and the pipes are the edges.

Of course you can also model a graph using an array. Usually you would use an adjacency matrix for that.

The appropriate structure really depends on your architecture and how you're accessing the data and what you're planning to do with it. The object based approach is closer to reality and might be easier to work with.

  • \$\begingroup\$ Giving pipes references to tanks and tanks references to pipes would be circular. Couldn't that cause problems later on? \$\endgroup\$ – mars Nov 20 '11 at 7:16
  • \$\begingroup\$ Circular references aren't a problem if you know about them and code your structures in a way that handles/frees these references correctly. I don't know what language you use to write your game, but there are existing graph implementations for all mayor programming-languages if you don't want to write this yourself. \$\endgroup\$ – bummzack Nov 20 '11 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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