编辑代码

/// 3*3 9宫格固定迷宫路径地图
final List<MapItem> map_list = [
    MapItem(fromIndex:1, pathType:MapType.Right, toIndex:2, stateType: MapType.Start),
    MapItem(fromIndex:2, pathType:MapType.Bottom, toIndex:5, stateType: MapType.Road),
    MapItem(fromIndex:5, pathType:MapType.Left, toIndex:4, stateType: MapType.Road),
    MapItem(fromIndex:5, pathType:MapType.Right, toIndex:6, stateType: MapType.Road),
    MapItem(fromIndex:6, pathType:MapType.Top, toIndex:3, stateType: MapType.Road),
    MapItem(fromIndex:8, pathType:MapType.Left, toIndex:7, stateType: MapType.Road),
    MapItem(fromIndex:8, pathType:MapType.Right, toIndex:9, stateType: MapType.End)
];

void main() {
    MazeOptionHandler().init(x:3, y:3, mapList:map_list);
}

class MazeOptionHandler {
    int x = 0;
    int y = 0;
    List<MapItem> _mapList = [];

    List<List<ModelItem>> _dataList = [];

    init({int x,int y,List<MapItem> mapList}) {
        this.x = x;
        this.y = y;
        this._mapList = mapList;

        _createItems();
    }

    increment() {}
     
    /// 创建迷宫格
    _createItems(){
        int index = 1;
        List<ModelItem> items = [];
        for(int i = 0; i < x; i++){
            List<ModelItem> rows = [];
            for(int j = 0; j < y; j++){
                print("$i:$j");
                ModelItem item = ModelItem(xy: [i+1,j+1], index: index++);
                rows.add(item);
                items.add(item);
            }
            _dataList.add(rows);
        }
        _mapList.forEach((MapItem mapItem){
            int fromIndex = mapItem.fromIndex;
            MapType pathType = mapItem.pathType;
            ModelItem item = items[fromIndex -1];
            if(pathType == MapType.Left) item.leftNode = item;
            ///else if(pathType == MapType.Right) item.rightNode = item;
            ///else if(pathType == MapType.Top) item.topNode = item;
            ///else if(pathType == MapType.Bottom) item.bottomNode = item;
        });

        ///print(_dataList);

    }
}

class ModelItem {
  /// Top Left, Top Right, Bottom Left, Bottom Right
  List<int> point = [0, 0, 0, 0];

  /// X, Y
  List<int> xy = [0, 0];

  int index = 0;

  /// Weight, Height
  int size = 0;

  /// 是否被选中
  bool selected = false;

  bool isStart = false;

  bool isEnd = false;

  ModelItem topNode;
  ModelItem leftNode;
  ModelItem rightNode;
  ModelItem bottomNode;

  ModelItem({this.xy,
    this.index,
    this.leftNode,
    this.rightNode,
    this.topNode,
    this.bottomNode});

  @override
  toString(){
      return {
          "xy": xy,
          "index": index,
          "topNode": topNode,
          "leftNode": leftNode,
          "rightNode": rightNode,
          "bottomNode": bottomNode
      }.toString();
  }
}

/// 地图类型
enum MapType{
    /// 向左
    Left,
    /// 向右
    Right,
    /// 向上
    Top,
    /// 向下
    Bottom,
    /// 开始
    Start,
    /// 结束
    End,
    /// 道路
    Road
}

/// 地图项
class MapItem {
    int fromIndex;
    MapType pathType;
    int toIndex;
    MapType stateType;

    MapItem({this.fromIndex,this.pathType,this.toIndex,this.stateType});

    @override
    toString(){
        return {
            "fromIndex": fromIndex,
            "pathType": pathType,
            "toIndex": toIndex,
            "stateType": stateType
        }.toString();
    }
}