示例#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)));
        }
示例#2
0
        public void FullRotations(params string[] rotationNames)
        {
            var rotationType = typeof(Rotation);
            var rotations    = rotationNames.Select(rn => Enum.Parse(rotationType, rn)).Cast <Rotation>().ToArray();
            var rotatedPiece = new RotatedPiece(_piece, rotations);

            CollectionAssert.AreEqual(_piece.OccupiedSquares, rotatedPiece.OccupiedSquares);
        }
示例#3
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;
        }
        public void RotatedPiece_ForTestPieceWithGivenOrientation_HasCorrectWidthAndHeight(Orientation orientation, int expectedWidth, int expectedHeight)
        {
            // Arrange, Act
            var rotatedPiece = new RotatedPiece(_piece, orientation);

            // Assert
            Assert.That(rotatedPiece.Width, Is.EqualTo(expectedWidth));
            Assert.That(rotatedPiece.Height, Is.EqualTo(expectedHeight));
        }
        public void SquareAt_ForTestPieceWithSouthOrientation_ReturnsCorrectSquareDetailsForAllValidCoordinates()
        {
            // Arrange, Act
            var rotatedPiece = new RotatedPiece(_piece, Orientation.South);

            // Assert
            Assert.That(VerifySquareDetails(rotatedPiece, 0, 0, Colour.White), Is.True);
            Assert.That(VerifySquareIsNull(rotatedPiece, 0, 1), Is.True);
            Assert.That(VerifySquareDetails(rotatedPiece, 1, 0, Colour.Black), Is.True);
            Assert.That(VerifySquareIsNull(rotatedPiece, 1, 1), Is.True);
            Assert.That(VerifySquareDetails(rotatedPiece, 2, 0, Colour.White), Is.True);
            Assert.That(VerifySquareDetails(rotatedPiece, 2, 1, Colour.Black), Is.True);
        }
示例#6
0
        public void RotatedX90Cw()
        {
            var rotatedPiece = new RotatedPiece(_piece, Rotation.X90Cw);

            AssertRotatedPiece(
                rotatedPiece,
                new[]
            {
                new Coords(0, 2, 0),
                new Coords(0, 2, 1),
                new Coords(1, 2, 1),
                new Coords(1, 1, 1),
                new Coords(1, 0, 1)
            });
        }
示例#7
0
        private void AddPiece(int pieceIndex, Piece piece, Orientation orientation)
        {
            var rotatedPiece = new RotatedPiece(piece, orientation);

            for (var x = 0; x < _board.BoardSize; x++)
            {
                for (var y = 0; y < _board.BoardSize; y++)
                {
                    _board.Reset();
                    if (!_board.PlacePieceAt(rotatedPiece, x, y))
                    {
                        continue;
                    }
                    var dataItem = BuildDataItem(pieceIndex, rotatedPiece, new Coords(x, y));
                    _data.Add(dataItem);
                }
            }
        }
示例#8
0
        public void AddPiece(RotatedPiece rotatedPiece, int x, int y)
        {
            if (IsPieceOnBoard(rotatedPiece.Piece.Name))
            {
                throw new InvalidOperationException(string.Format("Attempt to add a piece that is already on the board - \"{0}\"!", rotatedPiece.Piece.Name));
            }

            var aw = ActualWidth;
            var ah = ActualHeight;
            var sw = (aw - GridLineThickness) / Count;
            var sh = (ah - GridLineThickness) / Count;

            var pieceControl = new PieceControl(rotatedPiece, sw);

            Canvas.SetLeft(pieceControl, x * sw + GridLineHalfThickness);
            Canvas.SetBottom(pieceControl, y * sh + GridLineHalfThickness);
            BoardCanvas.Children.Add(pieceControl);
            _pieceDetails[rotatedPiece.Piece.Name] = Tuple.Create(rotatedPiece.Orientation, x, y, pieceControl);
        }
示例#9
0
文件: Solver.cs 项目: pdwetz/DlxLib
        private void AddDataItemsForPieceWithSpecificOrientation(int pieceIndex, Piece piece, Orientation orientation)
        {
            var rotatedPiece = new RotatedPiece(piece, orientation);

            for (var x = 0; x < _board.BoardSize; x++)
            {
                for (var y = 0; y < _board.BoardSize; y++)
                {
                    _board.Reset();
                    _board.ForceColourOfSquareZeroZeroToBeWhite();
                    if (!_board.PlacePieceAt(rotatedPiece, x, y))
                    {
                        continue;
                    }
                    var dataItem = BuildDataItem(pieceIndex, rotatedPiece, new Coords(x, y));
                    _data.Add(dataItem);
                }
            }
        }
示例#10
0
 public PiecePlacement(RotatedPiece rotatedPiece, Coords coords)
 {
     RotatedPiece = rotatedPiece;
     Coords       = coords;
 }
示例#11
0
 private static void AssertRotatedPiece(
     RotatedPiece rotatedPiece,
     IEnumerable <Coords> occupiedSquares)
 {
     CollectionAssert.AreEquivalent(occupiedSquares, rotatedPiece.OccupiedSquares);
 }
 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;
        }