Ejemplo n.º 1
0
        private RowPieces BuildDataItem(int pieceIndex, RotatedPiece rotatedPiece, Coords coords)
        {
            var numColumns = _pieces.Length + _board.BoardSize * _board.BoardSize;
            var matrixRow  = new bool[numColumns];

            matrixRow[pieceIndex] = true;

            var w = rotatedPiece.Width;
            var h = rotatedPiece.Height;

            for (var pieceX = 0; pieceX < w; pieceX++)
            {
                for (var pieceY = 0; pieceY < h; pieceY++)
                {
                    if (rotatedPiece.SquareAt(pieceX, pieceY) == null)
                    {
                        continue;
                    }
                    var boardX = coords.X + pieceX;
                    var boardY = coords.Y + pieceY;
                    var boardLocationColumnIndex = _pieces.Length + (_board.BoardSize * boardX) + boardY;
                    matrixRow[boardLocationColumnIndex] = true;
                }
            }

            return(new RowPieces(matrixRow, new PiecePlacement(rotatedPiece, coords)));
        }
Ejemplo n.º 2
0
        private const double BorderWidth = 8; // half of this width will be clipped away

        public PieceControl(RotatedPiece rotatedPiece, double squareSize)
        {
            _rotatedPiece = rotatedPiece;
            _squareSize   = squareSize;
            InitializeComponent();

            Width  = squareSize * rotatedPiece.Width;
            Height = squareSize * rotatedPiece.Height;

            var clipGeometryGroup = new GeometryGroup();
            var outsideEdges      = new List <Coords>();

            for (var px = 0; px < rotatedPiece.Width; px++)
            {
                for (var py = 0; py < rotatedPiece.Height; py++)
                {
                    var square = rotatedPiece.SquareAt(px, py);
                    if (square != null)
                    {
                        var rect      = new Rect(px * squareSize, (rotatedPiece.Height - py - 1) * squareSize, squareSize, squareSize);
                        var rectangle = new Rectangle {
                            Width = rect.Width, Height = rect.Height
                        };
                        Canvas.SetLeft(rectangle, rect.Left);
                        Canvas.SetTop(rectangle, rect.Top);
                        rectangle.Fill = new SolidColorBrush(square.Colour == Colour.Black ? Colors.Black : Colors.White);
                        PieceCanvas.Children.Add(rectangle);
                        var clipRectangleGeometry = new RectangleGeometry(rect);
                        clipGeometryGroup.Children.Add(clipRectangleGeometry);
                        DetermineOutsideEdges(outsideEdges, px, py);
                    }
                }
            }

            var combinedOutsideEdges  = CombineOutsideEdges(outsideEdges);
            var outsideEdgeLinePoints = CalculateEdgeLinePoints(combinedOutsideEdges);

            var polyLineSegment = new PolyLineSegment(outsideEdgeLinePoints, true);
            var pathFigure      = new PathFigure {
                StartPoint = outsideEdgeLinePoints.First()
            };

            pathFigure.Segments.Add(polyLineSegment);
            var pathGeometry = new PathGeometry();

            pathGeometry.Figures.Add(pathFigure);
            var path = new Path
            {
                Stroke           = new SolidColorBrush(Color.FromRgb(0x00, 0x66, 0xCC)),
                StrokeThickness  = BorderWidth,
                StrokeEndLineCap = PenLineCap.Square,
                Data             = pathGeometry
            };

            PieceCanvas.Children.Add(path);
            PieceCanvas.Clip = clipGeometryGroup;
        }
Ejemplo n.º 3
0
        private void DetermineOutsideEdges(ICollection <Coords> outsideEdges, int x, int y)
        {
            var pieceWidth  = _rotatedPiece.Width;
            var pieceHeight = _rotatedPiece.Height;

            foreach (var side in Enum.GetValues(typeof(Side)).Cast <Side>())
            {
                var isOutsideEdge = false;

                switch (side)
                {
                case Side.Top:
                    if (y + 1 >= pieceHeight)
                    {
                        isOutsideEdge = true;
                    }
                    else
                    {
                        if (_rotatedPiece.SquareAt(x, y + 1) == null)
                        {
                            isOutsideEdge = true;
                        }
                    }
                    if (isOutsideEdge)
                    {
                        outsideEdges.Add(new Coords(x, y + 1));
                        outsideEdges.Add(new Coords(x + 1, y + 1));
                    }
                    break;

                case Side.Right:
                    if (x + 1 >= pieceWidth)
                    {
                        isOutsideEdge = true;
                    }
                    else
                    {
                        if (_rotatedPiece.SquareAt(x + 1, y) == null)
                        {
                            isOutsideEdge = true;
                        }
                    }
                    if (isOutsideEdge)
                    {
                        outsideEdges.Add(new Coords(x + 1, y + 1));
                        outsideEdges.Add(new Coords(x + 1, y));
                    }
                    break;

                case Side.Bottom:
                    if (y == 0)
                    {
                        isOutsideEdge = true;
                    }
                    else
                    {
                        if (_rotatedPiece.SquareAt(x, y - 1) == null)
                        {
                            isOutsideEdge = true;
                        }
                    }
                    if (isOutsideEdge)
                    {
                        outsideEdges.Add(new Coords(x + 1, y));
                        outsideEdges.Add(new Coords(x, y));
                    }
                    break;

                case Side.Left:
                    if (x == 0)
                    {
                        isOutsideEdge = true;
                    }
                    else
                    {
                        if (_rotatedPiece.SquareAt(x - 1, y) == null)
                        {
                            isOutsideEdge = true;
                        }
                    }
                    if (isOutsideEdge)
                    {
                        outsideEdges.Add(new Coords(x, y));
                        outsideEdges.Add(new Coords(x, y + 1));
                    }
                    break;
                }
            }
        }
 private bool VerifySquareIsNull(RotatedPiece rotatedPiece, int x, int y)
 {
     return rotatedPiece.SquareAt(x, y) == null;
 }
        private bool VerifySquareDetails(RotatedPiece rotatedPiece, int x, int y, Colour colour)
        {
            var square = rotatedPiece.SquareAt(x, y);

            return
                square != null &&
                square.X == x &&
                square.Y == y &&
                square.Colour == colour;
        }