Exemplo n.º 1
0
        /// <summary>
        /// Makes this <see cref="Snake"/> crawl to the next <see cref="Land"/>.
        /// </summary>
        public void Move()
        {
            var NewHead = map.GetNextTo(Head, HeadsTo);

            try
            {
                NewHead.MoveSnakeIn(this);
            }
            catch (AlreadyOccupiedLandException e)
            {
                if (e.FiredMe == Space.ElementAt(0) || e.FiredMe == Space.ElementAt(1))
                {
                    return;
                }
                if (Length == map.Locations.Length)
                {
                    this.SnakeHasCapturedTheWholeMap?.Invoke(this, null);
                    return;
                }
                throw e;
            }
            Queue <Land> NewSpace = new Queue <Land>();

            NewSpace.Enqueue(NewHead);
            if (NumberOfTailsToAdd > 0)
            {
                while (Space.Count > 0)
                {
                    NewSpace.Enqueue(Space.Dequeue());
                }
                Space = NewSpace;
                --NumberOfTailsToAdd;
                Program.gamelog.AddLog("Snake moved to " + NewHead.Location.ToString());
                OnSnakeMoves?.Invoke(this, Head);
                return;
            }
            while (Space.Count > 1)
            {
                NewSpace.Enqueue(Space.Dequeue());
            }
            LastOccupied = Space.Dequeue();
            Space        = NewSpace;
            if (!(LastOccupied is null))
            {
                LastOccupied.MoveSnakeOut(this);
            }
            Program.gamelog.AddLog("Snake moved to " + NewHead.Location.ToString());
            Last_Move = DateTime.Now;
            OnSnakeMoves?.Invoke(this, Head);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Initialize a new game.
 /// </summary>
 /// <param name="Caller"></param>
 /// <param name="map"></param>
 public static void Initialize(Form Caller, Map map, Func <int, int>[] foodChanger = null)
 {
     if (GameRuning)
     {
         throw new InvalidOperationException("The Game Handle cant initialize when the game is running!");
     }
     FC             = foodChanger;
     GameUI         = Caller;
     GameHandle.map = map;
     snake          = new Snake.Snake(map.GetRandomLand(1));
     mapSize        = new Size(map.Width, map.Height);
     GameRuning     = true;
     GameT          = new Thread(() => {
         Game_Handle();
     });
     GameT.Start();
     snake.OnSnakeMoves += (x, y) => { OnSnakeMoves?.Invoke(snake, y); };
     snake.OnSnakeEats  += (s, l) => { OnSnakeEats?.Invoke(snake, l); };
     snake.SnakeHasCapturedTheWholeMap += (s, e) => { GameFinished?.Invoke(snake, e); };
     OnGameInitialize?.Invoke(Caller, map);
 }