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日

悬赏问题

  • ¥30 计算机硬件实验报告寻代
  • ¥15 51单片机写代码,要求是图片上的要求,请大家积极参与,设计一个时钟,时间从12:00开始计时,液晶屏第一行显示time,第二行显示时间
  • ¥15 用C语言判断命题逻辑关系
  • ¥15 原子操作+O3编译,程序挂住
  • ¥15 使用STM32F103C6微控制器设计两个从0到F计数的一位数计数器(数字),同时,有一个控制按钮,可以选择哪个计数器工作:需要两个七段显示器和一个按钮。
  • ¥15 在yolo1到yolo11网络模型中,具体有哪些模型可以用作图像分类?
  • ¥15 AD9910输出波形向上偏移,波谷不为0V
  • ¥15 淘宝自动下单XPath自动点击插件无法点击特定<span>元素,如何解决?
  • ¥15 曙光1620-g30服务器安装硬盘后 看不到硬盘
  • ¥15 抖音直播广场scheme