public static MovingTetromino CreateMovingTetromino(TetrisGameBoard gameBoard, BrickType brick) { MovingTetromino inst = null; switch (brick) { case BrickType.J: { inst = new MovingTetrominoJ(gameBoard); break; } case BrickType.L: { inst = new MovingTetrominoL(gameBoard); break; } case BrickType.O: { inst = new MovingTetrominoO(gameBoard); break; } case BrickType.S: { inst = new MovingTetrominoS(gameBoard); break; } case BrickType.T: { inst = new MovingTetrominoT(gameBoard); break; } case BrickType.Z: { inst = new MovingTetrominoZ(gameBoard); break; } default: case BrickType.I: { inst = new MovingTetrominoI(gameBoard); break; } } inst.InitializeDescriptiveGrid(); return(inst); }
/// <summary> /// Puts the tetromino into the brick area. /// Called when the engine prepares data for structured and visual output of the brick area or when /// a falling tetromino hits the bottom. /// </summary> public void MergeTetrominoWithGameBoard(MovingTetromino tetromino) { Debug.Assert(tetromino != null & tetromino.DescriptiveGrid != null); BrickType[,] tetrominoGrid = tetromino.DescriptiveGrid; // put the moving tetromino into the game board for (int iRow = 0; iRow < tetromino.Height; iRow++) { for (int iColumn = 0; iColumn < tetromino.Width; iColumn++) { if (tetrominoGrid[iRow, iColumn] != BrickType.None) { m_gameBoardState[tetromino.Row + iRow, tetromino.Column + iColumn] = tetrominoGrid[iRow, iColumn]; } } } CalculateHorizon(); }
private void ReinitGameBoard() { m_world.BrickAreaOutput.Fill((float)BrickType.None); m_world.HintAreaOutput.Fill((float)BrickType.None); m_world.NextBrickNumberOutput.Fill(0.0f); m_world.ScoreOutput.Fill(0.0f); m_world.ScoreDeltaOutput.Fill(0.0f); m_world.LevelOutput.Fill(0.0f); // erase visual output not necessary - done by RenderTask m_gameBoard = new TetrisGameBoard(m_world.BrickAreaColumns, m_world.BrickAreaRows); // add almost full lines for (int iRow = 0; iRow < m_params.AlmostFullLinesAtStart; iRow++) { int iRowIndex = m_world.BrickAreaRows - 1 - iRow; for (int iCol = 0; iCol < m_world.BrickAreaColumns; iCol++) { m_gameBoard.GameBoardState[iRowIndex, iCol] = BrickType.Preset; } int holeIndex = m_rndGen.Next(0, 10); m_gameBoard.GameBoardState[iRowIndex, holeIndex] = BrickType.None; } m_stepsFromLastDrop = 0; m_level = 0; m_score = 0; m_scoreDelta = 0; m_shouldSpawnTetromino = true; m_tetromino = null; m_totalErasedLines = 0; PrepareNextTetromino(); }
// return false if the spawned tetromino intersects bricks already present on the game board private bool SpawnTetromino() { m_tetromino = m_nextTetromino; PrepareNextTetromino(); return !m_tetromino.IsOverlappingGameBoardBricks(); }
private void ReinitGameBoard() { m_world.BrickAreaOutput.Fill((float)BrickType.None); m_world.HintAreaOutput.Fill((float)BrickType.None); m_world.NextBrickNumberOutput.Fill(0.0f); m_world.ScoreOutput.Fill(0.0f); m_world.ScoreDeltaOutput.Fill(0.0f); m_world.LevelOutput.Fill(0.0f); // erase visual output not necessary - done by RenderTask m_gameBoard = new TetrisGameBoard(m_world.BrickAreaColumns, m_world.BrickAreaRows); // add almost full lines for(int iRow = 0; iRow < m_params.AlmostFullLinesAtStart; iRow++) { int iRowIndex = m_world.BrickAreaRows - 1 - iRow; for(int iCol = 0; iCol < m_world.BrickAreaColumns; iCol++) { m_gameBoard.GameBoardState[iRowIndex,iCol] = BrickType.Preset; } int holeIndex = m_rndGen.Next(0, 10); m_gameBoard.GameBoardState[iRowIndex, holeIndex] = BrickType.None; } m_stepsFromLastDrop = 0; m_level = 0; m_score = 0; m_scoreDelta = 0; m_shouldSpawnTetromino = true; m_tetromino = null; m_totalErasedLines = 0; PrepareNextTetromino(); }
private void PrepareNextTetromino() { BrickType next = (BrickType)(m_rndGen.Next(0, 7) + 1); m_nextTetromino = MovingTetrominoFactory.CreateMovingTetromino(m_gameBoard, next); }
/// <summary> /// Compute next state of the world. Complete game logic for one step. /// </summary> public void Step(TetrisWorld.ActionInputType input) { IsMerging = false; if (m_shouldSpawnTetromino) { m_shouldSpawnTetromino = false; if(!SpawnTetromino()) { // game over ResetLost(); return; } } bool isDrop = false; if (m_stepsFromLastDrop >= GetWaitStepsPerDrop()) { isDrop = true; } switch(input) { case TetrisWorld.ActionInputType.MoveDown: { isDrop = true; break; } case TetrisWorld.ActionInputType.MoveLeft: { m_tetromino.TryMoveLeft(); break; } case TetrisWorld.ActionInputType.MoveRight: { m_tetromino.TryMoveRight(); break; } case TetrisWorld.ActionInputType.RotateLeft: { m_tetromino.TryRotateLeft(); break; } case TetrisWorld.ActionInputType.RotateRight: { m_tetromino.TryRotateRight(); break; } default: case TetrisWorld.ActionInputType.NoAction: { break; } } m_scoreDelta = 0; if (isDrop) { m_stepsFromLastDrop = 0; if(!m_tetromino.TryMoveDown()) { IsMerging = true; CalculateMerge(); m_gameBoard.MergeTetrominoWithGameBoard(m_tetromino); int erasedLines = m_gameBoard.EraseFullLines(); // most often returns 0 m_scoreDelta = 100 * erasedLines * erasedLines; m_score += m_scoreDelta; m_totalErasedLines += erasedLines; m_level = m_totalErasedLines / Math.Max(1,m_params.ClearedLinesPerLevel); m_tetromino = null; m_shouldSpawnTetromino = true; } } else { m_stepsFromLastDrop++; } FillWorldState(); }
/// <summary> /// Compute next state of the world. Complete game logic for one step. /// </summary> public void Step(TetrisWorld.ActionInputType input) { IsMerging = false; if (m_shouldSpawnTetromino) { m_shouldSpawnTetromino = false; if (!SpawnTetromino()) { // game over ResetLost(); return; } } bool isDrop = false; if (m_stepsFromLastDrop >= GetWaitStepsPerDrop()) { isDrop = true; } switch (input) { case TetrisWorld.ActionInputType.MoveDown: { isDrop = true; break; } case TetrisWorld.ActionInputType.MoveLeft: { m_tetromino.TryMoveLeft(); break; } case TetrisWorld.ActionInputType.MoveRight: { m_tetromino.TryMoveRight(); break; } case TetrisWorld.ActionInputType.RotateLeft: { m_tetromino.TryRotateLeft(); break; } case TetrisWorld.ActionInputType.RotateRight: { m_tetromino.TryRotateRight(); break; } default: case TetrisWorld.ActionInputType.NoAction: { break; } } m_scoreDelta = 0; if (isDrop) { m_stepsFromLastDrop = 0; if (!m_tetromino.TryMoveDown()) { IsMerging = true; CalculateMerge(); m_gameBoard.MergeTetrominoWithGameBoard(m_tetromino); int erasedLines = m_gameBoard.EraseFullLines(); // most often returns 0 m_scoreDelta = 100 * erasedLines * erasedLines; m_score += m_scoreDelta; m_totalErasedLines += erasedLines; m_level = m_totalErasedLines / Math.Max(1, m_params.ClearedLinesPerLevel); m_tetromino = null; m_shouldSpawnTetromino = true; } } else { m_stepsFromLastDrop++; } FillWorldState(); }
// return false if the spawned tetromino intersects bricks already present on the game board private bool SpawnTetromino() { m_tetromino = m_nextTetromino; PrepareNextTetromino(); return(!m_tetromino.IsOverlappingGameBoardBricks()); }