Beispiel #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);
        }
        private IList<bool> BuildDataItem(int pieceIndex, RotatedPiece rotatedPiece, int x, int y)
        {
            var numColumns = _pieces.Length + _board.BoardSize * _board.BoardSize;
            var dataItem = new bool[numColumns];

            dataItem[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 = x + pieceX;
                    var boardY = y + pieceY;
                    var boardLocationColumnIndex = _pieces.Length + (_board.BoardSize * boardX) + boardY;
                    dataItem[boardLocationColumnIndex] = true;
                }
            }

            return dataItem;
        }
        private void AddDataItemsForPieceWithSpecificOrientation(IList<IList<bool>> data, 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.ForceColourOfSquareZeroZeroToBeBlack();
                    if (!_board.PlacePieceAt(rotatedPiece, x, y)) continue;
                    var dataItem = BuildDataItem(pieceIndex, rotatedPiece, x, y);
                    data.Add(dataItem);
                    _dictionary.Add(data.Count - 1, Tuple.Create(rotatedPiece, x, y));
                }
            }
        }
Beispiel #4
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;
        }
Beispiel #5
0
 private static void PrintRotatedPiece(RotatedPiece rotatedPiece)
 {
     string orientationInitialLetter = rotatedPiece.Orientation.ToString().Substring(0, 1);
     Console.Write("{0}{1} ", rotatedPiece.Piece.Name, orientationInitialLetter);
 }