Пример #1
0
        public bool LayoutNextPiece(RotatedPiece rotatedPiece)
        {
            int firstEmptyX;
            int firstEmptyY;

            var foundAnEmptySquare = FindFirstEmptySquare(out firstEmptyX, out firstEmptyY);

            if (!foundAnEmptySquare)
            {
                throw new InvalidOperationException("The puzzle is already solved!");
            }

            return(PlacePieceAt(rotatedPiece, firstEmptyX, firstEmptyY));
        }
Пример #2
0
        public bool PlacePieceAt(RotatedPiece rotatedPiece, int x, int y)
        {
            if (x < 0 || x >= BoardSize)
            {
                throw new ArgumentOutOfRangeException("x");
            }
            if (y < 0 || y >= BoardSize)
            {
                throw new ArgumentOutOfRangeException("y");
            }

            if (x + rotatedPiece.Width > BoardSize)
            {
                return(false);
            }
            if (y + rotatedPiece.Height > BoardSize)
            {
                return(false);
            }

            // Check for any overlap with existing pieces before setting any squares.
            for (var pieceX = 0; pieceX < rotatedPiece.Width; pieceX++)
            {
                for (var pieceY = 0; pieceY < rotatedPiece.Height; pieceY++)
                {
                    var square = rotatedPiece.SquareAt(pieceX, pieceY);
                    if (square != null)
                    {
                        if (_pieces[x + pieceX, y + pieceY] != null)
                        {
                            return(false);
                        }
                    }
                }
            }

            if (ColourOfSquareZeroZero.HasValue)
            {
                var colourOfSquareZeroZero = ColourOfSquareZeroZero.Value;

                // Check that each square of the piece to be placed has
                // the appropriate colour for its intended board position.
                for (var pieceX = 0; pieceX < rotatedPiece.Width; pieceX++)
                {
                    for (var pieceY = 0; pieceY < rotatedPiece.Height; pieceY++)
                    {
                        var square = rotatedPiece.SquareAt(pieceX, pieceY);
                        if (square != null)
                        {
                            var boardX         = x + pieceX;
                            var boardY         = y + pieceY;
                            var expectedColour = colourOfSquareZeroZero.RelativeColour(boardX, boardY);
                            if (square.Colour != expectedColour)
                            {
                                return(false);
                            }
                        }
                    }
                }
            }

            Square firstSquare = null;

            // It's now OK to go ahead and set the squares for the new piece.
            for (var pieceX = 0; pieceX < rotatedPiece.Width; pieceX++)
            {
                for (var pieceY = 0; pieceY < rotatedPiece.Height; pieceY++)
                {
                    var square = rotatedPiece.SquareAt(pieceX, pieceY);
                    if (square != null)
                    {
                        // Remember the first square in case we haven't set _colourOfSquareZeroZero yet.
                        if (firstSquare == null)
                        {
                            firstSquare = square;
                        }
                        _pieces[x + pieceX, y + pieceY] = new PieceHolder {
                            RotatedPiece = rotatedPiece, Square = square
                        };
                    }
                }
            }

            if (!ColourOfSquareZeroZero.HasValue && firstSquare != null)
            {
                var boardX = x + firstSquare.X;
                var boardY = y + firstSquare.Y;
                ColourOfSquareZeroZero = firstSquare.Colour.RelativeColour(boardX, boardY);
            }

            return(true);
        }