[solved] Generate map[][] (xTiles&yTiles) vs hard-coding map[][]

Hey JGO, at the moment I’m trying to use a 2d array to create a list of tiles for my games pathfinding library.

I’m trying to figure out how to easily create a 2d array of tiles VS hard-coding them into an array.

NOTE: 2D Arrays are confusing for me as I’ve only dealt with lists and regular arrays[].

Here’s what I’m talking about:


private static GridCell[][] createCells() {
	// GridCell[y][x]
	GridCell[][] cells = new GridCell[][] {
		// new GridCell(column, row, walkableOrNot)
		{ new GridCell(0, 0, true), new GridCell(0, 1, true), new GridCell(0, 2, true) }, // row 0
		{ new GridCell(1, 0, true), new GridCell(1, 1, true), new GridCell(1, 2, true) }, // row 1
		{ new GridCell(2, 0, true), new GridCell(2, 1, true), new GridCell(2, 2, true) }, // row 2
		{ new GridCell(3, 0, true), new GridCell(3, 1, true), new GridCell(3, 2, true) } // row 3
	};
	return cells;
}

How could I do something like:


private static GridCell[][] createCells() {
	int columns = 10;
	int rows = 10;
	GridCell[][] cells = new GridCell[columns][rows];

	// Some for loop to generate a # of XTiles & YTiles
	// and add them to our cells [][]
	// I have no idea  how to do this XD

	return cells;
}

Thanks for any feedback/help, I don’t think I’d like to layout my whole gamemap in a hard-coded array :stuck_out_tongue:

Nested loop: for i, for j create cell (i, j, …)

Do look up looping thingers.

Solved it, thanks :slight_smile:

Didn’t think it would be that easy lol.


private static GridCell[][] createCells() {
	int columns = 10;
	int rows = 10;
	GridCell[][] cells = new GridCell[columns][rows];
	for (int y = 0; y < columns; y++) {
		for (int x = 0; x < rows; x++) {
			cells[y][x] = new GridCell(y, x, true);
		}
	}
	return cells;
}