1
\$\begingroup\$

In my Java game, I've got a number of randomly-generated maps made of tiles. I'm struggling with writing the code to handle the entering/exiting of the maps.

Originally, I only had Ascend and Descend. Easy. Enter exit, arrive at entrance. Basically, I used depth-- and depth++. Start at level 1, descend to level 2. Ascend to level 1 if desired.

Now I have maps that have North, East, South, and West entrances. So when I exit North, I need to arrive at the South entrance. Similarly, when I enter East, I need to arrive at the West entrance.

I'm working on an elaboration of the Ascend/Descend --/++ system. This basically means I have to have code for:

eastEntrance sends player to westExit,

westEntrance sends player to eastExit,

northEntrance sends player to southExit,

southEntrance sends player to northExit.

westExit sends players to eastEntrance (etc for east, south, north).

This seems pretty cumbersome. What is a good way to link my maps?

I'd really appreciate any input on this.

\$\endgroup\$
4
  • \$\begingroup\$ I was about to retitle this to How should I structure Java code that links “rooms” in a map through “doors”?, but thought I'd ask first after all. I know you said maps, but would “rooms” and “doors” be a useful analogy? \$\endgroup\$
    – Anko
    Aug 13 '15 at 23:10
  • \$\begingroup\$ Yes. I was just about to add an edit that what I really want to create are two way doors. I didn't realize at first that this is actually what I want to do- probably my own conceptual barrier from the rooms being "outside". Also, now that I look for the room-connectivity problem I see lots of examples. Oops. \$\endgroup\$
    – Jake
    Aug 13 '15 at 23:30
  • \$\begingroup\$ It might be helpful to draw some examples. (It always helps me anyway.) \$\endgroup\$
    – Anko
    Aug 13 '15 at 23:45
  • \$\begingroup\$ It definitely helped me. Phoenix suggested below that I assign an ID to each door- it was the missing piece in making sense of my own diagram. \$\endgroup\$
    – Jake
    Aug 13 '15 at 23:48
2
\$\begingroup\$

A simple solution would be to assign the doors IDs, where the matching pairs would share an ID, and have them send the player to the other door with the same ID.

Or, if for whatever reason you find the need to make such IDs unique, you could assign door pairs matching IDs where one is negative and one is positive but the same absolute value, and when teleporting get the door's ID and multiply it by -1 to get the ID of the destination door.

\$\endgroup\$
3
  • \$\begingroup\$ I like this solution- would it be more useful for me to think of the problem as one of Door1, Door2, Door3, Door 4? I'm pretty caught up in thinking of the maps as single entities and I think it might be holding me back. \$\endgroup\$
    – Jake
    Aug 13 '15 at 23:39
  • \$\begingroup\$ I think viewing each door as an entity linked to another and viewing each pair of doors as a single entity are equally valid. Viewing each pair as its own entity would make it easier to design maps, but viewing each door as its own entity would allow for easier modification if you find the desire to, say, create a door without pairing it, or have one door paired to different doors depending on conditions. \$\endgroup\$
    – Phoenix
    Aug 13 '15 at 23:44
  • \$\begingroup\$ I've got to change how I access my maps, but it's for the best. I knew there had to be a simple solution like ID'ing the doors- thank you very much for the help. \$\endgroup\$
    – Jake
    Aug 13 '15 at 23:57

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.