Example #1
0
        private bool AddRandomCage()
        {
            Cage randCage = GetRandomCage();

            if (randCage == null)
            {
                GameOver();
                return(false);
            }

            Cage cage = new Cage();

            cage.Name   = $"cage_{CageCounter}";
            cage.Size   = CageSize;
            cage.Number = DefaultNumber;
            cage.ApplyNumber();
            cage.Position = randCage.Position;
            cage.Location = new Point(
                Location.Y + randCage.Position.X * CageSize.Width + (CageDistance * randCage.Position.X),
                Location.X + randCage.Position.Y * CageSize.Height + (CageDistance * randCage.Position.Y));
            cage.PreviewKeyDown += Cage_PreviewKeyDown;

            Cages.Add(cage);
            MainForm.Controls.Add(cage);
            cage.BringToFront();
            CageCounter++;

            new Animation(CageDistance, 20, IsAnimation).ShowCage(cage, cage.BackColor);

            return(true);
        }
Example #2
0
        private void Animation_OnMix(Cage cage)
        {
            List <Cage> deleteCages = Cages.Where(c => c.Number == -1).ToList();

            for (int i = 0; i < deleteCages.Count; i++)
            {
                MainForm.Controls.Remove(deleteCages[i]);
                Cages.Remove(deleteCages[i]);
            }

            cage.ApplyNumber();
            if (cage.Number > MaxNumberNow)
            {
                MaxNumberNow = cage.Number;
            }
            Score = new Score(args.GetLastScore().CountScore + cage.Number, CountMove);
            OnScore?.Invoke(args);
            if (cage.Number > 99)
            {
                cage.Font = new Font("Google Sans", 22, FontStyle.Regular);
            }
            if (cage.Number > 512)
            {
                cage.Font = new Font("Google Sans", 18, FontStyle.Regular);
            }
            if (cage.Number > 8192)
            {
                cage.Font = new Font("Google Sans", 14, FontStyle.Regular);
            }
        }
Example #3
0
        //public async void EndOfTurn(Cage cage) {
        //    if (IsAnimation)
        //        await Task.Delay(72);
        //    OnEndOfTurn?.Invoke(cage);
        //}

        public async void ShowCage(Cage cage, Color backColor)
        {
            if (!IsAnimation)
            {
                return;
            }
            for (int i = 50; i <= 255; i += 15, await Task.Delay(IsAnimation ? (int)(Speed * 1.5) : 0))
            {
                cage.BackColor = Color.FromArgb(i, cage.BackColor);
            }
        }
Example #4
0
        private List <Cage> CheckCagesNearly(Cage cage)
        {
            Point       position = cage.Position;
            List <Cage> cageList = new List <Cage>();

            cageList.Add(Cages.Where(c => c.Position.X == position.X - 1 && c.Position.Y == position.Y).FirstOrDefault());
            cageList.Add(Cages.Where(c => c.Position.X == position.X + 1 && c.Position.Y == position.Y).FirstOrDefault());
            cageList.Add(Cages.Where(c => c.Position.Y == position.Y - 1 && c.Position.X == position.X).FirstOrDefault());
            cageList.Add(Cages.Where(c => c.Position.Y == position.Y + -1 && c.Position.X == position.X).FirstOrDefault());
            cageList.RemoveAll(a => a == null);

            return(cageList);
        }
Example #5
0
 private bool CheckCageCanMove(Cage cage, Move move)
 {
     if (!CheckCageBorder(cage, move))
     {
         Cage nearlyCage = CheckCageNearly(cage, move);
         if (nearlyCage == null)
         {
             return(true);
         }
         return(nearlyCage.Number == cage.Number);
     }
     return(false);
 }
Example #6
0
 private bool CheckCageBorder(Cage positionCage, Move move)
 {
     if (move == Move.Left)
     {
         return(positionCage.Position.X == 0);
     }
     if (move == Move.Right)
     {
         return(positionCage.Position.X == CellSize.Y - 1);
     }
     if (move == Move.Up)
     {
         return(positionCage.Position.Y == 0);
     }
     return(positionCage.Position.Y == CellSize.X - 1);
 }
