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