コード例 #1
0
ファイル: Snake.cs プロジェクト: dommie123/CSIS2410-Snake
        /// <summary>
        /// Method <c>Move</c> "moves" the snake
        /// </summary>
        public void Move()
        {
            Console.SetCursorPosition((head.Key + 4) * 2, head.Value + 2);
            CircularQueue <KeyValuePair <int, int> > segments2 = new CircularQueue <KeyValuePair <int, int> >(Length);
            KeyValuePair <int, int> tempSeg = head;

            if (Segments.Size >= Length)
            {
                KeyValuePair <int, int> del = Segments.Dequeue();

                DeleteLeftovers(del.Key + 1, del.Value);
                DeleteLeftovers(del.Key - 1, del.Value);
                DeleteLeftovers(del.Key, del.Value + 1);
                DeleteLeftovers(del.Key, del.Value - 1);
            }
            else
            {
                Segments.Size++;
            }

            for (int i = 0; i < Segments.Count;)
            {
                segments2.Enqueue(Segments.Dequeue());
            }


            switch (Compass)
            {
            case Direction.UP:
                if (HitEgg())
                {
                    EatEgg();
                    Grid.Tiles[tempSeg.Key, tempSeg.Value - 1] = 1;
                }
                else if (HitObstacle() || HitSelf())
                {
                    Speed  = 0;
                    IsDead = true;
                }
                segments2.Enqueue(new KeyValuePair <int, int>(tempSeg.Key, tempSeg.Value - 1));
                break;

            case Direction.LEFT:
                if (HitEgg())
                {
                    EatEgg();
                    Grid.Tiles[tempSeg.Key - 1, tempSeg.Value] = 1;
                }
                else if (HitObstacle() || HitSelf())
                {
                    Speed  = 0;
                    IsDead = true;
                }
                segments2.Enqueue(new KeyValuePair <int, int>(tempSeg.Key - 1, tempSeg.Value));
                break;

            case Direction.RIGHT:
                if (HitEgg())
                {
                    EatEgg();
                    Grid.Tiles[tempSeg.Key + 1, tempSeg.Value] = 1;
                }
                else if (HitObstacle() || HitSelf())
                {
                    Speed  = 0;
                    IsDead = true;
                }
                segments2.Enqueue(new KeyValuePair <int, int>(tempSeg.Key + 1, tempSeg.Value));
                break;

            case Direction.DOWN:
                if (HitEgg())
                {
                    EatEgg();
                    Grid.Tiles[tempSeg.Key, tempSeg.Value + 1] = 1;
                }
                else if (HitObstacle() || HitSelf())
                {
                    Speed  = 0;
                    IsDead = true;
                }
                segments2.Enqueue(new KeyValuePair <int, int>(tempSeg.Key, tempSeg.Value + 1));
                break;
            }

            if (Length > 0)
            {
                Segments = segments2;
            }
            head = Segments.Last();
        }