Пример #1
0
 public Piece(Shape shape, uint angle, Grid grid)
 {
     _shape = shape;
     _color = pickColor (_shape);
     _blocks = new Block[Constants.BlockPerPiece];
     placeBlockAccordingToShape((Constants.GridSizeXmin + Constants.GridSizeXmax) / 2, Constants.GridSizeYmax);
     _angle = 0;
     turnPieceAccordingToAngle(grid, angle);
     movePieceUp(Constants.GridSizeYmax);
 }
Пример #2
0
 //--------------------------------------------------------------
 // CONSTRUCTORS
 //--------------------------------------------------------------
 public Player(string name)
 {
     _name = name;
     _score = 0;
     _level = Constants.MinLevel;
     _removedRows = 0;
     _grid = new Grid();
     _proposedPieces = new Piece[Constants.NbProposedPiece];
     for(int i = 0; i < Constants.NbProposedPiece; i++)
     {
         _proposedPieces[i] = new Piece(0, 0, Constants.IdGeneratorPlayer2);
         _proposedPieces[i].MoveToZero();
     }
 }
Пример #3
0
 private void turnPieceAccordingToAngle(Grid grid, uint angle)
 {
     for (uint i = angle ; i > 0 && TurnLeft(grid); i--);
 }
Пример #4
0
        private bool pieceCanMove(Grid grid, int[] newX, int[] newY)
        {
            bool canRotate = true;
            if(grid != null)
            {
                // First we need to check if the piece is too high
                // In this case, we need to make it go down
                int offsetDown = 0;
                for(uint i = 0 ; i < Constants.BlockPerPiece; i++)
                {
                    if(newY[i] - offsetDown > Constants.GridSizeYmax)
                        offsetDown = newY[i] - Constants.GridSizeYmax;
                }

                if(offsetDown > 0)
                {
                    for(uint i = 0 ; i < Constants.BlockPerPiece; i++)
                    {
                        newY[i] -= offsetDown;
                    }
                }

                // We are going to make 3 iterations to find a position that is good
                // First with the position already calculated, then with a shift to the right
                // and finally with a shift to the left
                int iteration = 0;
                do
                {
                    if(iteration == 1)
                    {
                        // We shift to the left
                        for (uint i = 0 ; i < Constants.BlockPerPiece; i++)
                        {
                            newX[i]--;
                        }
                        canRotate = true;
                    }
                    else if(iteration == 2)
                    {
                        // We shift to the right
                        for (uint i = 0 ; i < Constants.BlockPerPiece; i++)
                        {
                            newX[i] += 2;
                        }
                        canRotate = true;
                    }

                    for (uint i = 0 ; i < Constants.BlockPerPiece; i++)
                    {
                        if (grid.isOutOfGrid(newX[i], newY[i]) || grid.isBlock(newX[i], newY[i]))
                        {
                            canRotate = false;
                            break;
                        }
                    }
                    iteration++;
                } while(!canRotate && iteration < 3);
            }
            return canRotate;
        }
Пример #5
0
        public bool TurnRight(Grid grid)
        {
            bool canRotate = true;
            int[] newX = new int[Constants.BlockPerPiece];
            int[] newY = new int[Constants.BlockPerPiece];

            // Calculate the new coordinates
            for (uint i = 0; i < Constants.BlockPerPiece; i++)
            {
                // Change the point of reference : x = x - x0 and y = y - y0
                // Rotate : x = y and y = - x
                // Go back to the original point of reference : x = x + x0 and y = y + y0
                newX[i] =  _blocks[i]._y - _blocks[0]._y + _blocks[0]._x;
                newY[i] = -_blocks[i]._x + _blocks[0]._x + _blocks[0]._y;
            }

            // Check if the piece can be rotated
            canRotate = pieceCanMove(grid, newX, newY);

            // Rotate the piece if it can be rotated
            if(canRotate)
            {
                for (uint i = 0 ; canRotate && i < Constants.BlockPerPiece; i++)
                {
                    _blocks[i]._x = newX[i];
                    _blocks[i]._y = newY[i];
                }
                _angle = (_angle == 0) ? (_angle + 3) : _angle-1;
            }

            return canRotate;
        }
Пример #6
0
 public Piece(Grid grid, Shape shape)
     : this(shape, pickAngle(), grid)
 {
 }
Пример #7
0
        public bool MoveRight(Grid grid)
        {
            bool canMove = true;
            int[] newX = new int[Constants.BlockPerPiece];

            // Calculate the new coordinates
            for (uint i = 0; i < Constants.BlockPerPiece ; i++)
            {
                newX[i] =  _blocks[i]._x + 1 ;
            }
            // Check if the piece can be moved
            for (uint i = 0 ; i < Constants.BlockPerPiece ; i++)
            {
                if (grid.isOutOfGrid(newX[i], _blocks[i]._y) || grid.isBlock(newX[i], _blocks[i]._y))
                {
                    canMove = false;
                }
            }
            // Rotate the piece if it can be rotated
            for (uint i = 0 ; canMove && i < Constants.BlockPerPiece ; i++)
            {
                _blocks[i]._x = newX[i];
            }
            return canMove;
        }
Пример #8
0
 //--------------------------------------------------------------
 // CONSTRUCTORS
 //--------------------------------------------------------------
 public Piece(Grid grid, int generatorKey)
     : this(pickShape(randomGenerator, generatorKey), pickAngle(), grid)
 {
 }
Пример #9
0
        public bool MoveDown(Grid grid)
        {
            bool canMove = true;
            int[] newY = new int[Constants.BlockPerPiece];

            // Calculate the new coordinates
            for (uint i = 0; i < Constants.BlockPerPiece; i++)
            {
                newY[i] =  _blocks[i]._y - 1 ;
            }
            // Check if the piece can be moved
            for (uint i = 0 ; i < Constants.BlockPerPiece ; i++)
            {
                if (grid.isOutOfGrid(_blocks[i]._x, newY[i]) || grid.isBlock(_blocks[i]._x, newY[i]))
                {
                    canMove = false;
                    break;
                }
            }

            // Validate the new position if it is ok
            if(canMove)
            {
                for (uint i = 0 ; i < Constants.BlockPerPiece ; i++)
                {
                    _blocks[i]._y = newY[i];
                }
            }
            return canMove;
        }
Пример #10
0
 public int MoveBottom(Grid grid)
 {
     int nbMoveDown = 0;
     while (this.MoveDown(grid))
     {
         nbMoveDown++;
     }
     return nbMoveDown;
 }
Пример #11
0
        public void Init(Grid grid)
        {
            // Associate the instance
            _grid = grid;

            // Create the associated PieceViews
            _fallingPieceView = new PieceView(_grid._fallingPiece, false);
            _shadowPieceView = new PieceView(_grid._shadowPiece, true);

            // Create the associated BlockViews
            _mapView = new BlockView[Constants.GridSizeX, Constants.GridSizeY];
            for (uint i = 0 ; i < _grid._map.GetLength(0) ; i++)
            {
                for (uint j = 0 ; j < _grid._map.GetLength(1) ; j++)
                {
                    _mapView[i,j] = new BlockView(_grid._map[i,j], false);
                }
            }

            _mutexView = new Mutex(false);
        }