cubemaster21
|
 |
«
Posted
2012-05-30 02:05:06 » |
|
I'm working on a class that's supposed to load the level for my game from a text file. I'm getting one error and I'm wondering how I could fix it. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| package waffles.main;
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException;
public class DataLoader { public String[][] Level = null; public DataLoader(){ } public void LoadLevel(String level){ try { BufferedReader in = new BufferedReader(new FileReader("res/levels/" + level + ".lvl")); String str; int line = 0; String readline[]; while ((str = in.readLine()) != null) { readline = str.split(":"); ProcessData(readline, line); line++; } in.close(); } catch (IOException e) { System.out.println("Level " + level + " is missing!"); } } private void ProcessData(String str[], int line){ Level[line][0] = str[]; } } |
Any help please?
|
|
|
|
UprightPath
|
 |
«
Reply #1 - Posted
2012-05-30 02:08:39 » |
|
What's the error?
|
|
|
|
cubemaster21
|
 |
«
Reply #2 - Posted
2012-05-30 02:10:55 » |
|
"Syntax error on token "[", Expression expected after this token"
|
|
|
|
Games published by our own members! Check 'em out!
|
|
sproingie
|
 |
«
Reply #3 - Posted
2012-05-30 02:13:15 » |
|
Considering the line is "Level[line][0] = str[];" that would be a syntax error. Lose the brackets after str. Better yet, stop using raw arrays and use an ArrayList instead.
|
|
|
|
|
|
|
cubemaster21
|
 |
«
Reply #5 - Posted
2012-05-30 02:47:55 » |
|
Better yet, stop using raw arrays and use an ArrayList instead.
How would I go about this. I've used ArrayLists before, but never as a 2 dimensional array.
|
|
|
|
sproingie
|
 |
«
Reply #6 - Posted
2012-05-30 04:30:20 » |
|
It's not really a 2-dimensional array, it's just an array of arrays. Same goes for lists, it's just a List of Lists. List<List<Foo>> x = new ArrayList<ArrayList<Foo>>()
|
|
|
|
|
Danny02
|
 |
«
Reply #7 - Posted
2012-05-30 12:28:24 » |
|
I also think that writing instead of is a bit misleading, because in Java an array is not a simple datastructure as in C but an own Class. just leav the processdata method out and write 1
| Level[line] = readline; |
notice that because Java don't have 2 dimensional arrays but arrays of arrays we can assign to the first dimension of "Level" an array of String
|
|
|
|
|
cubemaster21
|
 |
«
Reply #8 - Posted
2012-05-30 12:52:44 » |
|
How does this look? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| package waffles.main;
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList;
public class DataLoader { public ArrayList<ArrayList<String>> LevelData = new ArrayList<ArrayList<String>>(); public DataLoader(){ } public void LoadLevel(String level){ try { BufferedReader in = new BufferedReader(new FileReader("res/levels/" + level + ".lvl")); String str; String[] readline; ArrayList<String> newline = new ArrayList<String>(); while ((str = in.readLine()) != null) { readline = str.split(":"); for (int counter = 0; counter < readline.length;counter++){ newline.add(readline[counter]); } ProcessData(newline); } in.close(); } catch (IOException e) { System.out.println("Level " + level + " is missing!"); } Level.BackgroundWalls.clear(); Level.Bullets.clear(); Level.Elevators.clear(); Level.Platforms.clear(); Level.Robots.clear(); Level.WaffleCoins.clear(); Level.Walls.clear(); ConvertData(); } private void ProcessData(ArrayList<String> arg0){ LevelData.add(arg0); } private void ConvertData(){ for (int y = 0;y< LevelData.size();y++){ for (int x = 0;x < LevelData.get(y).size();x++){ String value = LevelData.get(y).get(y); if (value == "1"){ Level.Platforms.add(new Platform(Art.ImgRoot + "env/platform1.png", x, y)); } } } } } |
I don't get any errors, but it also doesn't work.
|
|
|
|
Danny02
|
 |
«
Reply #9 - Posted
2012-05-30 13:22:25 » |
|
How about this? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package waffles.main;
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException;
public class DataLoader { public Level loadLevel(String levelName){ Level level = new Level(); try(BufferedReader in = new BufferedReader(new FileReader("res/levels/" + llevelName + ".lvl"));) { for(int y=0; in.ready(); ++y) { String[] xdata = in.readline().split(":"); for (int x = 0; x <xdata.length; ++x){ level.addPlatform(new Platform(Art.ImgRoot + "env/platform1.png", x, y)); } } } catch (IOException e) { throw new RuntimeException("Level " + level + " is missing!"); } return level; } } |
|
|
|
|
|
Games published by our own members! Check 'em out!
|
|
ReBirth
|
 |
«
Reply #10 - Posted
2012-05-30 15:18:44 » |
|
1 2 3 4 5 6
| Json json = new Json(); ... Level level = ... json.toJson(level, new FileWriter("level")); ... Level level = json.fromJson(Level.class, new FileReader("level")); |
http://code.google.com/p/jsonbeans/ I admit this is the best solution. On other there's also Jackson.
|
|
|
|
gimbal
|
 |
«
Reply #11 - Posted
2012-05-30 16:39:31 » |
|
1 2 3 4 5 6
| Json json = new Json(); ... Level level = ... json.toJson(level, new FileWriter("level")); ... Level level = json.fromJson(Level.class, new FileReader("level")); |
http://code.google.com/p/jsonbeans/ That is really neat, Nate. Am I to assume that you are the author? (based on example code on that page).
|
|
|
|
|
Nate
|
 |
«
Reply #12 - Posted
2012-05-30 19:28:11 » |
|
I admit this is the best solution. On other there's also Jackson.
Not the best, just a possibly simpler solution. This way you can serialize from objects to JSON and back, without code for each piece of data. I wonder how using Jackson would look? How does the JSON look? That is really neat, Nate. Am I to assume that you are the author? (based on example code on that page).
Yep, that's me!  I seem to have an obsession with serialization. I've got projects for binary, JSON, and YAML. I guess I just didn't like the existing libraries for various reasons, and I saw the light on how straightforward it should be -- objects out, objects in. For JSON and YAML, nice looking output was important to me.
|
|
|
|
cubemaster21
|
 |
«
Reply #13 - Posted
2012-05-31 03:31:06 » |
|
Well. Now I have a reader that works. The only problem is that I don't have a class to do the opposite. I attempted to start writing it but quickly stopped and realized that i have no idea what i was doing I don't suppose you guys have a solution?
|
|
|
|
ra4king
|
 |
«
Reply #14 - Posted
2012-05-31 03:55:57 » |
|
"No idea what I was doing. help?" isn't a good question. What don't you know how to do? What do you need help with? 
|
|
|
|
ReBirth
|
 |
«
Reply #15 - Posted
2012-05-31 05:45:16 » |
|
@Nate: It depends. For simplicity, that's win 
|
|
|
|
cubemaster21
|
 |
«
Reply #16 - Posted
2012-05-31 13:25:47 » |
|
"No idea what I was doing. help?" isn't a good question. What don't you know how to do? What do you need help with?  If I have a list of coordinates that have one other number, how can I save that other number to the coordinates given, but in the text file?
|
|
|
|
ra4king
|
 |
«
Reply #17 - Posted
2012-05-31 13:49:22 » |
|
Well depending on the protocol used...it's a simple matter of writing plaintext into the file. What are you having trouble with? 1 2 3 4 5
| PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(new File("path/to/file.txt"),"UTF-8")));
writer.close(); |
|
|
|
|
matheus23
|
 |
