public static IEnumerable <OwnedLocation> AssignOwners(Map map) { var queue = new Queue <OwnedLocation>(); for (int i = 0; i < map.Players.Length; i++) { queue.Enqueue(new OwnedLocation(i, map.Players[i], 0)); } var way = new HashSet <Point>(); while (queue.Count != 0) { var own = queue.Dequeue(); way.Add(own.Location); foreach (var move in own.Location.NearbyMoves(map).Where(p => !way.Contains(p))) { if (queue.Any(l => l.Location == move)) { continue; } var next = new OwnedLocation(own.Owner, move, own.Distance + 1); queue.Enqueue(next); } yield return(new OwnedLocation(own.Owner, own.Location, own.Distance)); } yield break; }
public static IEnumerable <OwnedLocation> AssignOwners(Map map) { var visited = new HashSet <Point>(); var queues = map.Players.Select(_ => new Queue <OwnedLocation>()).ToArray(); for (var i = 0; i < map.Players.Length; ++i) { var playerPosition = map.Players[i]; var ownedLocation = new OwnedLocation(i, playerPosition, 0); visited.Add(playerPosition); queues[i].Enqueue(ownedLocation); yield return(ownedLocation); } while (queues.Any(x => x.Any())) { foreach (var queue in queues) { foreach (var ownedLocation in DoNextMove(map, visited, queue)) { yield return(ownedLocation); } } } }
private static void MarksAround(Queue <OwnedLocation> queue, OwnedLocation current) { //for (var dy = -1; dy <= 1; dy++) // for (var dx = -1; dx <= 1; dx++) // if ((dy == 0 || dx == 0) && dy != dx) // { // var next = new OwnedLocation(current.Owner, // new Point(current.Location.X + dx, current.Location.Y + dy), // current.Distance + 1); // queue.Enqueue(next); // } var range = Enumerable.Range(-1, 3); var marks = range.SelectMany(dx => range.Select(dy => (dx, dy))) .Where(loc => (loc.dx == 0 || loc.dy == 0) && loc.dx != loc.dy) .Select(loc => new OwnedLocation( current.Owner, new Point(current.Location.X + loc.dx, current.Location.Y + loc.dy), current.Distance + 1)); foreach (var mark in marks) { queue.Enqueue(mark); } }
private IEnumerable <OwnedLocation> PlacePlayers() { for (var i = 0; i < map.Players.Count(); i++) { var pos = map.Players[i]; var loc = new OwnedLocation(i, pos, 0); fronts[i].Enqueue(loc); visited.Add(pos); yield return(loc); } }
static void AroundMarks(Queue <OwnedLocation> turn, OwnedLocation presentStep) { for (var a = -1; a <= 1; a++) { for (var b = -1; b <= 1; b++) { if ((a == 0 || b == 0) && a != b) { turn.Enqueue(new OwnedLocation(presentStep.Owner, new Point(presentStep.Location.X + b, presentStep.Location.Y + a), presentStep.Distance + 1)); } } } }
private IEnumerable <OwnedLocation> ExpandFront(Queue <OwnedLocation> front) { for (var count = front.Count(); count > 0; --count) { OwnedLocation settle = front.Dequeue(); foreach (var pos in AvailablePositions(settle.Location) .Where(p => !visited.Contains(p))) { var newLoc = new OwnedLocation(settle.Owner, pos, settle.Distance + 1); front.Enqueue(newLoc); visited.Add(newLoc.Location); yield return(newLoc); } } }
private static bool TryGoInDirection(Map map, OwnedLocation position, MoveDirection direction, out OwnedLocation potentialPoint) { potentialPoint = new OwnedLocation( position.Owner, position.Location + OffsetToDirection[direction], position.Distance + 1); if (!map.InBounds(potentialPoint.Location) || map.Maze[potentialPoint.Location.X, potentialPoint.Location.Y] != MapCell.Empty) { return(false); } map.Maze[potentialPoint.Location.X, potentialPoint.Location.Y] = MapCell.Wall; return(true); }
private static void AddNearbyPoints(Queue <OwnedLocation> queue, OwnedLocation currentLocation) { for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { if ((i == 0 || j == 0) && i != j) { var newLocation = new OwnedLocation( currentLocation.Owner, new Point(currentLocation.Location.X + j, currentLocation.Location.Y + i), currentLocation.Distance + 1); queue.Enqueue(newLocation); } } } }
private static void FindNextPoint(Map map, OwnedLocation ownerPoint, HashSet <Point> visited, Queue <OwnedLocation> queue) { for (var dx = -1; dx <= 1; dx++) { for (int dy = -1; dy <= 1; dy++) { var nextPoint = new Point(ownerPoint.Location.X + dx, ownerPoint.Location.Y + dy); if (dx != 0 && dy != 0 || visited.Contains(nextPoint) || !map.InBounds(nextPoint) || map.Maze[nextPoint.X, nextPoint.Y] != MapCell.Empty) { continue; } queue.Enqueue(new OwnedLocation(ownerPoint.Owner, nextPoint, ownerPoint.Distance + 1)); visited.Add(nextPoint); } } }
public static IEnumerable <OwnedLocation> AssignOwners(Map map) { var queue = new Queue <OwnedLocation>(); var visited = new HashSet <Point>(); for (int owner = 0; owner < map.Players.Length; owner++) { var startOwner = new OwnedLocation(owner, map.Players[owner], 0); queue.Enqueue(startOwner); visited.Add(startOwner.Location); } while (queue.Count != 0) { var ownerPoint = queue.Dequeue(); yield return(ownerPoint); FindNextPoint(map, ownerPoint, visited, queue); } }
private static IEnumerable <OwnedLocation> DoNextMove( Map map, HashSet <Point> visited, Queue <OwnedLocation> queue) { var count = queue.Count(); for (var i = 0; i < count; ++i) { var ownedLocation = queue.Dequeue(); var nextPositions = GetPossibleNextPositions(map, visited, ownedLocation.Location); foreach (var position in nextPositions) { var nextOwnedLocation = new OwnedLocation(ownedLocation.Owner, position, ownedLocation.Distance + 1); if (ownedLocation.Distance >= nextOwnedLocation.Distance) { continue; } visited.Add(nextOwnedLocation.Location); queue.Enqueue(nextOwnedLocation); yield return(nextOwnedLocation); } } }
private static bool IsInvalidLocation(OwnedLocation current, Map map) => !map.InBounds(current.Location) || map.Maze[current.Location.X, current.Location.Y] != MapCell.Empty;