Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Level Reader  (Read 3295 times)
0 Members and 1 Guest are viewing this topic.
Offline cubemaster21
« Posted 2012-05-30 00: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[]; //Here is the error.
   }
}

Any help please?

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline UprightPath
« Reply #1 - Posted 2012-05-30 00:08:39 »

What's the error?

Offline cubemaster21
« Reply #2 - Posted 2012-05-30 00:10:55 »

"Syntax error on token "[", Expression expected after this token"

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sproingie

JGO Kernel


Medals: 202



« Reply #3 - Posted 2012-05-30 00: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.
Offline Nate

JGO Kernel


Medals: 149
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #4 - Posted 2012-05-30 00:41:12 »

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/
Smiley

Offline cubemaster21
« Reply #5 - Posted 2012-05-30 00: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.

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline sproingie

JGO Kernel


Medals: 202



« Reply #6 - Posted 2012-05-30 02: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>>()
Offline Danny02
« Reply #7 - Posted 2012-05-30 10:28:24 »

I also think that writing
1  
String var[]

instead of
1  
String[] var

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;//in Java variables should start lowercase


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
Offline cubemaster21
« Reply #8 - Posted 2012-05-30 10: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 String[][] Level = null;
   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));
               
               //System.out.println("new platform");
            }
         }
      }
   }
}

I don't get any errors, but it also doesn't work.

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline Danny02
« Reply #9 - Posted 2012-05-30 11: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){//method naming convention is the same as for variables starting with lowercase then for following word upper case
                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!
Legends of Yore - The Casual Retro Roguelike
Offline ReBirth
« Reply #10 - Posted 2012-05-30 13: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/
Smiley
I admit this is the best solution. On other there's also Jackson.

Offline gimbal

JGO Knight


Medals: 25



« Reply #11 - Posted 2012-05-30 14: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/
Smiley

That is really neat, Nate. Am I to assume that you are the author? (based on example code on that page).
Offline Nate

JGO Kernel


Medals: 149
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #12 - Posted 2012-05-30 17: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! Smiley 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.

Offline cubemaster21
« Reply #13 - Posted 2012-05-31 01: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?

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #14 - Posted 2012-05-31 01: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? Smiley

Offline ReBirth
« Reply #15 - Posted 2012-05-31 03:45:16 »

@Nate: It depends. For simplicity, that's win Grin

Offline cubemaster21
« Reply #16 - Posted 2012-05-31 11: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? Smiley
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?

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #17 - Posted 2012-05-31 11: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")));

//write!

writer.close();

Offline matheus23

JGO Kernel


Medals: 109
Projects: 3


You think about my Avatar right now!


« Reply #18 - Posted 2012-05-31 11: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")));

//write!

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 /* One Megabyte buffer */));

//write!

writer.close();

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline cubemaster21
« Reply #19 - Posted 2012-05-31 12: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.

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #20 - Posted 2012-05-31 12: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")));

//write!

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 /* One Megabyte buffer */));

//write!

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 Wink

Offline cubemaster21
« Reply #21 - Posted 2012-05-31 22: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){
      //setup
      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(){
   //   try{
         CreateMatrixBase();
                        //Please ignore the obvious error that i made where i forgot to change all of the gets to their respective lists..

         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:");
         }
//      }catch(Exception e){
   //      
   //      throw new RuntimeException("Level is too big!");  
   //   }

   }
   public static void CreateMatrixBase(){
      for (int y = 0; y < 50000;y++){
         for (int x = 0;x < 50000; x++){
            newData.get(y).add(x,"0:");
         }
      }
   }
}

Check out my game, Viking Supermarket Smash
http://www.java-gaming.org/topics/iconified/28984/view.html
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (49 views)
2014-10-17 03:59:02

Norakomi (38 views)
2014-10-16 15:22:06

Norakomi (31 views)
2014-10-16 15:20:20

lcass (35 views)
2014-10-15 16:18:58

TehJavaDev (65 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (55 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (43 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!