Rectangular Arrays

Another Roadside Attraction me at me.com
Thu Feb 22 22:25:48 PST 2007


I'm learning D by writing a logic game with a grid-shaped board. If possible, I'd like to use a rectangular array for the board, but I'd like to define the width of the board at runtime when I instantiate the class. Ideally, I'd like to do something like this:

public class Board {
	
	private Piece[,] grid;
	
	public this(uint width) {
		grid = new Piece[width,width];
	}

}

But that doesn't compile. It looks like the dimensions of rectangular arrays has to be known at compile-time. Is that right, or am I missing something?

I've also tried declaring the array as an ordinary multi-dimensional array, but this doesn't compile either:

public class Board {
	
	private Piece[][] grid;
	
	public this(uint width) {
		grid = new Piece[width][width];
	}

}

What's the best way to write this code? As I expand on this project, I plan on incorporating some time-consuming algorithms (think, n-Queens problem), so I'd prefer to use the most efficent data structure possible for the board.

Thanks!!

--ARA


More information about the Digitalmars-d-learn mailing list