示例#1
0
 internal Block[,] GetBlocks()
 {
     var result = new Block[Size,Size];
     for (int x = 0; x < Size; x++)
         for (int y = 0; y < Size; y++)
             result[x, y] = _blocks[x, y];
     return result;
 }
示例#2
0
        internal static Block[,] BuildShape(ShapeType type)
        {
            var size = GetShapeSize(type);
            var result = new Block[size,size];

            var color = GetShapeColor(type);

            switch (type)
            {
                case ShapeType.I:
                    /*    0  1  2  3
                     * 0 [ ][ ][ ][ ]
                     * 1 [X][X][X][X]
                     * 2 [ ][ ][ ][ ]
                     * 3 [ ][ ][ ][ ]
                     */
                    for (int i = 0; i < 4; i++)
                        result[i, 1] = new Block(color);
                    break;
                case ShapeType.J:
                    /*    0  1  2
                     * 0 [X][ ][ ]
                     * 1 [X][X][X]
                     * 2 [ ][ ][ ]
                     */
                    for (int i = 0; i < 3; i++)
                        result[i, 1] = new Block(color);
                    result[0, 0] = new Block(color);
                    break;
                case ShapeType.L:
                    /*    0  1  2
                     * 0 [ ][ ][X]
                     * 1 [X][X][X]
                     * 2 [ ][ ][ ]
                     */
                    for (int i = 0; i < 3; i++)
                        result[i, 1] = new Block(color);
                    result[2, 0] = new Block(color);
                    break;
                case ShapeType.Z:
                    /*    0  1  2
                     * 0 [X][X][ ]
                     * 1 [ ][X][X]
                     * 2 [ ][ ][ ]
                     */
                    result[0, 0] = new Block(color);
                    result[1, 0] = new Block(color);
                    result[1, 1] = new Block(color);
                    result[2, 1] = new Block(color);
                    break;
                case ShapeType.S:
                    /*    0  1  2
                     * 0 [ ][X][X]
                     * 1 [X][X][ ]
                     * 2 [ ][ ][ ]
                     */
                    result[1, 0] = new Block(color);
                    result[2, 0] = new Block(color);
                    result[0, 1] = new Block(color);
                    result[1, 1] = new Block(color);
                    break;
                case ShapeType.O:
                    /*    0  1
                     * 0 [X][X]
                     * 1 [X][X]
                     */
                    result[0, 0] = new Block(color);
                    result[1, 0] = new Block(color);
                    result[0, 1] = new Block(color);
                    result[1, 1] = new Block(color);
                    break;
                case ShapeType.T:
                    /*    0  1  2
                     * 0 [ ][X][ ]
                     * 1 [X][X][X]
                     * 2 [ ][ ][ ]
                     */
                    for (int i = 0; i < 3; i++)
                        result[i, 1] = new Block(color);
                    result[1, 0] = new Block(color);
                    break;
                default:
                    throw new Exception("Unsupported shape type: " + type);
            }

            return result;
        }
示例#3
0
 public void PlaceAt(Block block, Point point)
 {
     PlaceAt(block, point.X, point.Y);
 }
示例#4
0
        public void PlaceAt(Block block, int x, int y)
        {
            if (IsOutOfRange(x, y))
                return;

            _blocks[x, y] = block;
        }
示例#5
0
        // Rotation algorithm from Stackoverflow
        internal bool Rotate(Direction direction, IBlockArea blockArea)
        {
            if (direction != Direction.Right && direction != Direction.Left)
                throw new ArgumentException("Only left and right directions are supported", "direction");

            if (Position.Y < 0)
                return false;

            var newBlocks = new Block[Size,Size];

            if (direction == Direction.Left)
                for (int x = 0; x < Size; ++x)
                    for (int y = 0; y < Size; y++)
                        newBlocks[x, y] = _blocks[Size - y - 1, x];
            else // direction == Direction.Right
                for (int x = 0; x < Size; ++x)
                    for (int y = 0; y < Size; y++)
                        newBlocks[x, y] = _blocks[y, Size - x - 1];

            for (int x = 0; x < Size; x++)
                for (int y = 0; y < Size; y++)
                    if (newBlocks[x, y] != null && blockArea.IsOccupied(x + Position.X, y + Position.Y))
                        return false;

            _blocks = newBlocks;

            return true;
        }