如何将枚举与其相反的价值联系起来,如主要方向(南北,东西等)? [英] How can I associate an Enum with its opposite value, as in cardinal directions (North - South, East - West, etc)?

查看:114
本文介绍了如何将枚举与其相反的价值联系起来,如主要方向(南北,东西等)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我想要制作的迷宫游戏的Cell课程工作。在不同的线程中的帮助之后,建议我为我的墙壁/邻居使用EnumMap,这是非常有用的。

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.

这是我到目前为止

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

如果你看看最后一个评论,我先撕下我现在的细胞里的北墙。然后我带我的北邻,现在我必须拆掉我的SOUTH墙,所以两个牢房之间的墙壁已经被去除了。

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?

推荐答案

最简单,我想,只是给它添加一个方法。请注意,如果枚举常量的数量不会随时间而变化,这只能很好地运行。

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

这篇关于如何将枚举与其相反的价值联系起来,如主要方向(南北,东西等)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