I\'m programming a game in java which is made up of a grid of tiles. I wan\'t to be able to inuitively define the edges of the tiles and how they relate to each other, e.g.
You could use an internal Map instead to define these associations. This works if at the point of initializing the Map, you already have all enum values created:
public enum Edge {
TOP,
BOTTOM,
LEFT,
RIGHT;
private static final Map opposites =
new EnumMap(Edge.class);
static {
opposites.put(TOP, BOTTOM);
opposites.put(BOTTOM, TOP);
opposites.put(LEFT, RIGHT);
opposites.put(RIGHT, LEFT);
}
public Edge opposite(){
return opposites.get(this);
}
}