# Designing a tiling grid

I am implementing a grid-like game. The initial state is shown here:

``````+++===+++===+++===+++===+++===+++
|     |     |     |     |     |
|     |     |     |     |     |
+++===+++===+++===+++===+++===+++
|     |     |     |     |     |
|     |     |     |     |     |
+++===+++===+++===+++===+++===+++
|     |     |     |     |     |
|     |     |     |     |     |
+++===+++===+++===+++===+++===+++
|     |     |     |     |     |
|     |     |     |     |     |
+++===+++===+++===+++===+++===+++
|     |     |     |     |     |
|     |     |     |     |     |
+++===+++===+++===+++===+++===+++
``````

The user can select any of the borders of a cell. Let's say the coordinates are left to right, top to bottom:

``````(1,1) (2,1) (3,1) (4,1) (5,1)
(1,2) (2,2) (3,2) (4,2) (5,2)
(1,3) (2,3) (3,3) (4,3) (5,3)
(1,4) (2,4) (3,4) (4,4) (5,4)
(1,5) (2,5) (3,5) (4,5) (5,5)
``````

If the user selects the left border of `(2,2)`, it is the same as selecting the right border of `(1,2)`:

``````+++===+++===+++===+++===+++===+++
|     |     |     |     |     |
|     |     |     |     |     |
+++===+++===+++===+++===+++===+++
|    |||    |     |     |     |
|    |||    |     |     |     |
+++===+++===+++===+++===+++===+++
|     |     |     |     |     |
|     |     |     |     |     |
+++===+++===+++===+++===+++===+++
|     |     |     |     |     |
|     |     |     |     |     |
+++===+++===+++===+++===+++===+++
|     |     |     |     |     |
|     |     |     |     |     |
+++===+++===+++===+++===+++===+++
``````

What is a good way to design this in an object-oriented fashion?

I could have a `Board` object with many `Cell` objects inside:

``````class Board {
int rows
int cols
Cell cells[cols][rows]
}

class cells {
// Bit masks:
// 0000 = no sides selected
// 0001 = top selected
// 0010 = right selected
// 0100 = bottom selected
// 1000 = left selected
bitset selectedSides[4];
}
``````

However, this design means I have to check `rightSideSelected(1,2) || leftSideSelected(2,2)` to know if that border is selected.

I could also design it so that I only have a `Board` object:

``````class Board {
int rows
int cols
bool selected[cols+1][rows+1]
}
``````

If the user selects the left border of `(2,2)`, then `selected[2][2] = true` (using one-indexed arrays). I feel this is not really object-oriented, but I could be wrong.

This second approach will break down if I change the square cell to a triangle cell or hexagon cell.

## 2 Answers

Don't be fooled by the fact that your visual system tells you there are a lot of squares (cells) in this scenario. As you state the problem, the elements that matter are the edges. Therefore your objects should correspond to edges, not cells.

If you later need a concept of cells, e.g. to decide whether a cell is entirely enclosed in selected borders, that is relatively easy to add on, while thinking in cells from the beginning gives you problems right away, as you have seen.

• This is a viewpoint I had not considered. How would the concept of cells be realized? I can see how Sherwood Botsford's solution uses edges as objects and also how it can implement the concept of cells. Is this similar to what you had in mind? Dec 3, 2019 at 5:28

Number the vertexes. An edge then becomes a 2-ple of vertices. A cell then becomes a tuple of vertices, where the order is the number of sides a cell has.

This can later be expanded to have tuples that surround multiple cells. If your tuples are directed graphs, you can use analytical geometry to figure out if a point is inside or outside a compound cell.

• "A cell then becomes a tuple of vertices" - do you mean a tuple of edges? Dec 3, 2019 at 5:25