简体   繁体   English

如何将Enum与其相反的值相关联,如在主要方向(南北,东西等)?

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

I'm still working on my Cell class for my maze game I'm attempting to make. 我还在为我的迷宫游戏制作我的Cell课程。 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. 在另一个线程的帮助之后,有人建议我使用EnumMap作为我的墙/邻居,这到目前为止工作得很好。

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? 什么是一个简单的方法来扩展我的枚举,所以我可以给它一个方向,它回到我的对面?

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();
}

Simplest, I think, is just to add a method to it. 我认为,最简单的方法就是添加一个方法。 Note that this only works well if the number of enum constants won't change over time. 请注意,这仅在枚举常量的数量不会随时间变化时才有效。

enum Dir {
    NORTH,
    SOUTH,
    EAST,
    WEST;

    public Dir opposite() {
        switch(this) {
            case NORTH: return Dir.SOUTH;
            case SOUTH: return Dir.NORTH;
            case EAST: return Dir.WEST;
            case WEST: return Dir.EAST;
            default: throw new IllegalStateException("This should never happen: " + this + " has no opposite.");
        }
    }
}

Then, in your code, you could do this: 然后,在您的代码中,您可以这样做:

randomNeighbor.walls.put(randDir.opposite(), false);

You can try this out, the "good" thing about this is that you can actually index the enums like an array, kinda... and since we are dealing with opposites as in there are always pairs, the code supports it as long as you add the new values to the end so you don't mess up the indexing. 你可以尝试这个,关于这个的“好”的事情是,你可以实际上像数组一样索引枚举,有点......因为我们正在处理对立面,因为总是有对,代码支持它只要您将新值添加到最后,这样您就不会搞砸索引。

enum Directions {

    NORTH(1), 
    SOUTH(0), 
    WEST(3), 
    EAST(2);

    private final int opposite;

    Directions(int opposite) {
        this.opposite = opposite;
    }

    Directions opposite() {
        return Directions.values()[this.opposite];
    }

}

For example if you wanted to add more directions you would just add this: 例如,如果您想添加更多路线,您只需添加以下内容:

NORTH_WEST(7),
NORTH_EAST(6),
SOUTH_WEST(5),
SOUTH_EAST(4)

Easy. 简单。 :) :)

public enum Direction
{
    NORTH,EAST,SOUTH,WEST;

    public Direction opposite()
    {
        return Direction.values()[ (this.ordinal() + 2) & 3 ];
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 java 的 GUI 中,除了 .north、.south、.east、.west、.center 之外,我还能使用什么? - What can I use aside from .north, .south, .east, .west, .center in a GUI in java? java:将char [] []数组中的元素向北,向南,向东,向西等方向移动一个空格。 - java: Move elements in a char[][] array by one space either north, south, east, west, etc. 我如何按顺序简化一系列方向{“南”、“东”}等? - How do i simplify an array of directions {“south”, “east”} etc etc, in order? 如果int为0表示北,1表示东方等...如何反转方向? - If an int is 0 for north, 1 for east, etc… How do I reverse direction? Android-Google Maps V2-计算当前不在视野中的标记是屏幕的北,东,南还是西 - Android - Google maps V2 - Calculate whether a marker currently not in view is North, East, South or West of screen 如何将 JPanel 从应用程序的东侧移动到西侧? - How do I move a JPanel from the East side of an application to the West? 如何获得BorderLayout的西方和东方? - How to get the west and east of a BorderLayout attached? 如何在同一JPanel中实现南北节 - How to implement North and South sections in same JPanel 我有什么办法可以使BorderLayout的东,南和西边界变大? - Is there any way for me to make BorderLayout's east, south and west border bigger? 如何阻止BorderLayout的中心向东和向西移动? - How do I stop center of BorderLayout from taking up east and west?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM