Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  Resizing a 2D array of objects  (Read 537 times)
0 Members and 1 Guest are viewing this topic.
Offline CopyableCougar4

Senior Member


Medals: 8
Exp: 1 year



« Posted 2014-07-08 18:11:28 »

Hi

For my game's map system I have a 2D array of
Block
objects. I would like to resize/trim the array of null/empty rows and columns. Here is my current code, which doesn't work :/

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
   Block[][] clone = worldBlocks.clone();
      int maxX = 0, maxY = 0;
      for(Block[] row : clone){
         if(row != null){
            maxX = 0;
            for(Block block : row){
               if(block != null){
                  maxX++;
               }
            }
            maxY++;
         }
      }
      Block[][] refreshed = new Block[maxY][maxX];
      for(int y = 0; y < maxY; y++){
         for(int x = 0; x < maxX; x++){
            refreshed[y][x] = clone[y][x];
         }
      }
      worldBlocks = refreshed.clone();


This code should remove any null rows and columns, but it doesn't Smiley Any thoughts or fixes would be appreciated Smiley

CopyableCougar4

Either wandering the forum or programming. Most likely the latter Smiley

Github: http://github.com/CopyableCougar4
Offline Bearded Cow

Senior Member


Medals: 2
Projects: 1
Exp: 1 year


¬..¬


« Reply #1 - Posted 2014-07-08 18:16:01 »

refreshed[ y ][ x ] = clone[ y ][ x ];

I think this is where your issues is, you are not 'skipping' your initial blank positions.


I will try and give you an example,

You have a single dimension array of 1s, 0s.

Like {0,0,1,1,0,0,1}

What you work out in your code is the number of 1s. So 3 in this example, then what you are currently doing is getting the first 3 entries in your code. So you will end up with,

{0,0,1}. Which is not what you seem to want.
Offline CopyableCougar4

Senior Member


Medals: 8
Exp: 1 year



« Reply #2 - Posted 2014-07-08 18:22:51 »

Hi

I should probably explain more.

I create an array 10x10 just so that I won't run out of room, and then I want to shrink it so that there are no empty elements. Initially the array looks like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
1 1 1 1 0 0 0 0 0 0 0 0 0 0
1 1 1 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


But I want it to be like this:
1  
2  
1 1 1 1
1 1 1 1


Hope this makes it more clear Smiley

CopyableCougar4

Either wandering the forum or programming. Most likely the latter Smiley

Github: http://github.com/CopyableCougar4
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Rayvolution

JGO Kernel


Medals: 197
Projects: 2
Exp: 1 year


Resident Crazyman


« Reply #3 - Posted 2014-07-08 18:40:50 »

I have a feeling you're doing something kinda hacky elsewhere if this is how you're setting up your block objects.

But, you can try this hacky code here:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
byte [][] oldArray = new byte[10][10]
int rows = 0;
int col = 0;

for (int x = 0; x < oldArray.length; x++) {
   for (int y = 0; y < oldArray[x].length; y++) {
      if (oldArray[x][y] == 1){
         row = x;
         col = y;
      }
   }
}

byte[][] newArray = new byte[row][col];
for (int x = 0; x < newArray[].length; x++) {
   Arrays.fill(newArray[], 1);
}


Totally untested code (and really hacky, you shouldn't really be doing this at all), but in theory it'll output a new array full of 1s, in the new size. Keep in mind it will only work for rectangle shapes.

- Raymond "Rayvolution" Doerr.
Retro-Pixel Castles - Survival Sim/Builder/Roguelike!
LIVE-STREAMING DEVELOPMENT: http://www.twitch.tv/SG_Rayvolution
Online BurntPizza
« Reply #4 - Posted 2014-07-08 18:43:44 »

Do you want to trim the array, or remove all nulls?
Offline CopyableCougar4

Senior Member


Medals: 8
Exp: 1 year



« Reply #5 - Posted 2014-07-08 18:46:54 »

Hi

This is how I read in my map data:
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  
   public WorldReader(File file){
      ArrayList<String> lines = readFileToList(file);
     world = new World(10, 10); // Allocate space for 100 blocks
     int y = 0, x = 0;
      Block activeBlock = null;
      int blockX = -1, blockY = -1, width = -1, height = -1;
      Tile[][] tilemap = null;
      loop:
      for(String line : lines){
         if(line.startsWith("-------")){
            if(activeBlock != null){
               activeBlock.setTilemap(tilemap);
               world.setBlock(blockX, blockY, activeBlock);
               activeBlock = null;
            }
            String blockInfo = line.substring(7, line.length());
            if(blockInfo.length() <= 0){
               break loop;
            }
            String[] blockData = blockInfo.split(",");
            String blockName = blockData[0];
            blockX = Integer.parseInt(blockData[1]);
            blockY = Integer.parseInt(blockData[2]);
            width = Integer.parseInt(blockData[3]);
            height = Integer.parseInt(blockData[4]);
            tilemap = new Tile[height][width];
            activeBlock = new Block(blockName);
            continue loop;
         }
         x = 0;
         for(String statement : line.split(" ")){
            String layer1 = statement.substring(0, 1);
            int tileID = Integer.parseInt(layer1);
            Tile tile = TileRegistry.getMap().get(tileID);
            tilemap[y][x] = tile;
            x++;
         }
         y++;
      }
     world.trim(); // Trim null rows and columns
     System.out.println(world.getWidth() + "x" + world.getHeight());
   }

This is why I have to have hacky world code, because I don't know how big the array will be until AFTER the whole file has been read/parsed.

CopyableCougar4

Either wandering the forum or programming. Most likely the latter Smiley

Github: http://github.com/CopyableCougar4
Offline ags1

JGO Ninja


Medals: 62
Projects: 3
Exp: 5 years


Make code not war!


« Reply #6 - Posted 2014-07-08 18:54:44 »

Simple: change your file format to include a header row that tells you the dimensions, and walk away from all this hackiness.

Offline CopyableCougar4

Senior Member


Medals: 8
Exp: 1 year



« Reply #7 - Posted 2014-07-08 18:55:16 »

Hi

I suppose that would work Tongue

CopyableCougar4

Either wandering the forum or programming. Most likely the latter Smiley

Github: http://github.com/CopyableCougar4
Online BurntPizza
« Reply #8 - Posted 2014-07-08 18:56:49 »

That, or even use a List<List<Block>> as you parse, and use toArray() when done filling it with blocks.
Offline CopyableCougar4

Senior Member


Medals: 8
Exp: 1 year



« Reply #9 - Posted 2014-07-08 18:59:35 »

Hi

So I added that one line about the block dimensions and it solved my problem Smiley Why do the easiest solutions evade me so often? Smiley

CopyableCougar4

Either wandering the forum or programming. Most likely the latter Smiley

Github: http://github.com/CopyableCougar4
Pages: [1]
  ignore  |  Print  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

Dwinin (25 views)
2014-09-12 09:08:26

Norakomi (56 views)
2014-09-10 13:57:51

TehJavaDev (69 views)
2014-09-10 06:39:09

Tekkerue (35 views)
2014-09-09 02:24:56

mitcheeb (56 views)
2014-09-08 06:06:29

BurntPizza (40 views)
2014-09-07 01:13:42

Longarmx (26 views)
2014-09-07 01:12:14

Longarmx (33 views)
2014-09-07 01:11:22

Longarmx (31 views)
2014-09-07 01:10:19

mitcheeb (39 views)
2014-09-04 23:08:59
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!