«
Reply #18 - Posted
2012-05-31 13:54:10 » |
|
Well depending on the protocol used...it's a simple matter of writing plaintext into the file. What are you having trouble with? 1 2 3 4 5
| PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(new File("path/to/file.txt"),"UTF-8")));
writer.close(); |
Better use it this way, it is faster: 1 2 3 4 5
| PrintWriter writer = new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(new File("path/to/file.txt"),"UTF-8"), 100416 ));
writer.close(); |
|
|
|
|
cubemaster21
|
 |
«
Reply #19 - Posted
2012-05-31 14:12:26 » |
|
The trouble I'm having is that the file format is set up like a matrix and I want to use the coordinates that I talked about earlier as WHERE in the matrix the other data should be stored. I don't know how to set it up this way.
|
|
|
|
ra4king
|
 |
«
Reply #20 - Posted
2012-05-31 14:24:25 » |
|
Well depending on the protocol used...it's a simple matter of writing plaintext into the file. What are you having trouble with? 1 2 3 4 5
| PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(new File("path/to/file.txt"),"UTF-8")));
writer.close(); |
Better use it this way, it is faster: 1 2 3 4 5
| PrintWriter writer = new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(new File("path/to/file.txt"),"UTF-8"), 100416 ));
writer.close(); |
One megabyte is 1048576 bytes. Also a BufferedOutputStream won't make anything faster unless you're over a network. All flushing is done when you close() then PrintWriter, rendering the BOS useless 
|
|
|
|
cubemaster21
|
 |
