Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ACSII世界地图导入和读取_Java_Dictionary_Ascii_Bufferedreader - Fatal编程技术网

Java ACSII世界地图导入和读取

Java ACSII世界地图导入和读取,java,dictionary,ascii,bufferedreader,Java,Dictionary,Ascii,Bufferedreader,我正在用Java制作一个游戏,我希望玩家能够在一个有特定边界的世界中移动 在网上搜索定义“世界”的方法后,换句话说,玩家可以移动到哪里,不可以移动到哪里 我喜欢2D ASCII地图的想法,其中一个字符代表一定数量的像素,例如: ################### #-----------------# #-----###---------# #-----###---------# #-----------------# ################### class WorldMap

我正在用Java制作一个游戏,我希望玩家能够在一个有特定边界的世界中移动

在网上搜索定义“世界”的方法后,换句话说,玩家可以移动到哪里,不可以移动到哪里

我喜欢2D ASCII地图的想法,其中一个字符代表一定数量的像素,例如:

###################
#-----------------#
#-----###---------#
#-----###---------#
#-----------------#
###################
class WorldMap {
    private final char[7][3] world = new char[7][3];

    //x,y below are pixels, so you don't need to convert from array index to pixels
    //everywhere in your code
    public boolean canMoveTo(int x, int y) { ... }

    public void addObstacle (Rectangle r) { ... }
}
在本例中,
-
字符表示允许玩家进入的10x10平铺,而
#
字符表示玩家无法进入的10x10区域

在Java中,我使用以下方法导入
world.txt

void importMap() {
  BufferedReader br = new BufferedReader(new FileReader("world.txt"));
  String line;
  while ((line = br.readLine()) != null) {
     for (char ch: line.toCharArray()) {
       // Add to array
     }
  }
  br.close();
}
但是,我不知道在哪里存储这些数据。我在考虑一个2D数组,我可以简单地得到某个x,y值的字符,如下所示:

world[7][3];
// Should return #

如何存储此ASCII映射?2D数组、2D对象、hashmap等?

将信息存储在2D数组中似乎是一个合理的解决方案。但是,将其隐藏在类中可能会使您受益,例如:

###################
#-----------------#
#-----###---------#
#-----###---------#
#-----------------#
###################
class WorldMap {
    private final char[7][3] world = new char[7][3];

    //x,y below are pixels, so you don't need to convert from array index to pixels
    //everywhere in your code
    public boolean canMoveTo(int x, int y) { ... }

    public void addObstacle (Rectangle r) { ... }
}

你明白了。

将数据存储在2D数组中的主要缺点是,在解析以创建合适的数组之前,你应该知道“世界”的大小。如果大小不变,则2D阵列将适合您的解决方案。如果不是-考虑使用不同的结构。


您还可以创建一些动态扩展的结构,如ArrayList,并在解析后将其转换为2D数组。这将解决一个未知大小的问题。

2d阵列最好,因为它具有最小的内存占用,并且您将主要进行直接访问。hashmap会带来空间开销,实际上不会添加任何有用的内容。正如@Assylas所提到的,抽象类中的数据结构有助于实现干净的接口@Alexander Tokarev关于不知道阵列尺寸的说法是正确的,但是您可以使用NIO解决这个问题

char[][] createWorld(String file)
{
    try {
        List<String> lines = Files.readAllLines(Paths.get(file),
            Charset.forName("US-ASCII"));
        char[][] world = new char[lines.size()][];
        int i = 0;
        for(String ln: lines)
            world[i++] = ln.toCharArray();
        return world;
    } catch (IOException ex) {
        Logger.getGlobal().log(Level.SEVERE, null, ex);
    }
    return null;
}
char[]createWorld(字符串文件)
{
试一试{
列表行=文件.readAllLines(路径.get(文件),
字符集forName(“US-ASCII”);
char[][]world=new char[lines.size()][];
int i=0;
用于(字符串项次:行)
world[i++]=ln.toCharArray();
回归世界;
}捕获(IOEX异常){
Logger.getGlobal().log(Level.SEVERE,null,ex);
}
返回null;
}
动态2D数组类(可能使用ArrayList的ArrayList实现)会很酷。