Example #1
0
        public void UpdateField(TetrisBlock activeBlock)
        {
            for (int i = 0; i < activeBlock.Shape.GetLength(0); i++)
            {
                for (int j = 0; j < activeBlock.Shape.GetLength(1); j++)
                {
                    if (activeBlock.Shape[i, j])
                    {
                        playField[activeBlock.YPos + i, activeBlock.XPos + j] = true;
                    }
                }
            }

            activeBlock.SetBlock(Hud.nextBlock.ShapeNumber, Hud.nextBlock.ShapeColor);
            // reset
            Hud.nextBlock.ResetBlock();
            activeBlock.XPos = 8;
            activeBlock.YPos = 0;
        }
Example #2
0
        //public override void changeView(TetrisModel model) 多型覆蓋
        public override void changeView(TetrisModel model)
        {
            m_Graphics.Graphics.Clear(Color.Black);
            drawBackGround();
            bool[,] board = model.getBoard();
            TetrisBlock current_block = model.getBlock();


            //取得 GameOver 狀態,並決定是否顯示文字
            if (model.GameOver())
            {
                LbGO.Visible   = true;
                LbGO2.Visible  = true;
                iTime          = 0;
                sTimer.Enabled = false;
            }
            else
            {
                LbGO.Visible  = false;
                LbGO2.Visible = false;
            }

            for (int i = 0; i < 22; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    if (board[i, j])
                    {
                        drawBlock(whitePen, i, j);
                    }
                }
            }

            for (int i = 0; i < 4; i++)
            {
                drawBlock(bleakPen, current_block.get_CurrentX() + current_block.get_X(i), current_block.get_CurrentY() + current_block.get_Y(i));
            }
            m_Graphics.Render();
            Score.Text = "Score: " + model.getScore().ToString();
        }
Example #3
0
        //public override void changeView(TetrisModel model) 多型覆蓋
        public override void changeView(TetrisModel model)
        {
            m_Graphics.Graphics.Clear(Color.SkyBlue);
            drawBackGround();
            mymodel       = model;
            bool[,] board = model.getBoard();
            TetrisBlock current_block = model.getBlock();

            if (current_block.getBlockShape() != TetrisBlock.Blocks.No_shape)
            {
                for (int i = 0; i < 22; i++)
                {
                    for (int j = 0; j < 10; j++)
                    {
                        if (board[i, j])
                        {
                            drawBlock(board_Pen, board_brush, i, j);
                        }
                    }
                }

                for (int i = 0; i < 4; i++)
                {
                    drawBlock(current_Pen, current_brush, current_block.get_CurrentX() + current_block.get_X(i), current_block.get_CurrentY() + current_block.get_Y(i));
                }

                m_Graphics.Render();
                //m_Graphics.Dispose();
                scoreBox.Text = model.getScore().ToString();
            }

            if (mymodel.GameOver())
            {
                Gameover gameover_form = new Gameover();
                gameover_form.Show(this);
            }
        }
Example #4
0
        //public override void changeView(TetrisModel model) 多型覆蓋
        public override void changeView(TetrisModel model)
        {
            if (model.GameOver())
            {
                time = 0;
                viewTimer.Stop();
                button1.Text = "restart";
            }
            my_Graphics.Graphics.Clear(Color.White);
            drawBackGround();
            bool[,] board = model.getBoard();
            TetrisBlock current_block = model.getBlock();

            for (int i = 0; i < 22; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    if (board[i, j])
                    {
                        drawBlock(blackBrush, blackPen, i, j);
                    }
                }
            }


            int  depth = 0;
            bool add   = true;

            while (add)
            {
                depth++;
                for (int i = 0; i < 4; i++)
                {
                    //System.Diagnostics.Debug.WriteLine(current_block.get_X(i) + depth);
                    int h = current_block.get_X(i) + depth;
                    if (h < 0)
                    {
                        h = 0;
                    }
                    if (h > 21)
                    {
                        h = 21;
                    }
                    if (board[h, current_block.get_CurrentY() + current_block.get_Y(i)])
                    {
                        add = false;
                    }
                }
                if (depth > 21)
                {
                    break;
                }
            }

            for (int i = 0; i < 4; i++)
            {
                while (current_block.get_X(i) + depth > 22)
                {
                    depth--;
                }
            }

            for (int i = 0; i < 4; i++)
            {
                if (!model.GameOver())
                {
                    drawBlock(whiteBrush, redPen, current_block.get_X(i) + depth - 1, current_block.get_CurrentY() + current_block.get_Y(i));
                }
                if (!model.GameOver())
                {
                    drawBlock(yellowBrush, redPen, current_block.get_CurrentX() + current_block.get_X(i), current_block.get_CurrentY() + current_block.get_Y(i));
                }
            }
            my_Graphics.Render();
            score.Text    = "Score: " + model.getScore().ToString();
            viewTime.Text = "Time: " + time.ToString();
        }