«
Reply #21 - Posted
2012-06-01 00:56:38 » |
|
I've created this for the writer. It doesn't work though.... And yes, I know, it's not pretty in the least bit. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| package waffles.main;
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList;
public class DataWriter { public static String[][] writeData; public static ArrayList<Platform> Platforms = new ArrayList<Platform>(); public static ArrayList<Entity> Robots = new ArrayList<Entity>(); public static ArrayList<ItemWaffle> WaffleCoins = new ArrayList<ItemWaffle>(); public static ArrayList<Bullet> Bullets = new ArrayList<Bullet>(); public static ArrayList<Wall> Walls = new ArrayList<Wall>(); public static ArrayList<Wall> BackgroundWalls = new ArrayList<Wall>(); public static ArrayList<Door> Elevators = new ArrayList<Door>(); public static ArrayList<ArrayList<String>> newData = new ArrayList<ArrayList<String>>(); @SuppressWarnings("unchecked") public static void DataWriter(ArrayList<ArrayList<?>> LevelData){ Platforms = (ArrayList<Platform>) LevelData.get(0); Robots = (ArrayList<Entity>) LevelData.get(1); WaffleCoins = (ArrayList<ItemWaffle>) LevelData.get(2); Bullets = (ArrayList<Bullet>) LevelData.get(3); Walls = (ArrayList<Wall>) LevelData.get(4); BackgroundWalls = (ArrayList<Wall>) LevelData.get(5); Elevators = (ArrayList<Door>) LevelData.get(6); } public static void CloneFromLevel(){ Platforms = Level.Platforms; Robots = Level.Robots; WaffleCoins = Level.WaffleCoins; Bullets = Level.Bullets; Walls = Level.Walls; BackgroundWalls = Level.BackgroundWalls; Elevators = Level.Elevators; } public static void WriteToFile(String filename){ try{ BufferedWriter writer = new BufferedWriter(new FileWriter(filename + ".lvl")); for (int y = 0;y < newData.size();y++){ for (int x = 0;x < newData.get(y).size();x++){ String inf = null; inf = newData.get(y).get(x); if (inf == null) inf = "0:"; writer.write(inf); } writer.newLine(); } } catch(IOException e){ throw new RuntimeException("Error in creating level file!"); } } public static void SaveMatrix(){ CreateMatrixBase(); for (int x = 0;x < Platforms.size();x++){ newData.get((int) Platforms.get(x).y).add((int) Platforms.get(x).x, "1:"); } for (int x = 0;x < Robots.size();x++){ newData.get((int) Platforms.get(x).y).add((int) Platforms.get(x).x, "2:"); } for (int x = 0;x < WaffleCoins.size();x++){ newData.get((int) Platforms.get(x).y).add((int) Platforms.get(x).x, "3:"); } for (int x = 0;x < Bullets.size();x++){ newData.get((int) Platforms.get(x).y).add((int) Platforms.get(x).x, "3:"); } for (int x = 0;x < Walls.size();x++){ newData.get((int) Platforms.get(x).y).add((int) Platforms.get(x).x, "4:"); } for (int x = 0;x < BackgroundWalls.size();x++){ newData.get((int) Platforms.get(x).y).add((int) Platforms.get(x).x, "5:"); } for (int x = 0;x < Elevators.size();x++){ newData.get((int) Platforms.get(x).y).add((int) Platforms.get(x).x, "6:"); } } public static void CreateMatrixBase(){ for (int y = 0; y < 50000;y++){ for (int x = 0;x < 50000; x++){ newData.get(y).add(x,"0:"); } } } } |
|
|
|
|
|