views:

128

answers:

6

I'd like to create a component, which consist from a board and its surrounding corner. The size of the board (and therefore also of the border) is defined at run-time. Some examples (board is bright and border is dark): alt text

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

How can I represent 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 don't like this representation of the border, can you suggest something better?

Additional: I'd like to have a method SetSomethingForTheCell on the border object:

public void SetSomethingForTheCell(...)

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

+2  A: 

I would be tempted to define a common interface or base class for BoardCell and BorderCell (i.e. Cell?) and keep them in a (bigger) two dimensional array. This way you can easily address every cell, and it will be rather easy to determine if the address is a border or board.

Grzenio
+2  A: 

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

Ahmed Said
+2  A: 

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

If that is the case you could use a single BaseCell[,] array and let the distinction between Board and Border be defined by class type and/or position.


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

Henk Holterman
BoardCell and BorderCell have nothing in common, except that they both are called a cell :) But yes, I might be overcomplicating and just use one array...
_simon_
+2  A: 

I haven't used C# in some time, so I can't give you a c# specific answer. However I would have BorderCell be a subclass of BoardCell (or to have a common subclass). As you stated your board data structure would then be a 2D array of type BoardCell. When instantiating the board, instantiate the inner cells to normal BoardCells and the boundary to BorderCells.

If you think you might need to loop over borders etc, define an iterator in the Board class for each type of loop (ex. for corners, top border, bottom border, etc) using the yield operator.

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

BoardCell cell = board.getCell(i,j);
cell.setSomething(data)
Il-Bhima
+1  A: 

You could create, as said above, a base class called Cell (or so), and inherit from that. For example:

BoardCell is a cell with 3 values: x, y, bool isBorder
Board is a cell with 3 values: x, y, array Cells
daemonfire300
+2  A: 

Since it's really trivial to detect whether a cell is part of the border or not, just store the cells once and test for border membership when required.

A simple method to test whether a cell is in 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);
}
Christian Hayter