Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I associate an Enum with its opposite value, as in cardinal directions (North - South, East - West, etc)?

Tags:

java

enums

I'm still working on my Cell class for my maze game I'm attempting to make. After help in a different thread it was suggested that I use an EnumMap for my Walls/Neighbors and this is working great so far.

Here is what I have thus far:

enum Dir {
    NORTH, SOUTH, EAST, WEST
}

class Cell {
    public Map<Dir, Cell> neighbors = Collections
            .synchronizedMap(new EnumMap<Dir, Cell>(Dir.class));
    public Map<Dir, Boolean> walls = Collections
            .synchronizedMap(new EnumMap<Dir, Boolean>(Dir.class));

    public boolean Visited;

    public Cell() {
        Visited = false;
        for (Dir direction : Dir.values()) {
            walls.put(direction, true);
        }
    }

    // Randomly select an unvisited neighbor and tear down the walls
    // between this cell and that neighbor.
    public Cell removeRandomWall() {
        List<Dir> unvisitedDirections = new ArrayList<Dir>();
        for (Dir direction : neighbors.keySet()) {
            if (!neighbors.get(direction).Visited)
                unvisitedDirections.add(direction);
        }

        Random randGen = new Random();
        Dir randDir = unvisitedDirections.get(randGen
                .nextInt(unvisitedDirections.size()));
        Cell randomNeighbor = neighbors.get(randDir);

        // Tear down wall in this cell
        walls.put(randDir, false);
        // Tear down opposite wall in neighbor cell
        randomNeighbor.walls.put(randDir, false); // <--- instead of randDir, it needs to be it's opposite.

        return randomNeighbor;
    }
}

If you look at that last comment there, I first tear down say the NORTH wall in my current cell. I then take my North neighbor, and now I must tear down my SOUTH wall, so the walls between the two cells have been removed.

What would be a simple way to extend my enum so I can give it a direction and it return to me it's opposite?

like image 354
Scorcher84 Avatar asked Oct 15 '09 21:10

Scorcher84


People also ask

How do you equate two enums?

There are two ways for making comparison of enum members :By using == operator. By using equals() method.

How do you assign values to enum constants?

Custom values to the constants You cannot create an object of an enum explicitly so, you need to add a parameterized constructor to initialize the value(s). The initialization should be done only once. Therefore, the constructor must be declared private or default.

Can I use enum as map key?

EnumMap is a specialized map implementation that uses only Enum type key. In HashMap, we can use Enum as well as any other object as a key.


1 Answers

yet another way without switch/case, or having to store state:

public enum Dir {
   NORTH { @Override public Dir opposite() { return SOUTH; }},
   EAST  { @Override public Dir opposite() { return WEST;  }},
   SOUTH { @Override public Dir opposite() { return NORTH; }},
   WEST  { @Override public Dir opposite() { return EAST;  }},
   ;

   abstract public Dir opposite();
}
like image 171
Jason S Avatar answered Oct 04 '22 01:10

Jason S