Enum of directions with opposites
Clash 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 !
java enums
add a comment |Â
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 !
java enums
add a comment |Â
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 !
java enums
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
java enums
asked 55 mins ago
Rémy.W
685
685
add a comment |Â
add a comment |Â
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;
2
Opposite of North is South :)
– Nicholas K
41 mins ago
add a comment |Â
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.
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
add a comment |Â
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;
If you havereturn
thenbreak
is duplicate
– lucumt
44 mins ago
Why would you switch on a String (the name) when you can just switch onthis
and do it in a safe manner?
– Erwin Bolwidt
43 mins ago
add a comment |Â
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;
2
Opposite of North is South :)
– Nicholas K
41 mins ago
add a comment |Â
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;
2
Opposite of North is South :)
– Nicholas K
41 mins ago
add a comment |Â
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;
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;
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
add a comment |Â
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
add a comment |Â
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.
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
add a comment |Â
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.
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
add a comment |Â
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.
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.
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
add a comment |Â
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
add a comment |Â
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;
If you havereturn
thenbreak
is duplicate
– lucumt
44 mins ago
Why would you switch on a String (the name) when you can just switch onthis
and do it in a safe manner?
– Erwin Bolwidt
43 mins ago
add a comment |Â
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;
If you havereturn
thenbreak
is duplicate
– lucumt
44 mins ago
Why would you switch on a String (the name) when you can just switch onthis
and do it in a safe manner?
– Erwin Bolwidt
43 mins ago
add a comment |Â
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;
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;
edited 44 mins ago
answered 45 mins ago
Susmit Agrawal
882411
882411
If you havereturn
thenbreak
is duplicate
– lucumt
44 mins ago
Why would you switch on a String (the name) when you can just switch onthis
and do it in a safe manner?
– Erwin Bolwidt
43 mins ago
add a comment |Â
If you havereturn
thenbreak
is duplicate
– lucumt
44 mins ago
Why would you switch on a String (the name) when you can just switch onthis
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password