How to implement connected rooms?

后端 未结 7 1220
难免孤独
难免孤独 2021-02-04 22:27

This may be a duplicate question as I don\'t know to phrase the search query. I\'m creating a Zork-like text based game in Java where the character moves to different rooms whic

7条回答
  •  情书的邮戳
    2021-02-04 23:07

    You could implement this using an array:

    class Room {
        private Room[] exits = new Room[4];
    }
    

    In this example, exits[0] could contain a reference to the room to the north, exits[1] the room to the east, and so on. If an element contains null then that might represent no exit in that direction.

    Note that you can create nonlinear data structures using this method, such as A -> B -> C -> A.

提交回复
热议问题