2
\$\begingroup\$

I am working on an RPG in Java and I am using a tile-based map. The characters will not be building, so I will not need to store the maps outside of the game, but I feel as if there is a more efficient way to initialize my maps.

public static int[][] spawnWorld = {
            { 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
                    14, 14, 14 },
            { 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
                    14, 14, 14, 14 },
            { 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
                    14, 14, 14, 14 },
            { 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
                    14, 14, 14, 14 },
            { 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
                    14, 14, 14, 14 },
            { 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
                    14, 14, 14, 14 },
            { 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
                    14, 14, 14, 14 },
            { 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
                    14, 14, 14, 14 },
            { 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
                    14, 14, 14, 14 },
            { 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
                    14, 14, 14, 14 },
            { 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
                    14, 14, 14, 14 },
            { 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
                    14, 14, 14, 14 },
            { 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
                    14, 14, 14, 14 },
            { 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
                    14, 14, 14, 14 },
            { 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
                    14, 14, 14, 14 },
            { 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
                    14, 14, 14, 14 },
            { 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 6, 3, 3, 14, 14, 14, 14, 14, 14, 14, 14, 14,
                    14, 14, 14, 14 },
            { 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 1, 11, 4, 14, 14, 14, 14, 14, 14, 14, 14, 14,
                    14, 14, 14, 14 },
            { 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 1, 2, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
                    14, 14, 14, 14 },
            { 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 1, 2, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
                    14, 14, 14, 14 },
            { 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 1, 2, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
                    14, 14, 14, 14 },
            { 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 1, 2, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
                    14, 14, 14, 14 },
            { 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 1, 2, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
                    14, 14, 14, 14 },
            { 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 6, 5, 7, 8, 14, 14, 14, 14, 14, 14, 14, 14, 14,
                    14, 14, 14, 14 },
            { 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 1, 0, 0, 2, 14, 14, 14, 14, 14, 14, 14, 14, 14,
                    14, 14, 14, 14 },
            { 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 1, 0, 0, 2, 14, 14, 14, 14, 14, 14, 14, 14, 14,
                    14, 14, 14, 14 },
            { 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 10, 4, 4, 12, 14, 14, 14, 14, 14, 14, 14, 14, 14,
                    14, 14, 14, 14 },
            { 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
                    14, 14, 14, 14 },
            { 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
                    14, 14, 14, 14 },
            { 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
                    14, 14, 14 } };
    public static int[][] spawnSigns = {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
public static int[][] mainWorld;
public static int[][] grassHouse1;

It's just a bunch of cluttering numbers that takes up a lot of space. How can I make this more efficient?

EDIT:

A list of the tile ids so far:

VGrassPathCenter - 0

VGrassPathLeft - 1

VGrassPathRight - 2

VGrassPathTop - 3

VGrassPathBottom - 4

VGrassPathTopLeftCorner - 5

VGrassPathTopLeft - 6

VGrassPathTopRightCorner - 7

VGrassPathTopRight - 8

VGrassPathBottomLeftCorner - 9

VGrassPathBottomLeft - 10

VGrassPathBottomRightCorner - 11

VGrassPathBottomRight - 12

EmptyTile - 13

Grass - 14

Signs are just 1 = Sign, 0 = No Sign.

\$\endgroup\$
2
  • \$\begingroup\$ Could you maybe list what the numbers are for? Like, what makes a tile = 14 instead of 0 or 1? \$\endgroup\$
    – IEatBagels
    Aug 22, 2019 at 13:08
  • \$\begingroup\$ @IEatBagels, Just added Tile Ids. Its to tell the Tile Writing method which tile to put down. \$\endgroup\$ Aug 22, 2019 at 14:02

2 Answers 2

2
\$\begingroup\$

Is there a better way? Plainly speaking, not so much.

The thing is, you decide what is the content of the array and the clearest way of doing so is how you did it. We could do something like this :

//I put x,y because I didn't count the row/column sizes
public static int[][] spawnWorld = new int[x][y];

Array.fill(spawnWorld, 14);

//Those are random indices
spawnWorld[14][10] = 1;
spawnWorld[15][10] = 1;
spawnWorld[16][10] = 1;
//etc..

Does that look better? Hell no. At least with your current format, you can see the patterns.

So what can we do?

I think your best bet is to not store the maps in the code. Imagine you wanted to change a map at some point, you would need to recompile your code! That seems overkill. The best solution would be to have separate (text?) files that contains the map. For example :

map1.map

14,14,14,14,14,14,14,14,14,14
14,14,14,1,14,14,14,14,14,14
14,14,14,1,14,14,14,14,14,14
14,14,14,1,14,14,14,14,14,14
14,14,14,1,14,14,14,14,14,14
14,14,14,1,14,14,14,14,14,14

And to load the said map file in your code and put it in an array.

Also, you might want to consider writing a small form application to create the maps and save them to the .map format (I've used .map, but it could be whatever you want). That way you could make your map easily without having to write a bunch of 14s and 1s everywhere.

\$\endgroup\$
3
  • \$\begingroup\$ How would you load the format? \$\endgroup\$ Aug 22, 2019 at 15:23
  • \$\begingroup\$ @WeaponGod243 Well, that's kind of up to you to figure it out :) But if you read it like a text file, it won't be hard \$\endgroup\$
    – IEatBagels
    Aug 22, 2019 at 15:25
  • \$\begingroup\$ Once the data is in a text file, there is no pressing need to use numbers or commas anymore. Choose a suitable character for each tile type and map them to numbers when reading. Then you can edit the maps easily in Emacs (not vi). \$\endgroup\$ Aug 23, 2019 at 8:23
2
\$\begingroup\$

Can't post comments yet so just a thought - If you are going to have external files and will be editing them it might be an idea to map your numbers to emoji characters or other suitable font which might give you a more visually accessible map file format.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ No why did I not think of that! Pretty clever idea. I guess it shows that I'm from the ASCII generation. \$\endgroup\$ Aug 23, 2019 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.