Example #5
0
        public void genetateblock()     //generates a tetris block using the tetrisblock classes
        {
            Random rand = new Random(); //allows the creation of a Random number generator

            if (generated == -1 && nextgenerated == -1)
            {
                generated     = rand.Next(7); //generates a number between 0 and 7, this is used for all 7 tetris blocks
                nextgenerated = rand.Next(7); //generates a number between 0 and 7, this is used for all 7 tetris blocks
            }
            else
            {
                generated     = nextgenerated;
                nextgenerated = rand.Next(7);//generates a number between 0 and 7, this is used for all 7 tetris blocks
            }
            //generated = rand.Next(7);



            //generated = 6;
            regenerate(rand);
            //test case for tetris block zero


            int dot = 4;//dot is an intger used to map out the initial point of a falling block to make sure it's not on the edge

            switch (generated)
            {
            case 0:

                first = new TetrisBlock(0, dot, 0, dot - 1, 0, dot + 1, 1, dot);    //maps out the falling tetris block

                if (boardmatrix[0][dot] == 1 || boardmatrix[0][dot - 1] == 1 || boardmatrix[0][dot + 1] == 1 || boardmatrix[1][dot] == 1)
                {
                    setender(false);
                }

                break;

            case 1:
                first = new TetrisBlock(1, dot, 0, dot - 1, 1, dot + 1, 0, dot);

                if (boardmatrix[1][dot] == 1 || boardmatrix[0][dot - 1] == 1 || boardmatrix[1][dot + 1] == 1 || boardmatrix[0][dot] == 1)
                {
                    setender(false);
                }
                break;

            case 2:
                first = new TetrisBlock(1, dot, 1, dot - 1, 0, dot + 1, 0, dot);
                if (boardmatrix[1][dot] == 1 || boardmatrix[1][dot - 1] == 1 || boardmatrix[0][dot + 1] == 1 || boardmatrix[0][dot] == 1)
                {
                    setender(false);
                }
                break;

            case 3:
                first = new TetrisBlock(0, dot, 0, dot - 1, 0, dot + 1, 1, dot - 1);
                if (boardmatrix[0][dot] == 1 || boardmatrix[0][dot - 1] == 1 || boardmatrix[0][dot + 1] == 1 || boardmatrix[1][dot - 1] == 1)
                {
                    setender(false);
                }
                break;

            case 4:
                first = new TetrisBlock(0, dot, 0, dot - 1, 0, dot + 1, 1, dot + 1);
                if (boardmatrix[0][dot] == 1 || boardmatrix[0][dot - 1] == 1 || boardmatrix[0][dot + 1] == 1 || boardmatrix[1][dot + 1] == 1)
                {
                    setender(false);
                }
                break;

            case 5:
                first = new TetrisBlock(0, dot, 0, dot - 1, 0, dot + 1, 0, dot - 2);
                if (boardmatrix[0][dot] == 1 || boardmatrix[0][dot - 1] == 1 || boardmatrix[0][dot + 1] == 1 || boardmatrix[0][dot - 2] == 1)
                {
                    setender(false);
                }
                break;

            case 6:
                first = new TetrisBlock(0, dot, 0, dot - 1, 1, dot, 1, dot - 1);
                if (boardmatrix[0][dot] == 1 || boardmatrix[0][dot - 1] == 1 || boardmatrix[1][dot + 1] == 1 || boardmatrix[1][dot - 1] == 1)
                {
                    setender(false);
                }
                first.setvalid(false);    //prevents tetris block from rotating because it is the square block
                break;

            default:
                break;
            }
        }
