Java从property.file填充int [] []数组

时间:2016-01-06 12:20:34

标签: java arrays properties

我想用Java编写一个pacmanstyle迷宫游戏。 对于迷宫,我正在使用属性文件,其中坐标存储(即x = 1,y = 5 - > wall)作为字符串,格式如下:79,13=0 79,12=0 79,11=0

我想使用二维数组创建mazegrid:int [][] maze。 我知道如何加载属性文件。然而我的问题是,我不知道如何首先从属性中提取字符串变量,其次是填充数组的正确方法。

public final class Labyrinth {

    private int i;
    private int j;
    private static int [][] maze;
    private String p;
    private String l;

    public void setMaze(int x, int y){
        x = this.i;
        y = this.j;
    }

    public static int[][] getMaze(){
        return maze;        
    }

    public Labyrinth(int rows, int cols) throws IOException{        
        try (FileInputStream in = new FileInputStream("level.properties")) {
            Properties p1 = new Properties();
            p1.load(in);
            p = p1.getProperty("Height");
            l = p1.getProperty("Width");

            cols = parseInt(p);
            rows = parseInt(l);
            maze = new int[rows][cols];

            for (i=0; i < rows; i++){
                for(j=0; j < cols; j++){
                    setMaze(parseInt(p1.getProperty('ValueX,ValueY')),
                            parseInt(p1.getProperty('ValueX,ValueY')));               
                }
            }        
        }
    }
}

任何有益的想法都将受到高度赞赏!

1 个答案:

答案 0 :(得分:1)

  

我不知道如何首先从.property中提取字符串变量,其次是填充数组的正确方法。

提取字符串变量是什么意思?属性文件只是键值对的列表。在您的情况下,键是x,y,并且该值显然表示迷宫中的某个对象。

您可以尝试阅读这样的键:

for (i = 0; i < rows; i++) {
    for(j = 0; j < cols; j++) {
        int value = parseInt(p1.getProperty(i + "," + j); // Get the value of (i,j)
        maze[i][j] = value; // Assign the value to the maze
    }
}
相关问题