Hey there. I have working code that reads in a text file into a 2d array. The text file holds symbols that make up a map for a game. I'm trying to assign each of the symbols (like ^, for a mountain) to an object, and store those in the 2d array as well, so that my map is made up of mapspace objects (desert space, mountain space...all of which I have existing classes for). I'm thinking of using if(possibly?) statements to check each character that is stored in the array & instantiate a new mountain/desert object.
I'm not sure how to go about it, though.... Here's what I have so far:
public class Map {
ArrayList array = new ArrayList();
char [][] linksWorld = null;
public Map(String mapFile) {
try{
BufferedReader in = new BufferedReader(new FileReader(mapFile));
String data;
//Read from file
while ((data = in.readLine()) != null)
{
//Convert data to char array and add into array
array.add(data.toCharArray());
}
in.close();
//Creating a 2D char array using the arraylist size
linksWorld = new char [array.size()][];
//Convert array from ArrayList to 2D array
for (int i=0; i<array.size(); i++){
linksWorld[i] = (char[])array.get(i);
}
//Test 2d array
for (int j=0; j<linksWorld.length; j++){
char [] temp = linksWorld[j];
for (int x=0; x<temp.length; x++)
{
System.out.print(temp[x]);
}
System.out.println("");
}
}
catch (Exception ex){
ex.printStackTrace();
}
}//constructor
//test
public static void main(String[] args){
Map map = new Map("samplemap.txt");
}
}