你可以向网格中添加图层,只合并在同一图层上的单元格。
如果你想避免重叠的单元格,那么检查是否已经存在于其他图层中。从其他图层查找值还将允许你交叉合并单元格,但会增加很多复杂性。
这是一些伪代码:
enum Layer {
Building,
Wall,
Road
}
struct Point {
int x,y;
}
class Cell {
Point point;
int value;
}
class Cells {
Dictionary<Point, Cell> cells;
Cells(int width, int height) {
for x in width
for y in height
Point point = new Point(x,y);
cells.Add(point, new Cell(point, default))
}
Cell Get(Point point) => cells[point];
}
class Grid {
Dictionary<Layer, Cells> layers;
Grid(int width, int height) {
foreach layer in Layer
layers.Add(layer, new Cells(width, height));
}
Cells GetLayer(Layer layer) {
return layers[layer]
}
int GetValue(Point point, Layer layer) {
return GetLayer(layer).Get(point).value;
}
bool IsValueInAllLayers(Point point, int value) {
foreach layer in Layers
if layer.Get(point).value != value
return false;
return true;
}
}
void Main() {
Grid grid = new Grid(10, 10);
Point mousePoint = new Point(2,2);
if(grid.IsValueInAllLayers(mousePoint, 0)) {
grid.SetValue(mousePoint, Layer.Building);
}
}