I’m making a text-based game and I would like my hero to be able to move within a 3×3 grid. I tried doing this with a two-dimensional array fairly easily. The problem that I encountered was that I wanted to call a method for each grid the hero was in. So if the hero was in grid 0,0 then I would call the atHouse() method, if he went to 0,1 I call the atTree() method. Each of those methods would give a description of that area, items in it, etc…
So, since I wasn’t able to store methods in an array I was thinking of possibly doing another way. I just don’t know what way might be the best. Has anyone setup a world in a quality way that is better than what I described?
The simplest solution is to create a factory method:
I’d suggest a 2d array with names for each position:
Then, you’d pass the position into your factory method which would grab the function name for to use in a switch. Obviously, this solution is not the best, as you’ll have to explicitly define each method call.