Example #6
0
 public void SetValue(int[,] tetrisBoard, TetrisBlock block)
 {
     _tetrisBoard = tetrisBoard;
     _block       = block;
 }
        private void InvokeDraw(string propertyName)
        {
            int BlockX = 0;
            int BlockY = 0;

            if (propertyName.Length > 5 && propertyName.Substring(0, 5) == "TankX")
            {
                // выделяем координаты точки в стакане
                // ошибки при распознавании не проверяем

                int i = propertyName.IndexOf('Y', 6);

                BlockX = int.Parse(propertyName.Substring(5, i - 5));
                BlockY = int.Parse(propertyName.Substring(i + 1));

                // раньше попадали задачи на перерисовку в предыдущем стакане
                // после добавления Tetris.Dispose() проблема не проявляется
                //if (BlockX >= Tetris.Width || BlockY >= Tetris.Height) return;

                propertyName = "Block";
            }

            switch (propertyName)
            {
            case "Block":
                // точка в стакане

            {
                TetrisBlock TetrisView = Tetris.GetView(BlockX, BlockY);

                UIElement Block = GridTank.Children[BlockX + BlockY * Tetris.Width];

                if (TetrisView != null)
                {
                    Block.Visibility          = Visibility.Visible;
                    (Block as Rectangle).Fill = GetBrush(TetrisView.Color);
                }
                else
                {
                    Block.Visibility = Visibility.Hidden;
                }
            }

            break;

            case "Tank":
                // стакан

            {
                TetrisBlock[,] TetrisView = Tetris.GetView();

                for (int i = 0; i < GridTank.Children.Count; i++)
                {
                    int X = i % GridTank.Columns;
                    int Y = i / GridTank.Columns;

                    UIElement Block = GridTank.Children[i];

                    if (TetrisView[X, Y] != null)
                    {
                        Block.Visibility          = Visibility.Visible;
                        (Block as Rectangle).Fill = GetBrush(TetrisView[X, Y].Color);
                    }
                    else
                    {
                        Block.Visibility = Visibility.Hidden;
                    }
                }
            }

            break;

            case "NextFigure":
                // следующая фигура

                TetrisBlock[,] FigureView = Tetris.NextFigure?.GetView();

                if (FigureView != null)
                {
                    int NextWidth  = FigureView.GetLength(0);
                    int NextHeight = FigureView.GetLength(1);

                    int OffsetX = (MaxNextWidth - NextWidth) / 2;
                    int OffsetY = 0;

                    for (int i = 0; i < GridNext.Children.Count; i++)
                    {
                        int X = i % GridNext.Columns - OffsetX;
                        int Y = i / GridNext.Columns - OffsetY;

                        if (0 <= X && X < NextWidth && 0 <= Y && Y < NextHeight && FigureView[X, Y] != null)
                        {
                            UIElement Block = GridNext.Children[i];

                            Block.Visibility = Visibility.Visible;

                            (Block as Rectangle).Fill = GetBrush(FigureView[X, Y].Color);
                        }
                        else
                        {
                            GridNext.Children[i].Visibility = Visibility.Hidden;
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < GridNext.Children.Count; i++)
                    {
                        GridNext.Children[i].Visibility = Visibility.Hidden;
                    }
                }

                break;

            case "State":
                // статус

                CommandManager.InvalidateRequerySuggested();

                break;
            }
        }
Example #8
0
        public TetrisFiguresRandom(int Size, bool CheckConnected = true, int MinBlocks = 0, int MaxBlocks = 0)
        {
            int Variants = (int)Math.Pow(2, Size * Size); // количество вариантов для анализа

            for (int Variant = 1; Variant < Variants; Variant++)
            {
                bool[] Bits = Convert.ToString(Variant, 2).PadLeft(Size * Size, '0').Select(c => c == '1').ToArray();

                // проверяем количество точек

                int BlockCount = Bits.Count(b => b);

                if ((MinBlocks != 0 && BlockCount < MinBlocks) || (MaxBlocks != 0 && BlockCount > MaxBlocks))
                {
                    continue;
                }

                // заполняем точки фигуры, проверяем положение в левом верхнем углу

                TetrisBlock[] Blocks = new TetrisBlock[BlockCount];

                bool FirstRow    = false;
                bool FirstColumn = false;

                int BlockNumber = 0;

                for (int X = 0; X < Size; X++)
                {
                    for (int Y = 0; Y < Size; Y++)
                    {
                        if (Bits[X * Size + Y])
                        {
                            Blocks[BlockNumber] = new TetrisBlock(X, Y);

                            BlockNumber++;

                            if (Y == 0)
                            {
                                FirstRow = true;
                            }
                            if (X == 0)
                            {
                                FirstColumn = true;
                            }
                        }
                    }
                }

                if (!FirstRow || !FirstColumn)
                {
                    continue;
                }

                // проверяем связность фигуры

                if (CheckConnected)
                {
                    // http://algolist.manual.ru/maths/graphs/linked.php

                    // начальное значение - точки не связаны
                    foreach (TetrisBlock Block in Blocks)
                    {
                        Block.Mark = 1;
                    }

                    // начинаем проверку связности с этой точки
                    Blocks[0].Mark = 2;

                    TetrisBlock StartBlock;

                    while ((StartBlock = Blocks.FirstOrDefault(p => p.Mark == 2)) != null)
                    {
                        // точка обработана
                        StartBlock.Mark = 3;

                        // связанные точки ставим в очередь на обработку
                        foreach (TetrisBlock Block in Blocks.Where(p => p.Mark == 1 && Math.Abs(StartBlock.X - p.X) + Math.Abs(StartBlock.Y - p.Y) == 1))
                        {
                            Block.Mark = 2;
                        }
                    }

                    // остались не связанные точки
                    if (Blocks.Any(p => p.Mark == 1))
                    {
                        continue;
                    }
                }

                // проверяем совпадение фигур и добавляем фигуру

                TetrisFigure Figure = new TetrisFigure(Blocks);

                if (!Figures.Any(f => f.IsSimilar(Figure)))
                {
                    AddFigure(Figure);
                }
            }
        }
Example #9
0
        private static void KeyInputCheck(TetrisBlock activeBlock, PlayField playField)
        {
            string pauseText = @"
      ___     |      ___     |      ___     |      ___     |      ___     
     /\--\    |     /\--\    |     /\__\    |     /\--\    |     /\--\    
    /##\--\   |    /##\--\   |    /#/--/    |    /##\--\   |    /##\--\   
   /#/\#\--\  |   /#/\#\--\  |   /#/--/     |   /#/\#\--\  |   /#/\#\--\  
  /##\-\#\--\ |  /##\-\#\--\ |  /#/--/  ___ |  _\#\-\#\--\ |  /##\-\#\--\ 
 /#/\#\-\#\__\| /#/\#\-\#\__\| /#/__/  /\__\| /\-\#\-\#\__\| /#/\#\-\#\__\
 \/__\#\/#/--/| \/__\#\/#/--/| \#\--\ /#/--/| \#\-\#\-\/__/| \#\-\#\-\/__/
      \##/--/ |      \##/--/ |  \#\--/#/--/ |  \#\-\#\__\  |  \#\-\#\__\  
       \/__/  |      /#/--/  |   \#\/#/--/  |   \#\/#/--/  |   \#\-\/__/  
              |     /#/--/   |    \##/--/   |    \##/--/   |    \#\__\    
              |     \/__/    |     \/__/    |     \/__/    |     \/__/    
";

            if (KeyAvailable)
            {
                ConsoleKeyInfo key = ReadKey();

                if (key.Key == ConsoleKey.Escape)
                {
                    isPlaying = false;
                }

                if (key.Key == ConsoleKey.Enter && pauseMenu == false)
                {
                    Engine.ClearScreen();
                    Engine.DrawTitle(pauseText, 10);
                    pauseMenu = true;
                    ReadKey();
                }

                if (key.Key == ConsoleKey.Enter && pauseMenu == true)
                {
                    Engine.ClearScreen();
                    pauseMenu = false;
                }

                if (key.Key == ConsoleKey.RightArrow)
                {
                    if ((activeBlock.XPos < playField.XWith - (activeBlock.Shape.GetLength(1) + 1)))
                    {
                        activeBlock.XPos += 2;
                        if (playField.CollisionCheck(activeBlock))
                        {
                            activeBlock.XPos -= 2;
                        }
                    }
                }

                if (key.Key == ConsoleKey.LeftArrow)
                {
                    if (activeBlock.XPos >= 1)
                    {
                        activeBlock.XPos -= 2;
                        if (playField.CollisionCheck(activeBlock))
                        {
                            activeBlock.XPos += 2;
                        }
                    }
                }

                if (key.Key == ConsoleKey.UpArrow)
                {
                    activeBlock.RotateShape();
                }

                if (key.Key == ConsoleKey.DownArrow)
                {
                    tick = 1;
                    activeBlock.YPos++;
                }

                if (key.Key == ConsoleKey.Spacebar)
                {
                    while (!playField.CollisionCheck(activeBlock))
                    {
                        activeBlock.YPos++;
                        Hud.Score++;
                    }

                    playField.UpdateField(activeBlock);
                    playField.ScoreCheck();

                    // game over
                    if (playField.CollisionCheck(activeBlock))
                    {
                        HighScores.CheckHighScore(Hud.Score);
                        isPlaying = false;
                    }
                }
            }
        }
Example #10
0
 // конструктор для клонирования
 protected TetrisBlock(TetrisBlock another) : this(another.X, another.Y, another.Color)
 {
 }
Example #11
0
        public override void changeView(TetrisModel model)
        //public void changeView(TetrisModel model)
        {
            if (!model.GameOver())
            {
                m_Graphics.Graphics.Clear(Color.WhiteSmoke);
                drawBackGround();
                bool[,] board = model.getBoard();
                TetrisBlock current_block = model.getBlock();

                //draw bottom blocks
                for (int i = 0; i < (int)constant.BOARD_HEIGHT; i++)
                {
                    for (int j = 0; j < (int)constant.BOARD_WIDTH; j++)
                    {
                        if (board[i, j])
                        {
                            drawBlock(blackPen, i, j);
                        }
                    }
                }


                int shape = (int)current_block.getBlockShape();

                //draw predict blocks
                //find distance from falling block to exist blocks
                SolidBrush blockBrush    = chooseBrushColor(shape);
                int        fallingHeight = -1;
                bool       findHeight    = false;
                while (!findHeight)
                {
                    fallingHeight++;
                    for (int i = 0; i < 4; i++)
                    {
                        if (current_block.get_CurrentX() + current_block.get_X(i) + fallingHeight < (int)constant.BOARD_HEIGHT)
                        {
                            if (board[current_block.get_CurrentX() + current_block.get_X(i) + fallingHeight, current_block.get_CurrentY() + current_block.get_Y(i)])
                            {
                                findHeight = true;
                                if (fallingHeight != 0)
                                {
                                    fallingHeight--;
                                }
                                break;
                            }
                        }
                        else
                        {
                            findHeight = true;
                            if (fallingHeight != 0)
                            {
                                fallingHeight--;
                            }
                            break;
                        }
                    }
                }
                //draw
                for (int i = 0; i < 4; i++)
                {
                    drawPredictBlock(blockBrush, current_block.get_CurrentX() + current_block.get_X(i) + fallingHeight, current_block.get_CurrentY() + current_block.get_Y(i));
                }

                //draw falling blocks
                Pen blockPen = choosePenColor(shape);
                for (int i = 0; i < 4; i++)
                {
                    drawBlock(blockPen, current_block.get_CurrentX() + current_block.get_X(i), current_block.get_CurrentY() + current_block.get_Y(i));
                }
                m_Graphics.Render();
                //m_Graphics.Dispose();
                Score.Text = model.getScore().ToString();
            }
            else
            {
                GameOver();
            }
        }