public void MoveEast_WithObstacle_ShouldWork() { var depart = new Point(5, 5); var obstacle = new Point(8, 5); var world = AWorld.GivenWorld() .WithPlayer(depart.X, depart.Y) .WithCaddy(0, 1) .WithPlayer(obstacle.X, obstacle.Y) .WithCaddy(0, 3); var destination = new Point(11, 5); Point lastPosition; List<Direction> directions = ShortestPathTests.WhenMoving(world, depart, destination, out lastPosition, 3, 20); Assert.Equal(destination, lastPosition); Assert.Equal(new[] { Direction.N, Direction.N, Direction.N, Direction.E, Direction.E, Direction.E, Direction.E, Direction.S, Direction.E, Direction.S, Direction.E, Direction.S }, directions); }
public void LongMoveWestSouth_ShouldSucceed() { var depart = new Point(5, 5); var world = AWorld.GivenWorld() .WithPlayer(depart.X, depart.Y) .WithCaddy(0, 1); var destination = new Point(0, 7); Point lastPosition; List<Direction> directions = ShortestPathTests.WhenMoving(world, depart, destination, out lastPosition); Assert.Equal(destination, lastPosition); Assert.Equal(new[] { Direction.O, Direction.O, Direction.O, Direction.O, Direction.O, Direction.S, Direction.S }, directions); }
private static List<Direction> WhenMoving( WorldState world, Point depart, Point destination, out Point lastPosition, int cost = 0, int maxIteration = int.MinValue) { var distance = destination.Dist(depart); maxIteration = maxIteration < distance ? distance : maxIteration; var directions = new List<Direction>(); int i = 0; lastPosition = depart; while (lastPosition != destination && i < maxIteration) { var map = new DistanceMap(world, 0, cost, null); map.BuildAllPath(); var dir = map.MoveTo(destination); directions.Add(dir.Value); var next = lastPosition.Move(dir.Value); world.Players[0] = new Player(0, next.X, next.Y, 0, PlayerState.Playing); lastPosition = next; i++; } return directions; }
private bool IsValidMove(int player, Point npos) { if (npos.X < 0 || npos.Y < 0 || npos.X >= WorldState.MAP_WIDTH || npos.Y >= WorldState.MAP_HEIGHT) { return false; } return this.World.Players.All(p => p.Pos != npos); }