Beispiel #1
0
        public PacmanGame()
        {
            CurrentGameEvent = new List <KeyValuePair <EventType, string> >();
            Board            = new PacmanBoard();
            CurrentGameEvent.Add(new KeyValuePair <EventType, string>(EventType.BoardReset, GetBoardString()));
            Pacman = new PacmanPacman();
            CurrentGameEvent.Add(new KeyValuePair <EventType, string>(EventType.PacmanLives, $"{Pacman.Lives}"));
            Ghosts = new PacmanGhost[4]
            {
                new PacmanGhost(1),
                new PacmanGhost(2),
                new PacmanGhost(3),
                new PacmanGhost(4)
            };
            Random random = new Random();

            for (var i = 0; i < Ghosts.Length; i++)
            {
                Ghosts[i].Facing = (PacmanPacman.Direction)random.Next(1, 5);
                CurrentGameEvent.Add(new KeyValuePair <EventType, string>(EventType.GhostUpdate, $"{i} {Ghosts[i].GetPosition().ToString()} {Ghosts[i].IsDead} {Ghosts[i].IsVulnerable}"));
            }
            Score = 0;
            CurrentGameEvent.Add(new KeyValuePair <EventType, string>(EventType.Score, $"{Score}"));
            GhostScoreMultiplier = 1;
            PoweredUpCounter     = 0;
            FruitSpawnCounter    = 0;
            TimeStarted          = DateTime.Now;
            GameRunning          = true;
        }
Beispiel #2
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);
        }
Beispiel #3
0
 /// <summary>
 /// Call after all dots and powerUp are consumed
 /// </summary>
 private void ResetBoard()
 {
     Board  = new PacmanBoard();
     Pacman = new PacmanPacman();
     Ghosts = new PacmanGhost[]
     {
         new PacmanGhost(1),
         new PacmanGhost(2),
         new PacmanGhost(3),
         new PacmanGhost(4)
     };
     CurrentGameEvent.Add(new KeyValuePair <EventType, string>(EventType.BoardReset, GetBoardString()));
 }