Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (517)
Games in Android Showcase (123)
games submitted by our members
Games in WIP (578)
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  
  Make a tilemap based off of text file?  (Read 1515 times)
0 Members and 1 Guest are viewing this topic.
Offline wessles

JGO Wizard


Medals: 67
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Posted 2013-06-08 17:19:14 »

So I have a text file like this:
1 0 1 1 1 1 0 1
1 0 1 1 1 1 0 1
1 0 1 1 1 1 0 1
1 0 1 1 1 1 0 1
1 0 1 1 1 1 0 1
1 0 1 1 1 1 0 1
1 0 1 1 1 1 0 1
1 0 1 1 1 1 0 1

And I want to get the following from this file:
The number of lines
A double array of all values (i.e. In this file, tiles[2][2] = 0)


So should I use scanner, or what? I tried scanner, but cannot figure out how to get this info.

Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 136
Projects: 4
Exp: 3 years



« Reply #1 - Posted 2013-06-08 19:36:08 »

http://lmgtfy.com/?q=java+read+a+text+file
http://lmgtfy.com/?q=java+find+number+of+lines+in+a+text+file
Use google, guys! This has been asked so many times on so many different forums.
Offline Longor1996
« Reply #2 - Posted 2013-06-08 20:51:15 »

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  
public int[] readMap(Scanner scanner)
{
    // storage for the raw lines
    Vector<String[]> lines = new Vector<String[]>();
   
    // width and height of the map
    int width = 0;
    int height = 0;
   
    // read all lines from scanner
   
    while(scanner.hasNextLine()){
        String[] str_tiles = scannet.nextLine().split(" ");
       
        if(str_tiles.length > width)
                width = str_tiles.length;
       
        lines.add(str_tiles);
        height++;
    }
   
    // we are done reading
   
    // create map array
    int[][] map = new int[height][width];
   
    int yCount = 0;
    for(String[] strAry : lines){
       
        for(int xCount = 0; xCount < strAry.length; xCount++){
            map[yCount][xCount] = Integer.parseInt(strAry[xCount]);
        }
       
        yCount++;
    }
   
    return map;
}


Just some simple code on how to read a map.
Note that this code is really inefficient and won't work for map's that are very big (like 1000x1000 tiles).

- Longor1996

Sorry for my bad English! That's because i am from Germany.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline wessles

JGO Wizard


Medals: 67
Projects: 4
Exp: 3 years


Profile picture isn't relevant.


« Reply #3 - Posted 2013-06-08 21:34:41 »

Thank you longor, Although, in case anyone else needs that code, just please fix it. The orientation is off. Quite simple, however. All you need to do is [size=10pt]switch xCount and yCount towards the end...[/size]

Offline Longor1996
« Reply #4 - Posted 2013-06-09 11:09:01 »

There is a reason the xCount/yCount are used this way.
Reason: Images (too) are saved in a PIXEL[y]
  • way.
And since tile-map's are just image's in my opinion, im using it this way.
An even better way to do the whole thing would be to just use a int[index] array.

1  
2  
3  
4  
int[] map = new int[width * height];

// input: x, y, value
map[x + y * width] = value;


This is:
- Memory efficient
- A bit faster

Have a nice day!

- Longor1996

Sorry for my bad English! That's because i am from Germany.
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.

DarkCart (14 views)
2014-10-31 21:44:48

DarkCart (18 views)
2014-10-31 21:43:57

TehJavaDev (40 views)
2014-10-27 03:28:38

TehJavaDev (30 views)
2014-10-27 03:27:51

DarkCart (44 views)
2014-10-26 19:37:11

Luminem (26 views)
2014-10-26 10:17:50

Luminem (30 views)
2014-10-26 10:14:04

theagentd (36 views)
2014-10-25 15:46:29

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

Norakomi (62 views)
2014-10-16 15:22:06
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!