private void SetSides(Puzzle[,] result, PuzzleDirection firstDirection, PuzzleDirection secondDirection) { int i = 0; int j = 0; int firstN = firstDirection == PuzzleDirection.Right ? 0 : result.GetLength(1) - 1; int secondN = secondDirection == PuzzleDirection.Bottom ? 0 : result.GetLength(0) - 1; while (j != _n - 2 || i != _n - 2) { var flatItems = _puzzles.Where(x => !x.IsUsed && (x.Type == PuzzleType.SideFlatHorizontal || x.Type == PuzzleType.SideFlatVertical)).ToList(); foreach (var item in flatItems.Where(x => x.Direction == firstDirection)) { if (FitsWith(result[i, firstN], item, PuzzleDirection.Bottom)) { result[++i, firstN] = item; result[i, firstN].IsUsed = true; } } foreach (var item in flatItems.Where(x => x.Direction == secondDirection)) { if (FitsWith(result[secondN, j], item, PuzzleDirection.Right)) { result[secondN, ++j] = item; result[secondN, j].IsUsed = true; } } } }
public Puzzle(PuzzleType type, PuzzleDirection direction) { Type = type; Direction = direction; }
public bool FitsWith(Puzzle first, Puzzle second, PuzzleDirection secondRelativeToFirstDirection) { if (first == null || second == null) { throw new ArgumentNullException(); } if (first.Type == PuzzleType.Corner) { if (first.Direction == PuzzleDirection.Bottom) { if (second.Type == PuzzleType.SideFlatHorizontal && second.Direction == PuzzleDirection.Bottom && secondRelativeToFirstDirection == PuzzleDirection.Right) { return(true); } if (second.Type == PuzzleType.SideFlatVertical && second.Direction == PuzzleDirection.Right && secondRelativeToFirstDirection == PuzzleDirection.Bottom) { return(true); } } if (first.Direction == PuzzleDirection.Left) { if (second.Type == PuzzleType.SideFlatHorizontal && second.Direction == PuzzleDirection.Left && secondRelativeToFirstDirection == PuzzleDirection.Bottom) { return(true); } if (second.Type == PuzzleType.SideFlatVertical && second.Direction == PuzzleDirection.Bottom && secondRelativeToFirstDirection == PuzzleDirection.Left) { return(true); } } if (first.Direction == PuzzleDirection.Top) { if (second.Type == PuzzleType.SideFlatHorizontal && second.Direction == PuzzleDirection.Top && secondRelativeToFirstDirection == PuzzleDirection.Left) { return(true); } if (second.Type == PuzzleType.SideFlatVertical && second.Direction == PuzzleDirection.Left && secondRelativeToFirstDirection == PuzzleDirection.Top) { return(true); } } if (first.Direction == PuzzleDirection.Right) { if (second.Type == PuzzleType.SideFlatHorizontal && second.Direction == PuzzleDirection.Right && secondRelativeToFirstDirection == PuzzleDirection.Top) { return(true); } if (second.Type == PuzzleType.SideFlatVertical && second.Direction == PuzzleDirection.Top && secondRelativeToFirstDirection == PuzzleDirection.Right) { return(true); } } } if ((first.Type == PuzzleType.SideFlatVertical && second.Type == PuzzleType.SideFlatHorizontal) || (first.Type == PuzzleType.SideFlatHorizontal && second.Type == PuzzleType.SideFlatVertical)) { if (secondRelativeToFirstDirection == PuzzleDirection.Bottom || secondRelativeToFirstDirection == PuzzleDirection.Top) { if (first.Direction == PuzzleDirection.Right && second.Direction == PuzzleDirection.Right) { return(true); } if (first.Direction == PuzzleDirection.Left && second.Direction == PuzzleDirection.Left) { return(true); } } else { if (first.Direction == PuzzleDirection.Bottom && second.Direction == PuzzleDirection.Bottom) { return(true); } if (first.Direction == PuzzleDirection.Top && second.Direction == PuzzleDirection.Top) { return(true); } } } if (second.Type == PuzzleType.Normal) { if (first.Type == PuzzleType.SideFlatVertical) { if (first.Direction == PuzzleDirection.Right && second.Direction == PuzzleDirection.Top && secondRelativeToFirstDirection == PuzzleDirection.Right) { return(true); } if (first.Direction == PuzzleDirection.Left && second.Direction == PuzzleDirection.Top && secondRelativeToFirstDirection == PuzzleDirection.Left) { return(true); } if (first.Direction == PuzzleDirection.Bottom && second.Direction == PuzzleDirection.Right && secondRelativeToFirstDirection == PuzzleDirection.Bottom) { return(true); } if (first.Direction == PuzzleDirection.Top && second.Direction == PuzzleDirection.Right && secondRelativeToFirstDirection == PuzzleDirection.Top) { return(true); } } if (first.Type == PuzzleType.SideFlatHorizontal) { if (first.Direction == PuzzleDirection.Bottom && second.Direction == PuzzleDirection.Top && secondRelativeToFirstDirection == PuzzleDirection.Bottom) { return(true); } if (first.Direction == PuzzleDirection.Top && second.Direction == PuzzleDirection.Top && secondRelativeToFirstDirection == PuzzleDirection.Top) { return(true); } if (first.Direction == PuzzleDirection.Left && second.Direction == PuzzleDirection.Right && secondRelativeToFirstDirection == PuzzleDirection.Left) { return(true); } if (first.Direction == PuzzleDirection.Right && second.Direction == PuzzleDirection.Right && secondRelativeToFirstDirection == PuzzleDirection.Right) { return(true); } } if (first.Type == PuzzleType.Normal && second.Type == PuzzleType.Normal) { if (secondRelativeToFirstDirection == PuzzleDirection.Top) { if (second.Direction == PuzzleDirection.Right && first.Direction == PuzzleDirection.Top) { return(true); } if (second.Direction == PuzzleDirection.Top && first.Direction == PuzzleDirection.Right) { return(true); } } if (secondRelativeToFirstDirection == PuzzleDirection.Right) { if (second.Direction == PuzzleDirection.Right && first.Direction == PuzzleDirection.Top) { return(true); } if (second.Direction == PuzzleDirection.Top && first.Direction == PuzzleDirection.Right) { return(true); } } } } return(false); }