Cube.of constructor

Cube.of(
  1. List<Color> definition
)

Creates a Cube from the your definition colors U..R..F..D..L..B.

Implementation

factory Cube.of(List<Color> definition) {
  if (definition.length != 54) {
    throw ArgumentError('Invalid definition');
  }

  definition = Rotation.correctOrientation(definition);

  final cp = List.filled(Corner.count, Corner.upRightFront);
  final co = List.filled(Corner.count, 0);
  final ep = List.filled(Edge.count, Edge.upRight);
  final eo = List.filled(Edge.count, 0);

  for (var i = 0; i < Corner.count; i++) {
    var ori = 0;
    // get the colors of the cube at corner i, starting with U/D
    for (; ori < 3; ori++) {
      if (definition[_cornerFacelet[i][ori].index] == Color.up ||
          definition[_cornerFacelet[i][ori].index] == Color.down) {
        break;
      }
    }

    final a = definition[_cornerFacelet[i][(ori + 1) % 3].index];
    final b = definition[_cornerFacelet[i][(ori + 2) % 3].index];

    for (var j = 0; j < Corner.count; j++) {
      if (a == _cornerColor[j][1] && b == _cornerColor[j][2]) {
        cp[i] = Corner.values[j];
        co[i] = ori % 3;
        break;
      }
    }
  }

  for (var i = 0; i < Edge.count; i++) {
    for (var j = 0; j < Edge.count; j++) {
      if (definition[_edgeFacelet[i][0].index] == _edgeColor[j][0] &&
          definition[_edgeFacelet[i][1].index] == _edgeColor[j][1]) {
        ep[i] = Edge.values[j];
        eo[i] = 0;
        break;
      }

      if (definition[_edgeFacelet[i][0].index] == _edgeColor[j][1] &&
          definition[_edgeFacelet[i][1].index] == _edgeColor[j][0]) {
        ep[i] = Edge.values[j];
        eo[i] = 1;
        break;
      }
    }
  }

  return Cube._(cp: cp, co: co, ep: ep, eo: eo);
}