コード例 #1
0
ファイル: Scared.cs プロジェクト: jacobr98/PacMan-JR-DM-KB
        /// <summary>
        /// This method is invoked to move the scared Ghost to the random available tile.
        /// Everytime a Ghost moves, we have to do two things: update the Ghost's Position
        /// and update the Ghosts's Direction. This indicates the direction in which it is moving,
        /// and it is required to make sure that the Ghosts doesn't turn back to it's previous
        /// position (i.e., to avoid 180 degree turns) (used by the Maze class's GetAvailableNeighbours
        /// method)
        /// </summary>
        public void Move()
        {
            Tile        current = maze[(int)ghost.Position.X, (int)ghost.Position.Y];
            List <Tile> places  = maze.GetAvailableNeighbours(ghost.Position, ghost.Direction);
            int         num     = places.Count;

            if (num == 0)
            {
                throw new Exception("Nowhere to go");
            }

            Random rand   = new Random();
            int    choice = rand.Next(num);

            //determine direction
            if (places[choice].Position.X == ghost.Position.X + 1)
            {
                ghost.Direction = Direction.Right;
            }
            else if (places[choice].Position.X == ghost.Position.X - 1)
            {
                ghost.Direction = Direction.Left;
            }
            else if (places[choice].Position.Y == ghost.Position.Y - 1)
            {
                ghost.Direction = Direction.Up;
            }
            else
            {
                ghost.Direction = Direction.Down;
            }
            ghost.Position = places[choice].Position;
        }
コード例 #2
0
ファイル: Predict.cs プロジェクト: jacobr98/PacMan-JR-DM-KB
        /// <summary>
        /// Finds the nearest path to ambush pacman
        /// </summary>
        public Vector2 findAdjacentTarget()
        {
            Tile        current = maze[(int)ghost.Position.X, (int)ghost.Position.Y];
            List <Tile> places  = maze.GetAvailableNeighbours(ghost.Position, ghost.Direction);
            int         num     = places.Count;

            if (num == 0)
            {
                throw new Exception("Nowhere to go");
            }

            Tile shortestDistance = places[0];

            for (int i = 1; i < places.Count(); i++)
            {
                if (places[i].GetDistance(target) < shortestDistance.GetDistance(target))
                {
                    shortestDistance = places[i];
                }
            }

            if (shortestDistance.Position.X == ghost.Position.X + 1)
            {
                ghost.Direction = Direction.Right;
            }
            else if (shortestDistance.Position.X == ghost.Position.X - 1)
            {
                ghost.Direction = Direction.Left;
            }
            else if (shortestDistance.Position.Y == ghost.Position.Y - 1)
            {
                ghost.Direction = Direction.Up;
            }
            else
            {
                ghost.Direction = Direction.Down;
            }

            return(shortestDistance.Position);
        }