java - Creating a room by reading from a file (for a game) -


basically have make game , need create room reading file, one:

# # 0 d room0.txt 0 # 1 e room1.txt 0 # wwww0wwwww w        w w        w w        w w        w w      w w w      w w w      w w w      w 1 wwwwwwwwww 

the "#" means can skip lines(they're information) , "w" places need put wall, numbers doors , blank spaces floor.

i figured best way create room create method receives file , reads , put "information" string[10][10], , create method(or in main) receives string[10][10] created , creates room(adds images room), having difficulties reading file if guys me part thankful.

the error when run program on line:

if(r[x][y].equals("w"))

if guys need class, image of game should or else forgot show please let me know , help.

public void generateroom(file file){          if(file.exists()){         scanner sc = null;              string[][] r = new string[10][];         int row = 0;         try {             sc = new scanner(file);              string line = sc.nextline();              while(sc.hasnextline() && row < 10){                 if(line.startswith("#"))                     sc.nextline();                 else{                     string[] s0 = line.split("");                     if(s0.length==10){                         r[row]=s0;                      row++;                 }                      }         }         for(int x = 0; x < 10; x++){              for(int y = 0; y < 10; y++){              if(r[x][y].equals("w"))                    tiles.add(new wall(new position(x,y)));                    if(r[x][y].equals("1") || r[x][y].equals("2"))                    tiles.add(new door(new position(x,y)));              if(r[x][y].equals("0")){                  tiles.add(new door(new position(x,y)));                  hero.setposition(new position(x,y));                  tiles.add(hero);              }              else tiles.add(new floor(new position(x,y)));              gui.newimages(tiles);              gui.update();          }         }         }catch (filenotfoundexception e) {             system.out.println("ficheiro "+file.getabsolutepath()+                     " não existe. ");           }         finally{             if(sc!=null)sc.close();         }     }     else         system.out.println("ficheiro "+file.getabsolutepath()+                 " nã£o existe. ");           

}

you never reassigning line.

this:

if(line.startswith("#"))     sc.nextline(); 

should read

if(line.startswith("#"))     line = sc.nextline(); 

but procedure overly complicated. can read file , generate room in single pass. consider following version of procedure:

public static void generateroom(file file) throws filenotfoundexception {     if (file.exists()) {         scanner sc = new scanner(file);         int row = 0;         while (sc.hasnextline()) {             string line = sc.nextline();             if (line.startswith("#"))                 continue;             (int col = 0; col < line.length(); col++) {                 switch (line.charat(col)) {                 case 'w':                     system.out.println("new wall @" + row + '/' + col);                     break;                 case '0':                     system.out.println("hero @" + row + '/' + col);                     // fall-through                 case '1':                 case '2':                     system.out.println("new door @" + row + '/' + col);                     break;                 case ' ':                     system.out.println("new floor tile @" + row + '/' + col);                     break;                 default:                     system.out.println("invalid char @" + row + '/' + col);                 }             }             row++;         }         sc.close();     } else         system.out.println("ficheiro " + file.getabsolutepath() + " nã£o existe."); } 

it avoids temporary string array altogether. additionally, can use switch statement decoding characters in file.


Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -