lluvia900125 2023-04-04 12:38 采纳率: 45%
浏览 18
已结题

java class method 代码具体实现

我正在做cs61B的一个project,需要生成一个2D world,看到这篇文章,原文链接:https://blog.csdn.net/lindaicoding/article/details/119202956
里面有一个Directions类,direction包括上下左右,左上左下右上右下8个方向,我不知道这个Directions.getDirections().values()是怎么回事,我想知道这个getDirections() 和values的代码。


for(Point point : FloorPoints) {
            for(Tuple direction : Directions.getDirections().values()) {
                Point p = new Point(point.x + (int)direction.getFirst(), point.y + (int)direction.getSecond());
                if(p.x < 0 || p.y < 0 || p.x >=WIDTH || p.y >= HEIGHT) continue;
                if(world[p.x][p.y] == Tileset.FLOOR) continue;
                if(world[p.x][p.y] == Tileset.WALL) continue;
                
                WallPoints.add(new Point(p.x, p.y));
            }
  • 写回答

1条回答 默认 最新

  • Dummer25 2023-04-04 13:45
    关注
    public class Directions {
        private static final Map<String, Tuple<Integer, Integer>> directions = new HashMap<>();
    
        static {
            directions.put("UP", new Tuple<>(0, 1));
            directions.put("DOWN", new Tuple<>(0, -1));
            directions.put("LEFT", new Tuple<>(-1, 0));
            directions.put("RIGHT", new Tuple<>(1, 0));
            directions.put("UPLEFT", new Tuple<>(-1, 1));
            directions.put("UPRIGHT", new Tuple<>(1, 1));
            directions.put("DOWNLEFT", new Tuple<>(-1, -1));
            directions.put("DOWNRIGHT", new Tuple<>(1, -1));
        }
    
        public static Map<String, Tuple<Integer, Integer>> getDirections() {
            return Collections.unmodifiableMap(directions);
        }
    }
    
    
    
    public Collection<V> values() {
        Collection<V> vs = new ArrayList<>();
        for (Entry<K, V> e : entrySet()) {
            vs.add(e.getValue());
        }
        return vs;
    }
    
    
    
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 4月4日
  • 创建了问题 4月4日