Esempio n. 1
0
        /// <summary>
        /// Constructor of the board
        /// </summary>
        public Board()
        {
            //Init line remove
            linesToRemoveId = new List <int>();
            lineRemoveState = 0;

            //Init grid
            landedShape = new int[Settings.BOARD_HEIGHT][];
            for (int i = 0; i < landedShape.Length; i++)
            {
                landedShape[i] = new int[Settings.BOARD_WIDTH];
            }

            //Create first shape
            CurrentShape = Tetromino.Random(Settings.START_X, Settings.START_Y);

            //Create next shape
            NextShape = Tetromino.Random(Settings.START_X, Settings.START_Y);

            //Statistics
            statistics = new Dictionary <Tetromino.Shape, int>();
            foreach (Tetromino.Shape shape in Enum.GetValues(typeof(Tetromino.Shape)))
            {
                statistics.Add(shape, 0);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Move the pieces one line down
        /// </summary>
        public bool Down()
        {
            Tetromino movedShape = CurrentShape.Down();

            if (CanMove(movedShape))
            {
                CurrentShape = movedShape;
                return(true);
            }
            return(false);
        }
Esempio n. 3
0
        /// <summary>
        /// Move the piece one column left
        /// </summary>
        public bool Left()
        {
            Tetromino nextShape = CurrentShape.Left();

            if (CanMove(nextShape))
            {
                CurrentShape = nextShape;
                return(true);
            }
            return(false);
        }
Esempio n. 4
0
        /// <summary>
        /// Turn the pieces
        /// </summary>
        public bool TurnCCW()
        {
            //turn currentShape
            Tetromino nextShape = CurrentShape.RotateCCW();

            if (CanMove(nextShape))
            {
                CurrentShape = nextShape;
                return(true);
            }
            return(false);
        }
Esempio n. 5
0
        /// <summary>
        /// Return true if the move is valid
        /// </summary>
        /// <param name="nextPos"></param>
        /// <param name="shape"></param>
        /// <returns></returns>
        private bool CanMove(Tetromino shape)
        {
            int n = shape.Grid.GetLength(0);

            for (int i = 0; i < n; ++i)
            {
                for (int j = 0; j < n; ++j)
                {
                    if (shape.Grid[i, j] > 0 && shape.y + i >= 0 && (
                            shape.y + i >= Settings.BOARD_HEIGHT ||
                            shape.x + j < 0 || shape.x + j >= Settings.BOARD_WIDTH ||
                            landedShape[shape.y + i][shape.x + j] > 0))
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
Esempio n. 6
0
        /// <summary>
        /// Trigger when a turn must be run
        /// </summary>
        /// <returns>Nb line removed - (-1) for Game Over</returns>
        public int Tick()
        {
            if (endGameState >= Settings.BOARD_HEIGHT)
            {
                return(-1);
            }

            if (AnimationEndGame)
            {
                EndLineAnimate(endGameState);
                ++endGameState;
                return(-1);
            }

            if (lineRemoveState > 0)
            {
                switch (lineRemoveState)
                {
                //Last state, move down all the lines
                case 1:
                    ClearLines(linesToRemoveId);
                    break;

                //Default we remove two squares
                default:
                    RemoveAnimate(linesToRemoveId, lineRemoveState);
                    break;
                }

                --lineRemoveState;
                return(0);
            }

            if (!Down())
            {
                // Detect Game Over
                if (CurrentShape.y < 0)
                {
                    ++endGameState;
                    landedShape  = MergeGridWithShape();
                    CurrentShape = Tetromino.Empty();
                    return(-1);
                }

                //Update statistics
                statistics[CurrentShape.shape]++;

                //Merge shape to current grid
                landedShape = MergeGridWithShape();

                //Clear lines
                linesToRemoveId = CompletedLines(CurrentShape.y);

                //Change current shape
                CurrentShape = NextShape;

                //Generate next shape
                NextShape = Tetromino.Random(Settings.START_X, Settings.START_Y);

                //Set removing line state
                lineRemoveState = 6;

                return(linesToRemoveId.Count());
            }
            else
            {
                //Tetromino not down Nothing to do
            }
            return(0);
        }