示例#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);
            }

            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);
                        }
                    }
                }
            }



            Coordinate firstSquare = null;

            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 (firstSquare == null)
                        {
                            firstSquare = square;
                        }
                        _pieces[x + pieceX, y + pieceY] = new PieceHolder {
                            RotatedPiece = rotatedPiece, Square = square
                        };
                    }
                }
            }



            return(true);
        }