public TileRenderer(Canvas canvas, string displayText, Tile tile) { Tile = tile; AnimationTime = TimeSpan.FromSeconds(0.25); this.canvas = canvas; Width = DefaultWidth; Height = DefaultHeight; rectangle = new Rectangle { HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Stretch, Stroke = new SolidColorBrush(Colors.Black), Fill = new SolidColorBrush(Colors.WhiteSmoke) }; Children.Add(rectangle); text = new TextBlock { Text = displayText, HorizontalAlignment = HorizontalAlignment.Stretch, TextAlignment = TextAlignment.Center, FontSize = 36, FontWeight = FontWeights.Bold, VerticalAlignment = VerticalAlignment.Top, Margin = new Thickness(0, 26, 0, 26) }; Children.Add(text); canvas.Children.Add(this); SetPosition(); tile.PositionChanged += UpdatePosition; }
public Puzzle(int size) { this.size = size; puzzleState = new Tile[size,size]; for (int horizontalPosition = 0; horizontalPosition < size; horizontalPosition++) { for (int verticalPosition = 0; verticalPosition < size; verticalPosition++) { TilePosition position = new TilePosition(horizontalPosition, verticalPosition); Tile tile = new Tile(position); tile.PositionChanged += TilePositionChanged; this[position] = tile; if (horizontalPosition == size - 1 && verticalPosition == size - 1) { hole = tile; } } } }
private void UpdatePosition(Tile sender, TilePosition oldposition, TilePosition newposition) { int left = DefaultWidth * newposition.HComponent; int top = DefaultHeight * newposition.VComponent; AnimationTime = TimeSpan.FromSeconds(0.25); DoubleAnimation horizontalAnimation = new DoubleAnimation { From = Canvas.GetLeft(this), To = left, Duration = AnimationTime }; horizontalAnimation.Completed += StopAnimation; Storyboard.SetTarget(horizontalAnimation, this); Storyboard.SetTargetProperty(horizontalAnimation, new PropertyPath("(Canvas.Left)")); DoubleAnimation verticalAnimation = new DoubleAnimation { From = Canvas.GetTop(this), To = top, Duration = AnimationTime }; horizontalAnimation.Completed += StopAnimation; Storyboard.SetTarget(verticalAnimation, this); Storyboard.SetTargetProperty(verticalAnimation, new PropertyPath("(Canvas.Top)")); storyboard.Children.Add(horizontalAnimation); storyboard.Children.Add(verticalAnimation); storyboard.Begin(); }
private Tile GetNextTile(Tile currentTile, MoveDirection direction) { TilePosition currentPosition = currentTile.CurrentPosition; switch (direction) { case MoveDirection.Up: return puzzleState[currentPosition.HComponent, currentPosition.VComponent - 1]; case MoveDirection.Down: return puzzleState[currentPosition.HComponent, currentPosition.VComponent + 1]; case MoveDirection.Left: return puzzleState[currentPosition.HComponent-1, currentPosition.VComponent]; case MoveDirection.Right: return puzzleState[currentPosition.HComponent +1, currentPosition.VComponent]; default: return null; } }