예제 #1
0
파일: Game.cs 프로젝트: aisaiev/Tetris-Game
        public void GenerateFigure()
        {
            switch (rnd.Next(1, 8))
            {
            case 1:
                figure = new IFigure(border);
                break;

            case 2:
                figure = new JFigure(border);
                break;

            case 3:
                figure = new LFigure(border);
                break;

            case 4:
                figure = new OFigure(border);
                break;

            case 5:
                figure = new SFigure(border);
                break;

            case 6:
                figure = new TFigure(border);
                break;

            case 7:
                figure = new ZFigure(border);
                break;
            }
        }
예제 #2
0
 public void PasteFigureInField(Figures figure)
 {
     for (var i = 0; i < figure.X.Length; i++)
     {
         field[figure.X[i], figure.Y[i]] = 2;
     }
 }
예제 #3
0
 public void FillFieldWithBlocks(Figures figure)
 {
     for (var i = 0; i < figure.X.Length; i++)
     {
         field[figure.X[i], figure.Y[i]] = 1;
     }
 }
예제 #4
0
        // добавление фигуры в стакан
        private bool CreateCurrentFigure()
        {
            CurrentFigure = NextFigure ?? Figures.GetFigure(FigureColors.GetColor());

            CurrentFigure.OffsetX = (Width - CurrentFigure.Width) / 2;
            CurrentFigure.OffsetY = 0;

            lock (Tank)
            {
                // проверяем место для фигуры

                if (!TryCurrentFigure(CurrentFigure))
                {
                    return(false);
                }

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

                ShowCurrentFigure();

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

                CreateNextFigure();
            }

            return(true);
        }
예제 #5
0
 static public void Right(Figures figure)
 {
     if (field.TestMoveRight(figure))
     {
         field.DeleteFigureFromField(figure);
         figure.Right();
         field.PasteFigureInField(figure);
     }
 }
예제 #6
0
 public void DeleteFigureFromField(Figures figure)
 {
     for (var i = 0; i < figure.X.Length; i++)
     {
         if (field[figure.X[i], figure.Y[i]] == 2)
         {
             field[figure.X[i], figure.Y[i]] = 0;
         }
     }
 }
예제 #7
0
 public static void GameStart()
 {
     Parameters.SetMass();
     Figure        = null;
     WaitTime      = Levels.StartWaitTime;
     Scores        = 0;
     NextF         = rnd.Next() % 5;
     time.Interval = WaitTime; //установка интервала  на каждый тик
     Test.Refresh();
     time.Start();             // Запуск
 }
예제 #8
0
 public bool TestMoveRight(Figures figure)
 {
     for (var i = 0; i < figure.X.Length; i++)
     {
         if (figure.Y[i] == FieldY - 1 || field[figure.X[i], figure.Y[i] + 1] == 1)
         {
             return(false);
         }
     }
     return(true);
 }
예제 #9
0
 public bool TestMoveLeft(Figures figure)
 {
     for (var i = 0; i < figure.X.Length; i++)
     {
         if (figure.Y[i] == 0 || field[figure.X[i], figure.Y[i] - 1] == 1)
         {
             return(false);
         }
     }
     return(true);
 }
예제 #10
0
 public bool TestRotationInField(Figures figure)
 {
     for (var i = 0; i < figure.X.Length; i++)
     {
         if (field[figure.X[i], figure.Y[i]] == 1)
         {
             return(false);
         }
     }
     return(true);
 }
예제 #11
0
 public bool TestBottoming(Figures figure)
 {
     for (var i = 0; i < figure.X.Length; i++)
     {
         if (figure.X[i] + 1 == FieldX || field[figure.X[i] + 1, figure.Y[i]] == 1)
         {
             return(true);
         }
     }
     return(false);
 }
예제 #12
0
        public FigureTemp(Figures figure)

        {
            X = new int[4];
            Y = new int[4];

            for (var i = 0; i < figure.X.Length; i++)
            {
                X[i] = figure.X[i];
                Y[i] = figure.Y[i];
            }
        }
예제 #13
0
        static void TickHandler(object sender, EventArgs e)
        {
            if (Figure == null)
            {
                Scores += Parameters.LineBreak();

                switch (NextF)
                {
                case 0: Figure = new LineFigure(); break;

                case 1: Figure = new TriangleFigure(); break;

                case 2: Figure = new SquareFigure(); break;

                case 3:
                    if (NextFRlyPos == 1)
                    {
                        Figure = new FourLeftFigure();
                    }
                    else
                    {
                        Figure = new FourRightFigure();
                    }
                    break;

                case 4: Figure = new HorseFigure(NextFRlyPos); break;
                }
                if ((NextFRlyPos = rnd.Next() % 2) == 0)
                {
                    NextFRlyPos = -1;
                }
                else
                {
                    NextFRlyPos = 1;
                }
                NextF = rnd.Next() % 5;
                MiniPics.ShowJpg(NextF, NextFRlyPos);
            }
            else
            {
                if (!Figure.TickAction())
                {
                    Figure = null;
                }
            }
            Scores += WaitTime / 100;
            Test.ScoreSet(Scores);
        }