Example #7
0
 public void RemoveBackgroundCell()
 {
     for (int i = 0; i < CellSize.X; i++)
     {
         for (int j = 0; j < CellSize.Y; j++)
         {
             Cage background = MainForm.Controls.OfType <Cage>().Where(b => b.IsBackground && b.Position == new Point(i, j)).FirstOrDefault();
             if (background != null)
             {
                 MainForm.Controls.Remove(background);
             }
         }
     }
     IsBackground = false;
     BackgroundCages.Clear();
 }
Example #8
0
 private void MoveCage(Cage cage)
 {
     for (int i = 0; i < ((IsHorisontal) ? cage.Size.Width / Speed : cage.Size.Height / Speed); i++)
     {
         cage.Location = new Point(
             cage.Location.X + (IsHorisontal ? ((move == Game2048.Move.Left) ? -Speed : Speed) : 0),
             cage.Location.Y + ((!IsHorisontal) ? ((move == Game2048.Move.Up) ? -Speed : Speed) : 0)
             );
     }
     for (int j = 0; j < CageDistance; j++)
     {
         cage.Location = new Point(
             cage.Location.X + (IsHorisontal ? ((move == Game2048.Move.Left) ? -1 : 1) : 0),
             cage.Location.Y + ((!IsHorisontal) ? ((move == Game2048.Move.Up) ? -1 : 1) : 0)
             );
     }
 }
Example #9
0
 public async void Mix(Cage cage)
 {
     if (!IsAnimation)
     {
         return;
     }
     IsProcess = true;
     for (int i = 0; i < 4; i++, await Task.Delay(IsAnimation ? Speed : 0))
     {
         cage.Size     = new Size(cage.Size.Width + 2, cage.Size.Height + 2);
         cage.Location = new Point(cage.Location.X - 1, cage.Location.Y - 1);
     }
     for (int i = 0; i < 4; i++, await Task.Delay(IsAnimation ? Speed : 0))
     {
         cage.Size     = new Size(cage.Size.Width - 2, cage.Size.Height - 2);
         cage.Location = new Point(cage.Location.X + 1, cage.Location.Y + 1);
     }
     IsProcess = false;
 }
Example #10
0
        public async void Move(Move move, Cage cage, bool isMix)
        {
            CounterForMulti++;
            this.move    = move;
            this.isMix   = isMix;
            IsHorisontal = move == Game2048.Move.Left || move == Game2048.Move.Right;
            if (IsAnimation)
            {
                IsProcess = true;
                await MoveCageAsync(cage);

                IsProcess = false;
            }
            else
            {
                callback?.Invoke(cage);
                MoveCage(cage);
            }
        }
Example #11
0
 private async Task MoveCageAsync(Cage cage)
 {
     for (int i = 0; i < ((IsHorisontal) ? cage.Size.Width / Speed : cage.Size.Height / Speed); i++, await Task.Delay(1))
     {
         cage.Location = new Point(
             cage.Location.X + (IsHorisontal ? ((move == Game2048.Move.Left) ? -Speed : Speed) : 0),
             cage.Location.Y + ((!IsHorisontal) ? ((move == Game2048.Move.Up) ? -Speed : Speed) : 0)
             );
     }
     for (int j = 0; j < CageDistance; j++)
     {
         cage.Location = new Point(
             cage.Location.X + (IsHorisontal ? ((move == Game2048.Move.Left) ? -1 : 1) : 0),
             cage.Location.Y + ((!IsHorisontal) ? ((move == Game2048.Move.Up) ? -1 : 1) : 0)
             );
     }
     if (isMix)
     {
         callback?.Invoke(cage);
     }
 }
Example #12
0
        private Cage CheckCageNearly(Cage cage, Move move)
        {
            Point position = cage.Position;

            if (move == Move.Left)
            {
                return(Cages.Where(c => c.Position.X == position.X - 1 && c.Position.Y == position.Y).FirstOrDefault());
            }
            if (move == Move.Right)
            {
                return(Cages.Where(c => c.Position.X == position.X + 1 && c.Position.Y == position.Y).FirstOrDefault());
            }
            if (move == Move.Up)
            {
                return(Cages.Where(c => c.Position.Y == position.Y - 1 && c.Position.X == position.X).FirstOrDefault());
            }
            if (move == Move.Down)
            {
                return(Cages.Where(c => c.Position.Y == position.Y + 1 && c.Position.X == position.X).FirstOrDefault());
            }
            return(null);
        }
