public Wall(Cell[] cells, Orientation orientation) { if (cells == null) { throw new ArgumentNullException(nameof(cells)); } if (cells.Length != 4) { throw new ArgumentException("There must be exactly 4 cells in the array"); } Cells = new Cell[4]; Array.Copy(cells, Cells, 4); Array.Sort(Cells); Orientation = orientation; if (Orientation == Orientation.Vertical) { SegmentA = new WallSegment(Cells[0], Cells[2]); SegmentB = new WallSegment(Cells[1], Cells[3]); } else { SegmentA = new WallSegment(Cells[0], Cells[1]); SegmentB = new WallSegment(Cells[2], Cells[3]); } }
/// <summary> /// Creates Wall from short string description /// </summary> /// /// <param name="wallDescription"> /// 3-character description in the format "xyO", /// where 'x' and 'y' - coordinates of the lower left cell out of 4, /// and 'O' is a wall orientation (can be either 'v' or 'h') /// </param> public Wall(String wallDescription) { if (!IsCorrectWallDescription(wallDescription)) { throw new ArgumentException("Incorrect wall description"); } var x = int.Parse(wallDescription.Substring(0, 1)); var y = int.Parse(wallDescription.Substring(1, 1)); var dx = new[] { 0, 0, 1, 1 }; var dy = new[] { 0, 1, 0, 1 }; var cellsList = new List <Cell>(); for (var i = 0; i < 4; ++i) { cellsList.Add(new Cell(x + dx[i], y + dy[i])); } Cells = cellsList.ToArray(); switch (wallDescription[2]) { case 'v': Orientation = Orientation.Vertical; break; case 'h': Orientation = Orientation.Horizontal; break; default: Orientation = Orientation.Unknown; break; } if (Orientation == Orientation.Vertical) { SegmentA = new WallSegment(Cells[0], Cells[2]); SegmentB = new WallSegment(Cells[1], Cells[3]); } else { SegmentA = new WallSegment(Cells[0], Cells[1]); SegmentB = new WallSegment(Cells[2], Cells[3]); } }
public Wall(WallSegment segmentA, WallSegment segmentB) { if (segmentA == null) { throw new ArgumentNullException(nameof(segmentA)); } if (segmentB == null) { throw new ArgumentNullException(nameof(segmentB)); } if (!WallSegment.AreAdjacent(segmentA, segmentB)) { throw new ArgumentException("Wall segments are not adjacent"); } Orientation = segmentA.Orientation; if (Orientation == Orientation.Vertical) { if (segmentA.Cell1.Y > segmentB.Cell1.Y) { Helper.Swap(ref segmentA, ref segmentB); } } else { if (segmentA.Cell1.X > segmentB.Cell1.X) { Helper.Swap(ref segmentA, ref segmentB); } } SegmentA = segmentA; SegmentB = segmentB; Cells = new[] { segmentA.Cell1, segmentA.Cell2, segmentB.Cell1, segmentB.Cell2 }; Array.Sort(Cells); }