Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (686)
Games in Android Showcase (198)
games submitted by our members
Games in WIP (758)
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 2522 times)
0 Members and 1 Guest are viewing this topic.
Offline wessles

JGO Kernel


Medals: 258
Projects: 4


Enthusiast of amusement. Lover of code.


« 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.

RFLEX.
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 167
Projects: 5
Exp: 6 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

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline wessles

JGO Kernel


Medals: 258
Projects: 4


Enthusiast of amusement. Lover of code.


« 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]

RFLEX.
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

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 
roseslayer (457 views)
2016-08-06 11:43:29

roseslayer (416 views)
2016-08-06 09:43:11

xTheGamerCodes (492 views)
2016-08-04 15:40:59

xTheGamerCodes (484 views)
2016-08-04 15:40:24

orrenravid (833 views)
2016-07-16 03:57:23

theagentd (910 views)
2016-07-11 14:28:54

Hydroque (1006 views)
2016-07-06 05:56:57

Hydroque (1011 views)
2016-07-03 08:52:54

GrandCastle (824 views)
2016-07-01 09:13:47

GrandCastle (640 views)
2016-07-01 09:09:45
Rendering resources
by Roquen
2016-08-08 05:55:21

Rendering resources
by Roquen
2016-08-08 05:52:42

Rendering resources
by Roquen
2016-08-08 05:50:38

Rendering resources
by Roquen
2016-08-08 05:49:53

Rendering resources
by Roquen
2016-08-08 05:32:39

Making a Dynamic Plugin System
by Hydroque
2016-06-25 00:13:25

Java Data structures
by BinaryMonkL
2016-06-13 21:22:09

Java Data structures
by BinaryMonkL
2016-06-13 21:20:42
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!