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