What data structure to use in my example

I would like to create a component consisting of a board and the corner surrounding it. The size of the board (and therefore the boundaries) is determined at runtime. Some examples (the board is bright and the border is dark): alt text http://img340.imageshack.us/img340/3862/examplegw.png

A board consists of objects of type BoardCell, and a border consists of objects of type BorderCell. The data structure for the board - BoardCell [,] - is a simple two-dimensional array.

How can I imagine the border? I started with something like this:

public BorderCell TopLeft // top left corner cell public BorderCell TopRight // top right corner cell public BorderCell BottomRight // bottom right corner cell public BorderCell BottomLeft // bottom left corner cell public BorderCell[] Top // top border (without corners) public BorderCell[] Bottom // bottom border (without corners) public BorderCell[] Left // left border (without corners) public BorderCell[] Right // right border (without corners) 

I do not like this view of the border, can you offer something better?

Optional: I would like to have the SetSomethingForTheCell method on the border object:

 public void SetSomethingForTheCell(...) 

but with my current data structure I don’t know what to pass as a parameter.

+6
c # data-structures
source share
6 answers

Since it is really trivial to determine if a cell is part of the border or not, just store the cells once and check the cross-border membership when necessary.

A simple way to check if a cell is on the border:

 // assuming that the array is in row-major order... public static bool IsInBorder(this BoardCell[,] board, int x, int y) { return x == board.GetLowerBound(1) || x == board.GetUpperBound(1) || y == board.GetLowerBound(0) || y == board.GetUpperBound(0); } 
+3
source share

I will be tempted to define a common interface or base class for BoardCell and BorderCell (i.e. Cell?) And store them in a (larger) two-dimensional array. This way you can easily access each cell, and it will be pretty easy to determine if the address is a border or a board.

+2
source share

I think you can use a regular 2-dimensional array to represent the entire board (inner board with a border), and you can know the type of each cell if it is on the inner board or on the border using its position (i.e. border cells have a value of x 0 or a width of -1 and a value of y 0 or a height of -1)

+2
source share

Are BoardCell and BorderCell related (or can you make them from a common base class)?

If so, you can use a single BaseCell [,] array, and let the difference between Board and Border be determined by the class type and / or position.


Otherwise, a single-base array for the border: `new BorderCell [2 * rows + 2 * cols + 4];

+2
source share

I haven't used C # after a while, so I can't give you a specific C # answer. However, I would BorderCell be a subclass of BoardCell (or have a common subclass). As you stated, the structure of your data board will then be a two-dimensional array of type BoardCell. When creating a panel instance, create an instance of the internal cells for the regular BoardCells and BorderCells borders.

If it seems to you that you may need to iterate over borders, etc., define an iterator in the Board class for each type of loop (for example, for corners, upper border, lower border, etc.) using the yield operator.

As for your public void SetSomethingForTheCell(...) instead, I would provide attribute / properties for the cell elements. For example:

 BoardCell cell = board.getCell(i,j); cell.setSomething(data) 
+2
source share

You could create, as stated above, the base class of the Cell (or so) and inherit it. For example:

 BoardCell is a cell with 3 values: x, y, bool isBorder Board is a cell with 3 values: x, y, array Cells 
+1
source share

All Articles