internal Ghost Move(Game game, IReadOnlyGameState gameState) { if (Edible && gameState.TickCounter % 2 == 1) { return(this); } if (GhostWalkingOutOfGhostHouse(game)) { if (game.StartingCoins.Count - game.Coins.Count >= NumberOfCoinsRequiredToExitHouse) { var outDirection = Direction.Up; var target = game.Doors.First().Above; if (target.X < Location.X) { outDirection = Direction.Left; } else if (target.X > Location.X) { outDirection = Direction.Right; } var newGhostLocation = Location + outDirection; return(WithNewLocationAndDirection(newGhostLocation, outDirection)); } else { return(this); } } var nextDirection = CurrentStrategy.GetNextDirection(this, game); if (nextDirection is Direction newDirection) { var newGhostLocation = Location + newDirection; if (game.Portals.TryGetValue(newGhostLocation, out var otherEndOfThePortal)) { newGhostLocation = otherEndOfThePortal + newDirection; } if (game.GhostHouse.Contains(newGhostLocation)) { return(WithNewLocationAndDirection(newGhostLocation, newDirection) .WithNewStatusAndStrategy(GhostStatus.Alive, ChaseStrategy)); } return(WithNewLocationAndDirection(newGhostLocation, newDirection)); } else { return(this); } }
public Ghost Move(Game game) { var newDirection = CurrentStrategy.GetNextDirection(this, game); var newLocation = newDirection switch { Direction.Up => Location.Above, Direction.Down => Location.Below, Direction.Left => Location.Left, Direction.Right => Location.Right, _ => Location }; return(new Ghost(Name, Home, newLocation, newDirection ?? Direction, ScatterTarget, Strategy, CurrentStrategy, Edible)); }