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); }
public Square SquareAt(int x, int y) { PieceHolder pieceHolder = PieceHolderAt(x, y); return((pieceHolder != null) ? pieceHolder.Square : null); }
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; }
public Piece PieceAt(int x, int y) { PieceHolder pieceHolder = PieceHolderAt(x, y); return((pieceHolder != null) ? pieceHolder.RotatedPiece.Piece : null); }
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); }