Fill in 2d array

Hi,

The 2d int array below:

int testArray[][] = new int[4][5];   

I want filling with the following:

	1111
	1001
	1001
	1001
	1111

What’s the best way to go about this?

Obviously this is how to simply fill in with 1’s:


for(int y=0; y<5; y++)
{
	for(int x=0; x<4;x++) {
		testArray[x][y] = 1;
	}
}

I came up with this, but doesn’t look the best:


private void makeRoom(int[][]testArray, int w, int h, int tileOuterId, int tileInnerId) {
		for(int y=0; y<h; y++)
		{
			for(int x=0; x<w;x++) {
				if(x==0 || x==w-1 || y==0 || y==h-1)
					testArray[x][y] = tileOuterId;
				else
					testArray[x][y] = tileInnerId;
			}
		}
	}

Just would like a method which takes in the dimensions of the array and the outer side of it is filled with 1’s if you know what I mean?

Thanks,
Steve

Is this for homework?

There are quite a few ways to do this, and which approach you take really depends on your personal coding style and what fits in your brain the best.

I personally would probably just use 2 for loops: one to fill up the first and last row, and another to fill the first and last column.

final byte[][] testArray = {
  {1,1,1,1},
  {1,0,0,1},
  {1,0,0,1},
  {1,0,0,1},
  {1,1,1,1}
};

Kevin has given a good answer, so on a less related note: Computers like it when you access memory in a sequential fashion. At the moment, you are accessing memory in a scattered pattern. You should reverse the [X][y] in your loops to [y][X]. While you don’t need the optimization here, I believe it’s a good habit to get in to and it doesn’t really affect the complexity of your code or take away development time. See http://en.wikipedia.org/wiki/CPU_cache for more information.

Hi,

Unfortunately, the array is one big array but filled in at diiferent x,y and different sizes, many of them are created so cannot initialize as you show.

Yeah, I know what you mean about the x,y but guess it is the way my brain works, lol.


private void drawRooms() {
		for (Room rm : rooms) {
			for (int y = rm.y1; y <= rm.y1 + rm.height; y++) {
				for (int x = rm.x1; x <= rm.x1 + rm.width; x++) {
					try {

						// We should really store texture x,y position - THIS WILL NEED DOING!!!
						if(x==rm.x1 || x== rm.x2 || y == rm.y1 || y == rm.y2 )
							tilesArray[x][y].texture = this.wallEdge;
						else
							tilesArray[x][y].texture = this.pathTexture;				
						tilesArray[x][y].visible = true;
						tilesArray[x][y].name = "Room:" + rm.roomNo;
					} catch (Exception e) {
					}
				}
			}
		}
	}

The above fills in my tilesArray at various positions - the tilesArray is of type Tile which has a texture associated with it (will need to put texture x,y in here at
some point instead of having lots of separate textures in lots of files). What the code does is put walls around the rooms which are all procedurally generated. What
I need to work out now is to place doors where the corridors connect the rooms!

Thanks,
Steve

My [row][column] ordered tweak. Just a mock-up re-interpretation though: ;D

void drawRooms() {
  for (Room r : rooms) {
    int xx = Math.min(r.x1 + r.w, tiles[0].length - 1);
    int yy = Math.min(r.y1 + r.h, tiles.length - 1);

    for (int y = r.y1; y <= yy; ++y) {
      Tile[] t = tiles[y];

      for (int x = r.x1; x <= xx; ++x) {
        t[x].texture = x == r.x1 | x == r.x2 | y == r.y1 | y == r.y2
          ? Texture.WALL_EDGE : Texture.PATH_TEXTURE;

        t[x].visible = true;

        t[x].name = "Room: " + r.room;
      }
    }
  }
}

And the “full” mock-up using http://Processing.org for a faster prototype: ::slight_smile:

static final int QTY = 10;
Room[] rooms = new Room[QTY];

static final int COLS = 30, ROWS = 10;
Tile[][] tiles = new Tile[ROWS][COLS];

void setup() {
  for (int i = 0; i != QTY; rooms[i++] = new Room());
  for (Tile[] t : tiles)  for (int i = 0; i != COLS; t[i++] = new Tile());

  drawRooms();
  println("Done!");
  exit();
}

class Room {
  int x1, x2, y1, y2, w = 50, h = 80, room;
}

class Tile {
  String name;
  int texture;
  boolean visible;
}

class Texture {
  static final int WALL_EDGE = 0, PATH_TEXTURE = 1;
}

Many thanks :slight_smile:

I’m just struggling now with putting doors where corridors would connect…The corridors go to the centre of the rooms (x,y) they are connecting too, some corridors go
straight through a room as a room it is connecting to is on the other-side of another room, can you see my dilema?

Any advice is appreciated. Please see screenshot here: https://sites.google.com/site/dungeonplanetlite/

I need to remove corridors from within rooms and then place doors…

Thanks,
Steve

PS: this is the article I followed and coded it in Java : http://gamedevelopment.tutsplus.com/tutorials/create-a-procedurally-generated-dungeon-cave-system--gamedev-10099

how are you creating the paths?

Paths are created by taking a room say room 1 which then connects to room 2, room 2 connects to room 3 etc.
I take the center x,y of each room and draw horizontal and vertical paths.

This is the code to do the corridors:


// Create a vertical corridor
	private void create_v_tunnel(int y1, int y2, int x) {
		int amount = Math.max(y1 - y2, y2 - y1);
		int yy1 = Math.min(y1, y2);
		for (int y = 0; y <= amount; y++) {
			tilesArray[x][yy1 + y].visible = false;
			tilesArray[x][yy1 + y].name = "V Corridor";
			tilesArray[x][yy1 + y].texture = this.floor1;
		}
	}

	// Create a horizontal corridor
	private void create_h_tunnel(int x1, int x2, int y) {
		int amount = Math.max(x1 - x2, x2 - x1);
		int xx1 = Math.min(x1, x2);
		for (int x = 0; x <= amount; x++) {
			tilesArray[xx1 + x][y].visible = false;
			tilesArray[xx1 + x][y].name = "Hz Corridor";
			tilesArray[xx1 + x][y].texture = this.floor2;
		
		}
			
	}

and called from:


	// Carve out corridors - connect rooms to other rooms
	private void drawCorridors() {
		for (int i = 0; i < rooms.size(); i++) {
			Room r01, r02;
			try {
				r01 = rooms.get(i);
				r02 = rooms.get(i + 1);
			} catch (Exception ex) {
				return;
			}

			Random r = new Random();
			try {
					this.create_h_tunnel(r01.centerX, r02.centerX, r02.centerY);
					this.create_v_tunnel(r01.centerY, r02.centerY, r01.centerX);
			} catch (Exception ex) {
			}
		}
	}


Thanks,
Steve

Any reason for the (empty ) exception handler ?

i do’nt know what you’re actually doing there…
but you only need the start points of the paths; then you can put doors there… where is the problem?

I’ve got a feeling those are supposed to catch ArrayIndexOutOfBoundsException.
In my cleaner mock up example, I’ve relied on Math.min() to avoid getting past array’s upper bound index!

Exceptions will be tidied up later - yes Array index out of bounds ones.

Start points of the paths are the centre of the rooms. Good suggestion though.

PS - how do you embed images in here?

There really are not many different ways of doing this , they all relatively involve similar elements. Unless of course you want to do something horrendously convoluted like such: http://www.infiltec.com/j-h-wrld.htm

Did get it working, but still bug when corridor crosses through a room…will get it fixed hopefully!