0

I need to loop through a map that looks like the example bellow and get the width and height of it as well as the number at each point of the file. I then add a tile to that position (multiplied by size) width the parameters (x, y, width, height, id). "id" is the current number in the text file. Here's an example of what the file would look like:

2 0 0 0 0 0 0 0 0 2
2 0 0 0 0 0 0 0 0 2
2 0 0 0 0 0 0 0 0 2
1 1 1 1 1 1 1 1 1 1

I've atempted to add a new Tile at the respective position on the map but mapWidth and mapHeight are returning the actual data in the first position of the file (int this case, 2). How can I return width and height of this text file's data so I can add a tile to the array? Here's the code I've tried:

try {
    Tile[][] tiles;
    int mapWidth, mapHeight;
    int size = 64;
    //variables in same scop to save you time :)

    FileHandle file = Gdx.files.internal(s);

    BufferedReader br = new BufferedReader(file.reader());

    mapWidth = Integer.parseInt(br.readLine());
    mapHeight = Integer.parseInt(br.readLine());

    System.out.println(mapWidth);

    int[][] map = new int[mapWidth][mapHeight];
    tiles = new Tile[mapWidth][mapHeight];

    for(int i = 0; i < tiles.length; i++) {
        for(int j = 0; j < tiles[i].length; j++) {
            tiles[i][j] = new Tile(i * size, j * size, size, map[j][i]);
        }
    }

    br.close();         
} catch(Exception e) { e.printStackTrace(); }
Martin M J
  • 830
  • 1
  • 5
  • 12
jay
  • 7
  • 1
  • 1
  • 3

1 Answers1

0

It looks like the problem is how you are parsing the file. For the nature of this problem, you need to parse line by line, then within the line, you can parse item by item.

Here's a general example of how to parse the data out (I'm using List and ArrayList so that I don't need to deal with the size of the array... if you want to use a physical array you can read the whole file into memory first List<String> lines = new ArrayList(); then the buffered reader can insert into this List and you can loop over it to add each sub-array of items.

Data:

$ cat Tiles.dat 
2 0 0 0 0 0 0 0 0 2
2 0 0 0 0 0 0 0 0 2
2 0 0 0 0 0 0 0 0 2
1 1 1 1 1 1 1 1 1 1

Script (example to parse the file):

$ cat Tiles.java 
import java.io.*;
import java.util.*;

class MainApp {
    public static void main(final String[] args) {
        final String fileName = "Tiles.dat";
        try {
            File file = new File(fileName);
            FileReader fileReader = new FileReader(file);
            BufferedReader br = new BufferedReader(fileReader);

            List<List<Integer>> map = new ArrayList<List<Integer>>();

            String line = null;
            while ((line = br.readLine()) != null) {
                String[] items = line.split(" ");
                ArrayList<Integer> intItems = new ArrayList<Integer>();
                for (String item : items) {
                    int intItem = Integer.parseInt(item);
                    intItems.add(intItem);
                }

                map.add(intItems);
            }
            br.close();

            System.out.println("map: '" + String.valueOf(map) + "'.");
            for (List<Integer> intItems : map) {
                System.out.println("intItems: '" + String.valueOf(intItems) + "'.");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Output:

$ javac Tiles.java 
$ java MainApp 
map: '[[2, 0, 0, 0, 0, 0, 0, 0, 0, 2], [2, 0, 0, 0, 0, 0, 0, 0, 0, 2], [2, 0, 0, 0, 0, 0, 0, 0, 0, 2], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]'.
intItems: '[2, 0, 0, 0, 0, 0, 0, 0, 0, 2]'.
intItems: '[2, 0, 0, 0, 0, 0, 0, 0, 0, 2]'.
intItems: '[2, 0, 0, 0, 0, 0, 0, 0, 0, 2]'.
intItems: '[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]'.

Hope that helps!

anonymous
  • 657
  • 1
  • 8
  • 21