查找变量值最大的对象

问题描述 投票:-1回答:1

我有一个二维数组,用于存储每个Tile对象的索引位置。如何找到具有最大可变值或最大索引值的对象。我要在这里找到的变量是posX和posY。

请注意,我无法使用HashMap,因为我已经使用了2d数组,也没有使用定义地图宽度和高度的变量。我想制作无限创意游乐场之类的东西,但是在编译地图时,它仅显示最小和最大坐标值范围内的所有对象。就像自动尺寸定义系统一样

我已经检查过Google,但它们仅使用2d数据类型,例如整数,而不是对象。

这是我的代码:

HashMap<Tile, Float> xTileCoordinateValue = new HashMap<>();
                HashMap<Tile, Float> yTileCoordinateValue = new HashMap<>();
                HashMap<Autotile, Float> xAutotileCoordinateValue = new HashMap<>();
                HashMap<Autotile, Float> yAutotileCoordinateValue = new HashMap<>();

                float deltaXtile = 0, deltaYtile = 0;
                float deltaXautotile = 0, deltaYautotile = 0;

                for (int h = 0; h < mapCreator.getMapH(); h++) {
                    for (int w = 0; w < mapCreator.getMapW(); w++) {
                        if (mapCreator.getTileMap()[w][h] != null) {
                            Tile tile = mapCreator.getTileMap()[w][h];

                            xTileCoordinateValue.put(tile, tile.getPosX() / mapCreator.getTileSize());
                            yTileCoordinateValue.put(tile, tile.getPosY() / mapCreator.getTileSize());

                            float minXtile = Collections.min(xTileCoordinateValue.values());
                            float minYtile = Collections.min(yTileCoordinateValue.values());
                            float maxXtile = Collections.max(xTileCoordinateValue.values());
                            float maxYtile = Collections.max(yTileCoordinateValue.values());

                            deltaXtile = maxXtile - minXtile;
                            deltaYtile = maxYtile - minYtile;
                        }

                        if (mapCreator.getAutotileMap()[w][h] != null) {
                            Autotile autotile = mapCreator.getAutotileMap()[w][h];

                            xAutotileCoordinateValue.put(autotile, autotile.getPosX() / mapCreator.getTileSize());
                            yAutotileCoordinateValue.put(autotile, autotile.getPosY() / mapCreator.getTileSize());

                            float minXautotile = Collections.min(xAutotileCoordinateValue.values());
                            float minYautotile = Collections.min(yAutotileCoordinateValue.values());
                            float maxXautotile = Collections.max(xAutotileCoordinateValue.values());
                            float maxYautotile = Collections.max(yAutotileCoordinateValue.values());

                            deltaXautotile = maxXautotile - minXautotile + 1;
                            deltaYautotile = maxYautotile - minYautotile + 1;
                        }
                    }
                }

                DebugMethods.debugMessage("Default tile size: " + deltaXtile + "/" + deltaYtile);
                DebugMethods.debugMessage("Autotile size: " + deltaXautotile + "/" + deltaYautotile);

得到了2种类型瓷砖的极限尺寸...现在如何计算地图的宽度和高度...

我在左上角放置了一个普通图块,在右下角放置了一个小块拼图(瓦片限制:1/1,小块拼图限制:1/1),这些瓦片会形成一个大小为9的框,因此地图应为9x9格/块。我该如何计算?我无法采用最大值-最小值,因为有2种类型的图块。我需要检查一种瓷砖的数量为0吗?

java 2d-games
1个回答
0
投票

您可以做很多事情来修正代码。首先,瓦片到pos的哈希图完全没有意义。

HashMap<Tile, Float> xTileCoordinateValue = new HashMap<>();

您的图块具有该信息,因此您真正需要的只是一个列表。

List<Tile> tiles = ...;

您如何生成列表?一种方法,如果必须使用Tile [] [],则可以使用流和flatMap。

List<Tile> tiles = Arrays.stream( 
                     mapCreator.getTileMap() 
                   ).flatmap(
                     Arrays::stream
                   ).filter(o-> o!=null).collect(Collectors.toList());

然后,您将获得所有图块的列表,并且可以使用您使用的Collection方法获得最小值/最大值,也可以再次使用流。

double maxX = tiles.steam().mapToDouble(Tile::getPosX).max().orElse(-1);

或者,如果不想重复两次以获取X和Y最大值,则可以使用更传统的循环。

您可以使用地图代替Tile [] []。这是一个示例,除了使用Sting代替Tile之外。

import java.util.*;
import java.util.stream.*;
import java.awt.Point;

public class JunkCollections{
    public static void main(String[] args){
        String[][] grid = {
            { "one", "two", "three"},
            {"four", "five", "six"}
        };
        List<String> strings = Arrays.stream( grid ).flatMap( s -> Arrays.stream(s)).collect(Collectors.toList());
        Map<Point, String> mapped = new HashMap<>();
        for(int i = 0; i<2; i++){
            for(int j = 0; j<3; j++){
                mapped.put(new Point(i,j), grid[i][j]);
            }
        }

        for(int i = 0; i<2; i++){
            for(int j = 0; j<3; j++){
                System.out.print(mapped.get(new Point(i,j)) + " ");
            }
            System.out.println();
        }
    }
}

输出为:

一二三四五六

这具有不需要为网格尺寸指定数组大小,并且可以任意添加图块以添加不同点的优点。

© www.soinside.com 2019 - 2024. All rights reserved.