Enum of directions with opposites

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite












I'm currently working on a Dungeon game project which is composed of multiples rooms connected to each other.
Depending on the room you're in you can go to rooms next to yours by picking directions (North, South, East, West)
I've represented the directions with an Enum class and I'm looking for a way to implements that North is the opposite of South, East for West (and so on ...) for the sake of creating the layout of all the rooms in the final form of the game



Here is my Enum class :



public enum Direction 
NORTH,
EAST,
SOUTH,
WEST;

public Direction getOppositeDirection()
return Direction.values()[(this.ordinal()+2)%4];





The problem is that currently the method is not very safe, any change in the order of declarations in the direction will break the method, any suggestions on how to do this in a better way ? Thanks !









share

























    up vote
    6
    down vote

    favorite












    I'm currently working on a Dungeon game project which is composed of multiples rooms connected to each other.
    Depending on the room you're in you can go to rooms next to yours by picking directions (North, South, East, West)
    I've represented the directions with an Enum class and I'm looking for a way to implements that North is the opposite of South, East for West (and so on ...) for the sake of creating the layout of all the rooms in the final form of the game



    Here is my Enum class :



    public enum Direction 
    NORTH,
    EAST,
    SOUTH,
    WEST;

    public Direction getOppositeDirection()
    return Direction.values()[(this.ordinal()+2)%4];





    The problem is that currently the method is not very safe, any change in the order of declarations in the direction will break the method, any suggestions on how to do this in a better way ? Thanks !









    share























      up vote
      6
      down vote

      favorite









      up vote
      6
      down vote

      favorite











      I'm currently working on a Dungeon game project which is composed of multiples rooms connected to each other.
      Depending on the room you're in you can go to rooms next to yours by picking directions (North, South, East, West)
      I've represented the directions with an Enum class and I'm looking for a way to implements that North is the opposite of South, East for West (and so on ...) for the sake of creating the layout of all the rooms in the final form of the game



      Here is my Enum class :



      public enum Direction 
      NORTH,
      EAST,
      SOUTH,
      WEST;

      public Direction getOppositeDirection()
      return Direction.values()[(this.ordinal()+2)%4];





      The problem is that currently the method is not very safe, any change in the order of declarations in the direction will break the method, any suggestions on how to do this in a better way ? Thanks !









      share













      I'm currently working on a Dungeon game project which is composed of multiples rooms connected to each other.
      Depending on the room you're in you can go to rooms next to yours by picking directions (North, South, East, West)
      I've represented the directions with an Enum class and I'm looking for a way to implements that North is the opposite of South, East for West (and so on ...) for the sake of creating the layout of all the rooms in the final form of the game



      Here is my Enum class :



      public enum Direction 
      NORTH,
      EAST,
      SOUTH,
      WEST;

      public Direction getOppositeDirection()
      return Direction.values()[(this.ordinal()+2)%4];





      The problem is that currently the method is not very safe, any change in the order of declarations in the direction will break the method, any suggestions on how to do this in a better way ? Thanks !







      java enums





      share












      share










      share



      share










      asked 55 mins ago









      Rémy.W

      685




      685






















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          6
          down vote



          accepted










          You could make the method abstract, and implement it for each constant.



          enum Direction 
          NORTH()
          public Direction getOppositeDirection()
          return SOUTH;

          ,
          EAST()
          public Direction getOppositeDirection()
          return WEST;

          ,
          SOUTH()
          public Direction getOppositeDirection()
          return NORTH;

          ,
          WEST()
          public Direction getOppositeDirection()
          return EAST;

          ;

          public abstract Direction getOppositeDirection();



          It's a bit wordy, though.



          The next method is shorter but more difficult to read.



          enum Direction 
          NORTH, EAST, SOUTH, WEST;

          static
          (SOUTH.oppositeDirection = NORTH).oppositeDirection = SOUTH;
          (WEST.oppositeDirection = EAST).oppositeDirection = WEST;


          private Direction oppositeDirection;

          public Direction getOppositeDirection()
          return oppositeDirection;







          share|improve this answer


















          • 2




            Opposite of North is South :)
            – Nicholas K
            41 mins ago

















          up vote
          2
          down vote













          You may also want to look in lambda:



          public enum Direction 
          NORTH(() -> SOUTH),
          EAST(() -> WEST),
          SOUTH(() -> NORTH),
          WEST(() -> EAST);

          private final Supplier<Direction> direction;

          private Direction(Supplier<Direction> direction)
          this.direction = direction;


          public final Direction getOppositeDirection()
          return direction.get();




          This does the same than having abstract method, but is more concise: you read directly what is the opposite direction instead of having the verbose method declaration (it is another matter if you need to also add other methods).



          Also, it will not create pseudo class for each enums constant (eg: Direction$1, Direction$2, ...) because there is no need to.






          share|improve this answer




















          • I thought about it, but it shouldn't compile because there are 2 illegal forward references here.
            – Andrew Tobilko
            26 mins ago










          • There are no forward reference: it would make an error if I was passing the field directly in the constructor, but that is not the case here. The lambda is not evaluated before getOppositeDirection get called.
            – NoDataFound
            2 mins ago

















          up vote
          1
          down vote













          Since you have only 4 values, I feel it would be better to hard-code the opposites:



          public Direction getOppositeDirection() 
          switch(this.name())
          case "NORTH":
          return SOUTH;
          case "SOUTH":
          return NORTH;
          case "EAST":
          return WEST;
          case "WEST":
          return EAST;







          share|improve this answer






















          • If you have return then break is duplicate
            – lucumt
            44 mins ago










          • Why would you switch on a String (the name) when you can just switch on this and do it in a safe manner?
            – Erwin Bolwidt
            43 mins ago










          Your Answer





          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52687763%2fenum-of-directions-with-opposites%23new-answer', 'question_page');

          );

          Post as a guest






























          3 Answers
          3






          active

          oldest

          votes








          3 Answers
          3






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          6
          down vote



          accepted










          You could make the method abstract, and implement it for each constant.



          enum Direction 
          NORTH()
          public Direction getOppositeDirection()
          return SOUTH;

          ,
          EAST()
          public Direction getOppositeDirection()
          return WEST;

          ,
          SOUTH()
          public Direction getOppositeDirection()
          return NORTH;

          ,
          WEST()
          public Direction getOppositeDirection()
          return EAST;

          ;

          public abstract Direction getOppositeDirection();



          It's a bit wordy, though.



          The next method is shorter but more difficult to read.



          enum Direction 
          NORTH, EAST, SOUTH, WEST;

          static
          (SOUTH.oppositeDirection = NORTH).oppositeDirection = SOUTH;
          (WEST.oppositeDirection = EAST).oppositeDirection = WEST;


          private Direction oppositeDirection;

          public Direction getOppositeDirection()
          return oppositeDirection;







          share|improve this answer


















          • 2




            Opposite of North is South :)
            – Nicholas K
            41 mins ago














          up vote
          6
          down vote



          accepted










          You could make the method abstract, and implement it for each constant.



          enum Direction 
          NORTH()
          public Direction getOppositeDirection()
          return SOUTH;

          ,
          EAST()
          public Direction getOppositeDirection()
          return WEST;

          ,
          SOUTH()
          public Direction getOppositeDirection()
          return NORTH;

          ,
          WEST()
          public Direction getOppositeDirection()
          return EAST;

          ;

          public abstract Direction getOppositeDirection();



          It's a bit wordy, though.



          The next method is shorter but more difficult to read.



          enum Direction 
          NORTH, EAST, SOUTH, WEST;

          static
          (SOUTH.oppositeDirection = NORTH).oppositeDirection = SOUTH;
          (WEST.oppositeDirection = EAST).oppositeDirection = WEST;


          private Direction oppositeDirection;

          public Direction getOppositeDirection()
          return oppositeDirection;







          share|improve this answer


















          • 2




            Opposite of North is South :)
            – Nicholas K
            41 mins ago












          up vote
          6
          down vote



          accepted







          up vote
          6
          down vote



          accepted






          You could make the method abstract, and implement it for each constant.



          enum Direction 
          NORTH()
          public Direction getOppositeDirection()
          return SOUTH;

          ,
          EAST()
          public Direction getOppositeDirection()
          return WEST;

          ,
          SOUTH()
          public Direction getOppositeDirection()
          return NORTH;

          ,
          WEST()
          public Direction getOppositeDirection()
          return EAST;

          ;

          public abstract Direction getOppositeDirection();



          It's a bit wordy, though.



          The next method is shorter but more difficult to read.



          enum Direction 
          NORTH, EAST, SOUTH, WEST;

          static
          (SOUTH.oppositeDirection = NORTH).oppositeDirection = SOUTH;
          (WEST.oppositeDirection = EAST).oppositeDirection = WEST;


          private Direction oppositeDirection;

          public Direction getOppositeDirection()
          return oppositeDirection;







          share|improve this answer














          You could make the method abstract, and implement it for each constant.



          enum Direction 
          NORTH()
          public Direction getOppositeDirection()
          return SOUTH;

          ,
          EAST()
          public Direction getOppositeDirection()
          return WEST;

          ,
          SOUTH()
          public Direction getOppositeDirection()
          return NORTH;

          ,
          WEST()
          public Direction getOppositeDirection()
          return EAST;

          ;

          public abstract Direction getOppositeDirection();



          It's a bit wordy, though.



          The next method is shorter but more difficult to read.



          enum Direction 
          NORTH, EAST, SOUTH, WEST;

          static
          (SOUTH.oppositeDirection = NORTH).oppositeDirection = SOUTH;
          (WEST.oppositeDirection = EAST).oppositeDirection = WEST;


          private Direction oppositeDirection;

          public Direction getOppositeDirection()
          return oppositeDirection;








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 13 mins ago

























          answered 47 mins ago









          Andrew Tobilko

          22.4k83876




          22.4k83876







          • 2




            Opposite of North is South :)
            – Nicholas K
            41 mins ago












          • 2




            Opposite of North is South :)
            – Nicholas K
            41 mins ago







          2




          2




          Opposite of North is South :)
          – Nicholas K
          41 mins ago




          Opposite of North is South :)
          – Nicholas K
          41 mins ago












          up vote
          2
          down vote













          You may also want to look in lambda:



          public enum Direction 
          NORTH(() -> SOUTH),
          EAST(() -> WEST),
          SOUTH(() -> NORTH),
          WEST(() -> EAST);

          private final Supplier<Direction> direction;

          private Direction(Supplier<Direction> direction)
          this.direction = direction;


          public final Direction getOppositeDirection()
          return direction.get();




          This does the same than having abstract method, but is more concise: you read directly what is the opposite direction instead of having the verbose method declaration (it is another matter if you need to also add other methods).



          Also, it will not create pseudo class for each enums constant (eg: Direction$1, Direction$2, ...) because there is no need to.






          share|improve this answer




















          • I thought about it, but it shouldn't compile because there are 2 illegal forward references here.
            – Andrew Tobilko
            26 mins ago










          • There are no forward reference: it would make an error if I was passing the field directly in the constructor, but that is not the case here. The lambda is not evaluated before getOppositeDirection get called.
            – NoDataFound
            2 mins ago














          up vote
          2
          down vote













          You may also want to look in lambda:



          public enum Direction 
          NORTH(() -> SOUTH),
          EAST(() -> WEST),
          SOUTH(() -> NORTH),
          WEST(() -> EAST);

          private final Supplier<Direction> direction;

          private Direction(Supplier<Direction> direction)
          this.direction = direction;


          public final Direction getOppositeDirection()
          return direction.get();




          This does the same than having abstract method, but is more concise: you read directly what is the opposite direction instead of having the verbose method declaration (it is another matter if you need to also add other methods).



          Also, it will not create pseudo class for each enums constant (eg: Direction$1, Direction$2, ...) because there is no need to.






          share|improve this answer




















          • I thought about it, but it shouldn't compile because there are 2 illegal forward references here.
            – Andrew Tobilko
            26 mins ago










          • There are no forward reference: it would make an error if I was passing the field directly in the constructor, but that is not the case here. The lambda is not evaluated before getOppositeDirection get called.
            – NoDataFound
            2 mins ago












          up vote
          2
          down vote










          up vote
          2
          down vote









          You may also want to look in lambda:



          public enum Direction 
          NORTH(() -> SOUTH),
          EAST(() -> WEST),
          SOUTH(() -> NORTH),
          WEST(() -> EAST);

          private final Supplier<Direction> direction;

          private Direction(Supplier<Direction> direction)
          this.direction = direction;


          public final Direction getOppositeDirection()
          return direction.get();




          This does the same than having abstract method, but is more concise: you read directly what is the opposite direction instead of having the verbose method declaration (it is another matter if you need to also add other methods).



          Also, it will not create pseudo class for each enums constant (eg: Direction$1, Direction$2, ...) because there is no need to.






          share|improve this answer












          You may also want to look in lambda:



          public enum Direction 
          NORTH(() -> SOUTH),
          EAST(() -> WEST),
          SOUTH(() -> NORTH),
          WEST(() -> EAST);

          private final Supplier<Direction> direction;

          private Direction(Supplier<Direction> direction)
          this.direction = direction;


          public final Direction getOppositeDirection()
          return direction.get();




          This does the same than having abstract method, but is more concise: you read directly what is the opposite direction instead of having the verbose method declaration (it is another matter if you need to also add other methods).



          Also, it will not create pseudo class for each enums constant (eg: Direction$1, Direction$2, ...) because there is no need to.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 27 mins ago









          NoDataFound

          5,1311539




          5,1311539











          • I thought about it, but it shouldn't compile because there are 2 illegal forward references here.
            – Andrew Tobilko
            26 mins ago










          • There are no forward reference: it would make an error if I was passing the field directly in the constructor, but that is not the case here. The lambda is not evaluated before getOppositeDirection get called.
            – NoDataFound
            2 mins ago
















          • I thought about it, but it shouldn't compile because there are 2 illegal forward references here.
            – Andrew Tobilko
            26 mins ago










          • There are no forward reference: it would make an error if I was passing the field directly in the constructor, but that is not the case here. The lambda is not evaluated before getOppositeDirection get called.
            – NoDataFound
            2 mins ago















          I thought about it, but it shouldn't compile because there are 2 illegal forward references here.
          – Andrew Tobilko
          26 mins ago




          I thought about it, but it shouldn't compile because there are 2 illegal forward references here.
          – Andrew Tobilko
          26 mins ago












          There are no forward reference: it would make an error if I was passing the field directly in the constructor, but that is not the case here. The lambda is not evaluated before getOppositeDirection get called.
          – NoDataFound
          2 mins ago




          There are no forward reference: it would make an error if I was passing the field directly in the constructor, but that is not the case here. The lambda is not evaluated before getOppositeDirection get called.
          – NoDataFound
          2 mins ago










          up vote
          1
          down vote













          Since you have only 4 values, I feel it would be better to hard-code the opposites:



          public Direction getOppositeDirection() 
          switch(this.name())
          case "NORTH":
          return SOUTH;
          case "SOUTH":
          return NORTH;
          case "EAST":
          return WEST;
          case "WEST":
          return EAST;







          share|improve this answer






















          • If you have return then break is duplicate
            – lucumt
            44 mins ago










          • Why would you switch on a String (the name) when you can just switch on this and do it in a safe manner?
            – Erwin Bolwidt
            43 mins ago














          up vote
          1
          down vote













          Since you have only 4 values, I feel it would be better to hard-code the opposites:



          public Direction getOppositeDirection() 
          switch(this.name())
          case "NORTH":
          return SOUTH;
          case "SOUTH":
          return NORTH;
          case "EAST":
          return WEST;
          case "WEST":
          return EAST;







          share|improve this answer






















          • If you have return then break is duplicate
            – lucumt
            44 mins ago










          • Why would you switch on a String (the name) when you can just switch on this and do it in a safe manner?
            – Erwin Bolwidt
            43 mins ago












          up vote
          1
          down vote










          up vote
          1
          down vote









          Since you have only 4 values, I feel it would be better to hard-code the opposites:



          public Direction getOppositeDirection() 
          switch(this.name())
          case "NORTH":
          return SOUTH;
          case "SOUTH":
          return NORTH;
          case "EAST":
          return WEST;
          case "WEST":
          return EAST;







          share|improve this answer














          Since you have only 4 values, I feel it would be better to hard-code the opposites:



          public Direction getOppositeDirection() 
          switch(this.name())
          case "NORTH":
          return SOUTH;
          case "SOUTH":
          return NORTH;
          case "EAST":
          return WEST;
          case "WEST":
          return EAST;








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 44 mins ago

























          answered 45 mins ago









          Susmit Agrawal

          882411




          882411











          • If you have return then break is duplicate
            – lucumt
            44 mins ago










          • Why would you switch on a String (the name) when you can just switch on this and do it in a safe manner?
            – Erwin Bolwidt
            43 mins ago
















          • If you have return then break is duplicate
            – lucumt
            44 mins ago










          • Why would you switch on a String (the name) when you can just switch on this and do it in a safe manner?
            – Erwin Bolwidt
            43 mins ago















          If you have return then break is duplicate
          – lucumt
          44 mins ago




          If you have return then break is duplicate
          – lucumt
          44 mins ago












          Why would you switch on a String (the name) when you can just switch on this and do it in a safe manner?
          – Erwin Bolwidt
          43 mins ago




          Why would you switch on a String (the name) when you can just switch on this and do it in a safe manner?
          – Erwin Bolwidt
          43 mins ago

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52687763%2fenum-of-directions-with-opposites%23new-answer', 'question_page');

          );

          Post as a guest













































































          Comments

          Popular posts from this blog

          What does second last employer means? [closed]

          List of Gilmore Girls characters

          One-line joke