Beispiel #1
0
 internal void ChangeDirectionOfPacMan(Direction direction)
 {
     if (direction != PacMan.Direction)
     {
         PacMan = PacMan.WithNewDirection(direction);
     }
 }
Beispiel #2
0
 public GameSettings(
     int width,
     int height,
     IReadOnlyCollection <CellLocation> walls,
     IReadOnlyCollection <CellLocation> coins,
     IReadOnlyCollection <CellLocation> powerPills,
     IReadOnlyDictionary <CellLocation, CellLocation> portals,
     PacMan pacMan,
     IReadOnlyCollection <Ghost> ghosts,
     IReadOnlyCollection <CellLocation> doors,
     IReadOnlyCollection <CellLocation> ghostHouse,
     CellLocation fruit)
 {
     Width      = width;
     Height     = height;
     Portals    = portals;
     PacMan     = pacMan;
     Ghosts     = ghosts;
     Walls      = walls;
     Coins      = coins;
     PowerPills = powerPills;
     Doors      = doors;
     GhostHouse = ghostHouse;
     Fruit      = fruit;
 }
Beispiel #3
0
        public void ChangeDirection(Direction direction)
        {
            var nextSpace = PacMan.Location + direction;

            if (!Walls.Contains(nextSpace))
            {
                PacMan = PacMan.WithNewDirection(direction);
            }
        }
Beispiel #4
0
        async Task IGameActions.MovePacMan(BehaviorContext <GameState, Tick> context, GameStateMachine gameStateMachine)
        {
            var newPacMan = PacMan.Move();

            if (_settings.Portals.TryGetValue(newPacMan.Location, out var portal))
            {
                newPacMan = PacMan.WithNewX(portal.X).WithNewY(portal.Y);
                newPacMan = newPacMan.Move();
            }

            if (!_settings.Walls.Contains(newPacMan.Location))
            {
                PacMan = newPacMan;
            }
            var died = false;

            if (TryHasDied(out var ghost))
            {
                died = !ghost !.Edible;

                await context.Raise(gameStateMachine.GhostCollision, new GhostCollision(ghost));
            }

            if (!died && Coins.Contains(newPacMan.Location))
            {
                var newCollectedCoins = new List <CellLocation>(_collectedCoins)
                {
                    (newPacMan.Location)
                };
                _collectedCoins = newCollectedCoins;

                await context.Raise(gameStateMachine.CoinEaten);
            }
            else if (!died && PowerPills.Contains(newPacMan.Location))
            {
                var newCollectedPowerPills = new List <CellLocation>(_collectedPowerPills)
                {
                    (newPacMan.Location)
                };
                _collectedPowerPills = newCollectedPowerPills;

                await context.Raise(gameStateMachine.PowerPillEaten);
            }
        }
Beispiel #5
0
        public static IGameSettings Load(string board)
        {
            var allRows      = board.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
            var rows         = allRows.Where(line => !line.StartsWith("{")).ToArray();
            var instructions = allRows.Where(line => line.StartsWith("{")).ToArray();

            var height = rows.Length;
            var width  = 0;

            var homeLocations = FindHomeLocations(instructions);

            var    coins       = new List <CellLocation>();
            var    powerPills  = new List <CellLocation>();
            var    walls       = new List <CellLocation>();
            var    doors       = new List <CellLocation>();
            var    portalParts = new List <CellLocation>();
            var    ghosts      = new List <Ghost>();
            PacMan?pacMan      = null;

            for (int rowNumber = 0; rowNumber < height; rowNumber++)
            {
                var row = rows[rowNumber];
                for (int columnNumber = 0; columnNumber < row.Length; columnNumber++)
                {
                    var location = new CellLocation(columnNumber - 1, rowNumber);
                    switch (row[columnNumber])
                    {
                    case 'B':
                        var homeB = homeLocations[GhostNames.Blinky];
                        ghosts.Add(new Ghost(GhostNames.Blinky,
                                             location,
                                             Direction.Left,
                                             new CellLocation(homeB.X, homeB.Y),
                                             new DirectToStrategy(new DirectToPacManLocation())));
                        break;

                    case 'P':
                        var homeP = homeLocations[GhostNames.Pinky];
                        ghosts.Add(new Ghost(GhostNames.Pinky,
                                             location,
                                             Direction.Left,
                                             new CellLocation(homeP.X, homeP.Y),
                                             new DirectToStrategy(new DirectToExpectedPacManLocation())));
                        break;

                    case 'I':
                        var homeI = homeLocations[GhostNames.Inky];
                        ghosts.Add(new Ghost(GhostNames.Inky,
                                             location,
                                             Direction.Left,
                                             new CellLocation(homeI.X, homeI.Y),
                                             new DirectToStrategy(new InterceptPacManLocation(GhostNames.Blinky))));
                        break;

                    case 'C':
                        var homeC = homeLocations[GhostNames.Clyde];
                        ghosts.Add(new Ghost(GhostNames.Clyde,
                                             location,
                                             Direction.Left,
                                             new CellLocation(homeC.X, homeC.Y),
                                             new DirectToStrategy(new StaysCloseToPacManLocation(GhostNames.Clyde))));
                        break;

                    case '▲':
                        pacMan = new PacMan(location, Direction.Up);
                        break;

                    case '▼':
                        pacMan = new PacMan(location, Direction.Down);
                        break;

                    case '►':
                        pacMan = new PacMan(location, Direction.Right);
                        break;

                    case '◄':
                        pacMan = new PacMan(location, Direction.Left);
                        break;

                    case 'X':
                        walls.Add(location);
                        break;

                    case '-':
                        doors.Add(location);
                        break;

                    case 'T':
                        portalParts.Add(location);
                        break;

                    case '.':
                        coins.Add(location);
                        break;

                    case '*':
                        powerPills.Add(location);
                        break;

                    default:
                        break;
                    }

                    width = Math.Max(width, row.Length);
                }
            }

            if (portalParts.Count() != 0 && portalParts.Count() != 2)
            {
                throw new Exception("Unexpected number of portals");
            }

            var portals = new Dictionary <CellLocation, CellLocation>();

            if (portalParts.Any())
            {
                portals.Add(portalParts[0], portalParts[1]);
                portals.Add(portalParts[1], portalParts[0]);
            }

            if (pacMan is null)
            {
                throw new Exception("Pacman seems to be missing from the board.");
            }

            return(new GameSettings(width - 2, height, walls, coins, powerPills, portals, pacMan, ghosts, doors));
        }
Beispiel #6
0
 internal void ReplacePacMan(PacMan pacMan)
 {
     PacMan = pacMan;
 }
Beispiel #7
0
 internal void MovePacManTo(CellLocation newPacManLocation)
 {
     PacMan = PacMan.WithNewLocation(newPacManLocation);
 }
Beispiel #8
0
 void IGameActions.MovePacManHome()
 {
     PacMan = PacMan.SetToHome();
 }