예제 #14
0
        static public void Rotate(Figures figure)
        {
            FigureTemp figureTmp = new FigureTemp(figure);

            field.DeleteFigureFromField(figure);
            figure.Rotate();
            if (field.TestRotationInField(figure))
            {
                field.PasteFigureInField(figure);
            }
            else
            {
                figure.FigureRestore(figureTmp);
                field.PasteFigureInField(figure);
            }
        }
예제 #15
0
        /// <summary>
        /// init all objects
        /// </summary>
        public void Start()
        {
            RootGameObject = GameObject.Find("Root");
            tetris         = RootGameObject.GetComponent <Tetris>();

            glass   = new Glass(Mode);
            checker = new Checker(glass);
            DoInit  = true;

            figures   = new Figures(Mode);
            NewFigure = true;

            userInput = new UserInput();
            DoUpdate  = false;
            GameOver  = false;
        }
예제 #16
0
 static void PrintingTechnicalInformation(Figures figure)
 {
     Console.SetCursorPosition(0, 26);
     Console.Write("                                                       ");
     Console.SetCursorPosition(0, 27);
     Console.WriteLine("                                                       ");
     Console.SetCursorPosition(0, 26);
     for (var i = 0; i < figure.X.Length; i++)
     {
         Console.Write(figure.X[i] + " ");
     }
     Console.SetCursorPosition(0, 27);
     for (var i = 0; i < figure.Y.Length; i++)
     {
         Console.Write(figure.Y[i] + " ");
     }
     Console.SetCursorPosition(0, 28);
     Console.Write(figure);
     Console.SetCursorPosition(0, 0);
 }
예제 #17
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);
                }
            }
        }
예제 #18
0
파일: Game.cs 프로젝트: aisaiev/Tetris-Game
        public void StartGame(Border border)
        {
            Console.CursorVisible = false;
            this.border           = border;
            currentFigure         = new Figure();
            nextFigure            = new Figure();
            drawer      = new Drawer();
            kbh         = new KeyboardHendler();
            rnd         = new Random();
            fallenNodes = new List <Node>();
            drawer.DrawBorderForNextFigureAndScore(border);
            kbh.KeyRight += MoveRight;
            kbh.KeyLeft  += MoveLeft;
            kbh.KeyUp    += Rotate;
            kbh.KeyDown  += IncreaseSpeed;

            GenerateFigure();
            do
            {
                speed           = 400;
                currentPosition = FigurePosition.position1;
                currentFigure.FigureNodes.Clear();
                nextFigure.FigureNodes.Clear();
                currentFigure.FigureNodes.AddRange(figure.FigureNodes);
                currentFigure.FigureShape = figure.FigureShape;
                GenerateFigure();
                nextFigure.FigureNodes.AddRange(figure.FigureNodes);

                do
                {
                    direction = Direction.Down;
                    Console.SetCursorPosition(0, 0);
                    drawer.DrawBorder(border);
                    drawer.DrawFigure(currentFigure.FigureNodes, border);
                    drawer.DrawFigure(fallenNodes, border);
                    drawer.DrawNextFigure(nextFigure.FigureNodes, border);
                    drawer.DrawScore(border, score);
                    Thread.Sleep(speed);
                    kbh.QueryKbH();
                    if (direction == Direction.Down)
                    {
                        MoveDown();
                    }
                } while (IsFigureOnTheBottom(currentFigure.FigureNodes) == false && IsFigureOnTheFallenFigures(currentFigure.FigureNodes, fallenNodes) == false);

                drawer.ClearNextFigure(nextFigure.FigureNodes, border);
                fallenNodes.AddRange(currentFigure.FigureNodes);

                for (int i = 0; i < border.Height; i++)
                {
                    if (fallenNodes.Where(x => x.Y == i).Count() == border.Width - 2)
                    {
                        fallenNodes.RemoveAll(x => x.Y == i);
                        burnNodes = fallenNodes.Where(x => x.Y < i).ToList();
                        foreach (var node in burnNodes)
                        {
                            node.MoveDown();
                        }
                        burnNodes.Clear();
                        score += 100;
                    }
                }
            } while (IsFallenFigureOnTheTop(fallenNodes) == false);
            drawer.DrawGameOver(border);
        }
예제 #19
0
 public void CreateNextFigure()
 {
     NextFigure = Figures.GetFigure(FigureColors.GetColor());
 }