Exemplo n.º 1
0
 private void PortalGhost(PacmanCoordinate pos, int i)
 {
     if (Board.GetTile(pos) == PacmanBoard.Tile.portal)
     {
         Ghosts[i].Location = new PacmanCoordinate(Board.GetCorrespondingPortal(pos));
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Helper method for editing Ghost's Target Location and calling necessary moves
        /// </summary>
        /// <param name="i">Ghost Index</param>
        private void ProcessGhostMove(int i)
        {
            var GhostTargetLocation = new PacmanCoordinate(Pacman.Location);

            switch (i)
            {
            case 0:
                break;

            case 1:
                GhostTargetLocation.Xpos += 5;
                break;

            case 2:
                GhostTargetLocation.Xpos -= 2;
                break;

            case 3:
                GhostTargetLocation.Ypos += 3;
                break;
            }
            if (Ghosts[i].Location.Xpos % 1 == 0 && Ghosts[i].Location.Ypos % 1 == 0)
            {
                PortalGhost(Ghosts[i].Location, i);
                Ghosts[i].Facing = Ghosts[i].DetermineGhostMove(Board.PotentialDirections(Ghosts[i].Location), GhostTargetLocation, Score);
            }

            Ghosts[i].Move();
        }
Exemplo n.º 3
0
 public Tile GetTile(PacmanCoordinate p)
 {
     if (p.Xpos < 0 || p.Xpos >= Width || p.Ypos < 0 || p.Ypos >= Height)
     {
         return(Tile.wall);
     }
     return(Board[p.XRoundPos, p.YRoundPos]);
 }
Exemplo n.º 4
0
 public PacmanGhost(int number)
 {
     Facing       = PacmanPacman.Direction.start;
     IsDead       = true;
     IsVulnerable = false;
     DeadCounter  = 50 * number;
     Location     = new PacmanCoordinate(13m, 12m);
 }
Exemplo n.º 5
0
        public List <PacmanPacman.Direction> PotentialDirections(PacmanCoordinate pos)
        {
            List <PacmanPacman.Direction> directions = new List <PacmanPacman.Direction>();

            for (int i = 1; i < 5; i++)
            {
                if (ValidMove((PacmanPacman.Direction)i, pos))
                {
                    directions.Add((PacmanPacman.Direction)i);
                }
            }
            return(directions);
        }
Exemplo n.º 6
0
 // Called after pacman enters a consumable (fruit, dot, or powerup) tile
 public void UpdateTile(PacmanCoordinate pos)
 {
     if (Board[pos.XRoundPos, pos.YRoundPos] == Tile.dot || Board[pos.XRoundPos, pos.YRoundPos] == Tile.powerUp)
     {
         DotCount--;
         Board[pos.XRoundPos, pos.YRoundPos] = Tile.blank;
     }
     else if (Board[pos.XRoundPos, pos.YRoundPos] == Tile.fruit)
     {
         Board[pos.XRoundPos, pos.YRoundPos] = Tile.blank;
     }
     else
     {
         Console.Error.WriteLine("Error: PacmanBoard.UpdateTile tried to update a non consumable tile");
     }
 }
Exemplo n.º 7
0
 /// <returns>Coordinates to the other portal on the map</returns>
 public PacmanCoordinate GetCorrespondingPortal(PacmanCoordinate p)
 {
     p.Xpos = p.XRoundPos;
     p.Ypos = p.YRoundPos;
     if (p == Portals[0])
     {
         return(Portals[1] - new PacmanCoordinate(1, 0));
     }
     else if (p == Portals[1])
     {
         return(Portals[0] + new PacmanCoordinate(1, 0));
     }
     else
     {
         Console.Error.WriteLine($"Error: Coordinate {p.Xpos}, {p.Ypos} is not a designated portal");
         return(p);
     }
 }
Exemplo n.º 8
0
        /// <summary> This method is called after a move is made by pacman to determine whether or not to update the board.
        /// It also calls update score if a fruit or dot was consumed </summary>
        private void CheckTile(PacmanCoordinate pos)
        {
            switch (Board.GetTile(pos))
            {
            case PacmanBoard.Tile.portal:
                Pacman.Location = new PacmanCoordinate(Board.GetCorrespondingPortal(pos));
                break;

            case PacmanBoard.Tile.powerUp:
                foreach (var g in Ghosts)
                {
                    if (!g.IsVulnerable && !g.IsDead)
                    {
                        g.IsVulnerable   = true;
                        PoweredUpCounter = 150;
                    }
                }
                UpdateScore(pos);
                Board.UpdateTile(pos);
                if (Board.DotCount <= 0)
                {
                    ResetBoard();
                }
                GhostScoreMultiplier = 1;
                break;

            case PacmanBoard.Tile.dot:
            case PacmanBoard.Tile.fruit:
                UpdateScore(pos);
                Board.UpdateTile(pos);
                if (Board.DotCount <= 0)
                {
                    ResetBoard();
                }
                break;

            default:
                break;
            }
            return;
        }
Exemplo n.º 9
0
        /// <summary>
        /// Updates score based on tiles that pacman enters
        /// </summary>
        /// <param name="t">Tile entered by pacman</param>
        private void UpdateScore(PacmanCoordinate pos)
        {
            var t = Board.GetTile(pos);

            if (t == PacmanBoard.Tile.dot)
            {
                Score += 10;
                CurrentGameEvent.Add(new KeyValuePair <EventType, string>(EventType.Dot, pos.FloorToString() + " 10"));
            }
            else if (t == PacmanBoard.Tile.fruit)
            {
                Score += 100;
                CurrentGameEvent.Add(new KeyValuePair <EventType, string>(EventType.Fruit, pos.FloorToString() + " 100"));
            }
            else if (t == PacmanBoard.Tile.powerUp)
            {
                Score += 50;
                CurrentGameEvent.Add(new KeyValuePair <EventType, string>(EventType.PowerUp, pos.FloorToString() + " 50"));
            }
            return;
        }
Exemplo n.º 10
0
        /// <summary>
        /// Checks the result of executing a direction based on the position of an entity
        /// </summary>
        /// <param name="d">Direction entity is trying to move towards</param>
        /// <param name="pos">Position of entity currently</param>
        /// <returns>Whether a move is valid</returns>
        public bool ValidMove(PacmanPacman.Direction d, PacmanCoordinate pos)
        {
            switch (d)
            {
            case PacmanPacman.Direction.start:
                return(true);

            case PacmanPacman.Direction.up:
                return(GetTile(pos.XRoundPos, pos.YRoundPos - 1) != Tile.wall);

            case PacmanPacman.Direction.down:
                return(GetTile(pos.XRoundPos, pos.YRoundPos + 1) != Tile.wall);

            case PacmanPacman.Direction.left:
                return(GetTile(pos.XRoundPos - 1, pos.YRoundPos) != Tile.wall);

            case PacmanPacman.Direction.right:
                return(GetTile(pos.XRoundPos + 1, pos.YRoundPos) != Tile.wall);
            }
            return(false);
        }
Exemplo n.º 11
0
 public PacmanPacman()
 {
     Location = new PacmanCoordinate(13m, 17m);
     Facing   = Direction.right;
     Lives    = 3;
 }
Exemplo n.º 12
0
        /// <summary>
        /// Determines the Ghosts moves based on a target position prioritizing minimizing distance
        /// Has a chance to choose a random valid move
        /// </summary>
        /// <param name="possible">List of valid directions</param>
        /// <param name="pos">Target Coordinate</param>
        /// <param name="chance">Percent Chance the move will be random</param>
        /// <returns></returns>
        public PacmanPacman.Direction DetermineGhostMove(List <PacmanPacman.Direction> possible, PacmanCoordinate pos, int score)
        {
            // f(x) = x / -250 + 100
            // Dividend should change after testing
            var chance = (score / DividendForChanceOfHinderingMove) + StartingPercentage;
            var random = new Random();
            var chanceForRandomMove = random.Next(101);
            var difference          = Location - pos;

            if (possible.Count > 1 && possible.Contains(PacmanPacman.InverseDirection(Facing)))
            {
                possible.Remove(PacmanPacman.InverseDirection(Facing));
            }
            if (chanceForRandomMove >= chance)
            {
                return(possible[random.Next(0, possible.Count)]);
            }
            if (possible.Count > 0)
            {
                if (Math.Abs(difference.Xpos) > Math.Abs(difference.Ypos))
                {
                    if (difference.Xpos > 0)
                    {
                        if (possible.Contains(PacmanPacman.Direction.left))
                        {
                            return(PacmanPacman.Direction.left);
                        }
                    }
                    else
                    {
                        if (possible.Contains(PacmanPacman.Direction.right))
                        {
                            return(PacmanPacman.Direction.right);
                        }
                    }
                }
                else
                {
                    if (difference.Ypos > 0)
                    {
                        if (possible.Contains(PacmanPacman.Direction.up))
                        {
                            return(PacmanPacman.Direction.up);
                        }
                    }
                    else
                    {
                        if (possible.Contains(PacmanPacman.Direction.down))
                        {
                            return(PacmanPacman.Direction.down);
                        }
                    }
                }
                return(possible[random.Next(0, possible.Count)]);
            }
            return(PacmanPacman.Direction.start);
        }