Example #13
0
        public void SetBackgroundCell(Color color = default(Color))
        {
            if (BackgroundCages.Count == 0)
            {
                for (int i = 0; i < CellSize.X; i++)
                {
                    for (int j = 0; j < CellSize.Y; j++)
                    {
                        Cage background = new Cage();
                        background.IsBackground = true;
                        background.Position     = new Point(i, j);
                        background.BackColor    = Equals(color, default(Color)) ? Color.Gainsboro : color;
                        background.Size         = CageSize;
                        //background.Location = new Point(Location.Y + j * CageSize.Width + (CageDistance * j), Location.X + i * CageSize.Height + (CageDistance * i));
                        //background.PreviewKeyDown += Cage_PreviewKeyDown;

                        BackgroundCages.Add(background);
                        MainForm.Controls.Add(background);
                    }
                }
            }
            IsBackground = true;
        }
Example #14
0
 private bool CheckWays()
 {
     if (Cages.Count == CellSize.X * CellSize.Y)
     {
         for (int i = 0; i < CellSize.X; i++)
         {
             for (int j = 0; j < CellSize.Y; j++)
             {
                 Cage cage = Cages.Where(c => c.Position.X == j && c.Position.Y == i).FirstOrDefault();
                 if (cage == null)
                 {
                     return(true);
                 }
                 else if (CheckCagesCanMove())
                 {
                     return(true);
                 }
             }
         }
         return(false);
     }
     return(true);
 }
Example #15
0
        private void GameLogic(Move move)
        {
            //if (Anim.IsProcess)
            //    return;

            bool isLeft     = move == Move.Left;
            bool isRight    = move == Move.Right;
            bool isUp       = move == Move.Up;
            bool isDown     = move == Move.Down;
            bool isMoveDone = false;

            for (int i = 0; isLeft || isRight ? i < CellSize.X : i < CellSize.Y; i++)
            {
                for (int j = isRight ? CellSize.Y - 1 : isDown ? CellSize.X - 1 : 0; isRight || isDown ? j >= 0 : isUp?j < CellSize.X : j < CellSize.Y; j += isRight || isDown ? -1 : 1)
                {
                    Cage cage = Cages.Where(c => c.Position.X == (isUp || isDown ? i : j) && c.Position.Y == (isUp || isDown ? j : i)).FirstOrDefault();
                    if (cage != null && cage.Number != -1 && CheckCageCanMove(cage, move))
                    {
                        for (int k = 1; isLeft || isRight ? k < CellSize.Y : k < CellSize.X; k++)
                        {
                            Cage nearlyCage = CheckCageNearly(cage, move);
                            if (CheckCageCanMove(cage, move))
                            {
                                if (nearlyCage?.Number == cage.Number && nearlyCage.CanMix)
                                {
                                    cage.Position = new Point(cage.Position.X + (isLeft ? -1 : isRight ? 1 : 0), cage.Position.Y + (isUp ? -1 : isDown ? 1 : 0));
                                    cage.Number  *= 2;
                                    cage.CanMix   = false;

                                    nearlyCage.Number   = -1;
                                    nearlyCage.Name     = cage.Name;
                                    nearlyCage.Position = new Point(-2, -2);

                                    Anim = new Animation(CageDistance, 20, IsAnimation, Animation_OnMix);
                                    Anim.Move(move, cage, true);
                                    Anim.Mix(cage);

                                    isMoveDone = true;
                                    break;
                                }
                                else if (nearlyCage == null)
                                {
                                    cage.Position = new Point(cage.Position.X + (isLeft ? -1 : isRight ? 1 : 0), cage.Position.Y + (isUp ? -1 : isDown ? 1 : 0));
                                    Anim          = new Animation(CageDistance, 20, IsAnimation);
                                    Anim.Move(move, cage, false);
                                    isMoveDone = true;
                                }
                                else
                                {
                                    break;
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
            }
            Cages.ForEach(c => c.CanMix = true);
            //Cages.ForEach(c=> {
            //    args.History[args.History.Count - 1].Where(c2 => c2.Name == c.Name).FirstOrDefault().Position = c.Position;
            //});
            if (isMoveDone)
            {
                AddRandomCage();
                CountMove++;
                args.History.Add(new List <Cage>(Cages));
                OnMove?.Invoke(args);
            }
            if (!CheckWays())
            {
                OnNoWays?.Invoke(args);
                IsWin = false;
                GameOver();
            }
        }