Example #1
0
            private bool Attack(Map map)
            {
                var adjacentUnits = Position.Adjacent()
                                    .Select(p => map.Units.FirstOrDefault(u => Equals(p, u.Position) && u.Type != Type))
                                    .Where(u => u != null)
                                    .ToList();

                if (!adjacentUnits.Any())
                {
                    return(false);
                }

                var lowestHitPoints     = adjacentUnits.Min(u => u.HitPoints);
                var lowestHitPointUnits = adjacentUnits.Where(u => u.HitPoints == lowestHitPoints).ToList();

                Attack(map, lowestHitPointUnits.OrderByReading().First());
                return(true);
            }
Example #2
0
            internal void Move(Map map)
            {
                var nearestPosition = NearestPosition(map);

                if (nearestPosition == null)
                {
                    return;
                }
                var distances = new List <(Position P, int D)>
                {
                    (nearestPosition, 0)
                };
                var i = -1;

                while (++i < distances.Count)
                {
                    var(position, distance) = distances[i];
                    var adjacentPositions = position.Adjacent().ToList();
                    foreach (var adjacentPosition in adjacentPositions)
                    {
                        if (distances.All(d => !Equals(d.P, adjacentPosition)) &&
                            map.IsOpen(adjacentPosition))
                        {
                            distances.Add((adjacentPosition, distance + 1));
                        }
                    }
                }

                var possiblePositions = Position.Adjacent()
                                        .Select(a => distances.SingleOrDefault(d => Equals(d.P, a)))
                                        .Where(d => d.P != null)
                                        .OrderBy(d => d.D)
                                        .ToList();
                var nearestPositions = possiblePositions.TakeWhile(d => d.D == possiblePositions.First().D);

                Position = nearestPositions.Select(d => d.P).OrderByReading().First();
            }