public void Move_ReturnsValidPosition() { int i = 0; for (int x = 0; x < Constants.MAP_WIDTH; x++) { for (int y = 0; y < Constants.MAP_HEIGHT; y++) { for (int speed = 0; speed <= Constants.MAX_SHIP_SPEED; speed++) { for (int orientation = 0; orientation < 6; orientation++) { foreach (var moveCommand in ShipMoveCommands.all) { var shipPosition = new ShipPosition(new Coord(x, y), orientation, speed); var nextPositions = shipPosition.Apply(moveCommand); nextPositions.Count.Should().Be(2); var fastShipPosition = FastShipPosition.Create(shipPosition); for (int phase = 0; phase < nextPositions.Count; phase++) { if ((i++) % 99 == 0) { Console.Out.WriteLine($"shipPosition: {shipPosition}; moveCommand: {moveCommand}; phase: {phase}; nextPosition: {nextPositions[phase]}"); } var actual = FastShipPosition.ToShipPosition(FastShipPosition.GetPositionAtPhase(FastShipPosition.Move(fastShipPosition, moveCommand), phase)); actual.Should().Be(nextPositions[phase], $"shipPosition: {shipPosition}; moveCommand: {moveCommand}; phase: {phase}; nextPosition: {nextPositions[phase]}"); } } } } } } }
public void DistanceTo_ReturnsValidValue() { for (int xt = 0; xt < Constants.MAP_WIDTH; xt++) { for (int yt = 0; yt < Constants.MAP_HEIGHT; yt++) { for (int x = 0; x < Constants.MAP_WIDTH; x++) { for (int y = 0; y < Constants.MAP_HEIGHT; y++) { for (int speed = 0; speed <= Constants.MAX_SHIP_SPEED; speed++) { for (int orientation = 0; orientation < 6; orientation++) { var shipPosition = new ShipPosition(new Coord(x, y), orientation, speed); var target = new Coord(xt, yt); var fastShipPosition = FastShipPosition.Create(shipPosition); FastShipPosition.DistanceTo(fastShipPosition, FastCoord.Create(target)).Should().Be(shipPosition.DistanceTo(target), $"shipPosition: {shipPosition}; target: {target}"); } } } } } } }
public void GetCollisionType_ComplexCases_ReturnsValidResult(int myx, int myy, int myor, int mysp, ShipMoveCommand myc, int ox, int oy, int oor, int osp, ShipMoveCommand oc, CollisionType expected) { var my = FastShipPosition.Create(myx, myy, myor, mysp); var other = FastShipPosition.Create(ox, oy, oor, osp); GetCollisionType(my, myc, other, oc).Should().Be(expected); }
public void DistanceTo_StrangeCase_ReturnsValidValue() { //coord: 23, 5, orientation: 0, speed: 0 23, 5 var shipPosition = new ShipPosition(new Coord(23, 5), 0, 0); var target = new Coord(20, 5); var fastShipPosition = FastShipPosition.Create(shipPosition); var fastTarget = FastCoord.Create(target); FastShipPosition.DistanceTo(fastShipPosition, fastTarget).Should().Be(1000); }
public Ship(int index, int id, int x, int y, int orientation, int speed, int rum, int owner) : base(id, EntityType.Ship, x, y) { this.index = index; this.orientation = orientation; this.speed = speed; this.rum = rum; this.owner = owner; bow = coord.Neighbor(orientation); stern = coord.Neighbor((orientation + 3) % 6); fposition = FastShipPosition.Create(x, y, orientation, speed); fbow = FastShipPosition.Bow(fposition); fstern = FastShipPosition.Stern(fposition); }
public void Create_OutsideMap_ReturnsValidPosition(int x, int y) { for (int speed = 0; speed <= Constants.MAX_SHIP_SPEED; speed++) { for (int orientation = 0; orientation < 6; orientation++) { var shipPosition = new ShipPosition(new Coord(x, y), orientation, speed); var fastShipPosition = FastShipPosition.Create(shipPosition); var actual = FastShipPosition.ToShipPosition(fastShipPosition); actual.coord.Should().Be(new Coord(-1000, -1000), $"it's coord of [{shipPosition}]"); actual.speed.Should().Be(shipPosition.speed, $"it's speed of [{shipPosition}]"); actual.orientation.Should().Be(shipPosition.orientation, $"it's orientation of [{shipPosition}]"); } } }
public void Stern_ReturnsValidCoord() { for (int x = 0; x < Constants.MAP_WIDTH; x++) { for (int y = 0; y < Constants.MAP_HEIGHT; y++) { for (int speed = 0; speed <= Constants.MAX_SHIP_SPEED; speed++) { for (int orientation = 0; orientation < 6; orientation++) { var shipPosition = new ShipPosition(new Coord(x, y), orientation, speed); var fastShipPosition = FastShipPosition.Create(shipPosition); FastCoord.ToCoord(FastShipPosition.Stern(fastShipPosition)).Should().Be(shipPosition.stern, shipPosition.ToString()); } } } } }
public void IsInsideMap_ReturnsValidValue() { for (int x = -2; x < Constants.MAP_WIDTH + 2; x++) { for (int y = -2; y < Constants.MAP_HEIGHT + 2; y++) { for (int speed = 0; speed <= Constants.MAX_SHIP_SPEED; speed++) { for (int orientation = 0; orientation < 6; orientation++) { var shipPosition = new ShipPosition(new Coord(x, y), orientation, speed); var fastShipPosition = FastShipPosition.Create(shipPosition); FastShipPosition.IsInsideMap(fastShipPosition).Should().Be(shipPosition.IsInsideMap(), shipPosition.ToString()); } } } } }
public void Create_InsideMap_ReturnsValidPosition() { for (int speed = 0; speed <= Constants.MAX_SHIP_SPEED; speed++) { for (int orientation = 0; orientation < 6; orientation++) { for (int x = 0; x < Constants.MAP_WIDTH; x++) { for (int y = 0; y < Constants.MAP_HEIGHT; y++) { var shipPosition = new ShipPosition(new Coord(x, y), orientation, speed); var fastShipPosition = FastShipPosition.Create(shipPosition); var actual = FastShipPosition.ToShipPosition(fastShipPosition); actual.coord.Should().Be(shipPosition.coord, $"it's coord of [{shipPosition}]"); actual.speed.Should().Be(shipPosition.speed, $"it's speed of [{shipPosition}]"); actual.orientation.Should().Be(shipPosition.orientation, $"it's orientation of [{shipPosition}]"); } } } } }
public void CollidesShip_ReturnsValidValue() { for (int xt = 0; xt < Constants.MAP_WIDTH; xt += 3) { for (int yt = 0; yt < Constants.MAP_HEIGHT; yt += 3) { for (int speedT = 0; speedT <= Constants.MAX_SHIP_SPEED; speedT++) { for (int orientationT = 0; orientationT < 6; orientationT++) { for (int x = 0; x < Constants.MAP_WIDTH; x++) { for (int y = 0; y < Constants.MAP_HEIGHT; y++) { for (int speed = 0; speed <= Constants.MAX_SHIP_SPEED; speed++) { for (int orientation = 0; orientation < 6; orientation++) { if (!(Math.Abs(x - xt) < 6 && Math.Abs(y - yt) < 6)) { continue; } var shipPosition = new ShipPosition(new Coord(x, y), orientation, speed); var targetPosition = new ShipPosition(new Coord(xt, yt), orientationT, speedT); var fastShipPosition = FastShipPosition.Create(shipPosition); var fastTargetPosition = FastShipPosition.Create(targetPosition); FastShipPosition.CollidesShip(fastShipPosition, fastTargetPosition).Should().Be(shipPosition.CollidesShip(targetPosition), $"shipPosition: {shipPosition}; targetPosition: {targetPosition}"); } } } } } } } } }
public static CollisionType Move(int myPosition, ShipMoveCommand myCommand, int otherPosition, ShipMoveCommand otherCommand, out uint myMovement, out uint otherMovement) { var result = CollisionType.None; int myCoord, myBow, myStern, myOrientation, mySpeed, nmyCoord, nmyBow, nmyStern, nmyOrientation; Split(myPosition, myCommand, out myCoord, out myBow, out myStern, out myOrientation, out mySpeed, out nmyCoord, out nmyBow, out nmyStern, out nmyOrientation); int otherCoord, otherBow, otherStern, otherOrientation, otherSpeed, notherCoord, notherBow, notherStern, notherOrientation; Split(otherPosition, otherCommand, out otherCoord, out otherBow, out otherStern, out otherOrientation, out otherSpeed, out notherCoord, out notherBow, out notherStern, out notherOrientation); // move ships for (int i = 1; i <= Constants.MAX_SHIP_SPEED; i++) { if (!GoForward(i, ref myCoord, ref myBow, ref myStern, ref myOrientation, ref mySpeed, ref nmyCoord, ref nmyBow, ref nmyStern)) { result |= CollisionType.MyWall; } if (!GoForward(i, ref otherCoord, ref otherBow, ref otherStern, ref otherOrientation, ref otherSpeed, ref notherCoord, ref notherBow, ref notherStern)) { result |= CollisionType.OtherWall; } var myCollides = mySpeed > 0 && (nmyBow == notherBow || nmyBow == notherCoord || nmyBow == notherStern); var otherCollides = otherSpeed > 0 && (notherBow == nmyBow || notherBow == nmyCoord || notherBow == nmyStern); if (myCollides) { nmyCoord = myCoord; nmyBow = myBow; nmyStern = myStern; mySpeed = 0; result |= CollisionType.MyMove; } if (otherCollides) { notherCoord = otherCoord; notherBow = otherBow; notherStern = otherStern; otherSpeed = 0; result |= CollisionType.OtherMove; } myCoord = nmyCoord; myBow = nmyBow; myStern = nmyStern; otherCoord = notherCoord; otherBow = notherBow; otherStern = notherStern; } var myMovedPosition = FastShipPosition.Create(myCoord, myOrientation, mySpeed); var otherMovedPosition = FastShipPosition.Create(otherCoord, otherOrientation, otherSpeed); // rotate ships nmyBow = FastCoord.Neighbor(myCoord, nmyOrientation); nmyStern = FastCoord.Neighbor(myCoord, (nmyOrientation + 3) % 6); notherBow = FastCoord.Neighbor(otherCoord, notherOrientation); notherStern = FastCoord.Neighbor(otherCoord, (notherOrientation + 3) % 6); var rotationCollides = nmyBow == notherBow || nmyBow == notherCoord || nmyBow == notherStern || nmyCoord == notherBow || nmyCoord == notherCoord || nmyCoord == notherStern || nmyStern == notherBow || nmyStern == notherCoord || nmyStern == notherStern; if (rotationCollides) { if (myCommand == ShipMoveCommand.Port || myCommand == ShipMoveCommand.Starboard) { result |= CollisionType.MyRotation; } if (otherCommand == ShipMoveCommand.Port || otherCommand == ShipMoveCommand.Starboard) { result |= CollisionType.OtherRotation; } } else { myBow = nmyBow; myStern = nmyStern; myOrientation = nmyOrientation; otherBow = notherBow; otherStern = notherStern; otherOrientation = notherOrientation; } var myFinalPosition = FastShipPosition.Create(myCoord, myOrientation, mySpeed); myMovement = FastShipPosition.Move(myMovedPosition, myFinalPosition); var otherFinalPosition = FastShipPosition.Create(otherCoord, otherOrientation, otherSpeed); otherMovement = FastShipPosition.Move(otherMovedPosition, otherFinalPosition); return(result); }
private static void Main22(string[] args) { FastCoord.Init(); var coordsX = new List <Coord>(); for (int x = -1; x < Constants.MAP_WIDTH + 1; x++) { for (int y = -1; y < Constants.MAP_HEIGHT + 1; y++) { var coord = new Coord(x, y); coordsX.Add(coord); } } var indexes = Enumerable.Range(0, coordsX.Count).ToArray(); var seed = new Random().Next(); Console.Out.WriteLine($"Seed: {seed}"); var random = new Random(seed); for (int i = 0; i < indexes.Length; i++) { var r = random.Next(i, indexes.Length); var tmp = indexes[r]; indexes[r] = indexes[i]; indexes[i] = tmp; } var coords = indexes.Select(i => coordsX[i]).ToArray(); var fastCoords = indexes.Select(i => FastCoord.Create(coords[i])).ToArray(); var ships = coords.Select(c => new ShipPosition(c, random.Next(6), random.Next(3))).ToArray(); var fastShips = ships.Select(FastShipPosition.Create).ToArray(); var stopwatch = Stopwatch.StartNew(); Console.Out.WriteLine("IsInsideMap"); stopwatch.Restart(); int ind = 0; for (int i = 0; i < 10000000; i++) { coords[ind++].IsInsideMap(); if (ind >= indexes.Length) { ind = 0; } } stopwatch.Stop(); Console.Out.WriteLine(stopwatch.ElapsedMilliseconds); stopwatch.Restart(); ind = 0; for (int i = 0; i < 10000000; i++) { FastCoord.IsInsideMap(fastCoords[ind++]); if (ind >= indexes.Length) { ind = 0; } } stopwatch.Stop(); Console.Out.WriteLine(stopwatch.ElapsedMilliseconds); Console.Out.WriteLine("DistanceTo"); stopwatch.Restart(); ind = 0; for (int i = 0; i < 10000000; i++) { coords[ind++].DistanceTo(coords[0]); if (ind >= indexes.Length) { ind = 0; } } stopwatch.Stop(); Console.Out.WriteLine(stopwatch.ElapsedMilliseconds); stopwatch.Restart(); ind = 0; for (int i = 0; i < 10000000; i++) { FastCoord.Distance(fastCoords[ind++], fastCoords[0]); if (ind >= indexes.Length) { ind = 0; } } stopwatch.Stop(); Console.Out.WriteLine(stopwatch.ElapsedMilliseconds); Console.Out.WriteLine("Neighbor"); stopwatch.Restart(); ind = 0; for (int i = 0; i < 10000000; i++) { coords[ind].Neighbor(0); coords[ind].Neighbor(1); coords[ind].Neighbor(2); coords[ind].Neighbor(3); coords[ind].Neighbor(4); coords[ind].Neighbor(5); if (++ind >= indexes.Length) { ind = 0; } } stopwatch.Stop(); Console.Out.WriteLine(stopwatch.ElapsedMilliseconds); stopwatch.Restart(); ind = 0; for (int i = 0; i < 10000000; i++) { FastCoord.Neighbor(fastCoords[ind], 0); FastCoord.Neighbor(fastCoords[ind], 1); FastCoord.Neighbor(fastCoords[ind], 2); FastCoord.Neighbor(fastCoords[ind], 3); FastCoord.Neighbor(fastCoords[ind], 4); FastCoord.Neighbor(fastCoords[ind], 5); if (++ind >= indexes.Length) { ind = 0; } } stopwatch.Stop(); Console.Out.WriteLine(stopwatch.ElapsedMilliseconds); Console.Out.WriteLine("ShipDistanceTo"); var shipPosition = new ShipPosition(coords[0], 0, 0); stopwatch.Restart(); ind = 0; for (int i = 0; i < 10000000; i++) { shipPosition.DistanceTo(coords[ind]); if (++ind >= indexes.Length) { ind = 0; } } stopwatch.Stop(); Console.Out.WriteLine(stopwatch.ElapsedMilliseconds); var fastShipPosition = FastShipPosition.Create(shipPosition); stopwatch.Restart(); ind = 0; for (int i = 0; i < 10000000; i++) { FastShipPosition.DistanceTo(fastShipPosition, fastCoords[ind++]); if (ind >= indexes.Length) { ind = 0; } } stopwatch.Stop(); Console.Out.WriteLine(stopwatch.ElapsedMilliseconds); Console.Out.WriteLine("Collides"); stopwatch.Restart(); ind = 0; for (int i = 0; i < 10000000; i++) { shipPosition.Collides(coords[ind++]); if (ind >= indexes.Length) { ind = 0; } } stopwatch.Stop(); Console.Out.WriteLine(stopwatch.ElapsedMilliseconds); stopwatch.Restart(); ind = 0; for (int i = 0; i < 10000000; i++) { FastShipPosition.Collides(fastShipPosition, fastCoords[ind++]); if (ind >= indexes.Length) { ind = 0; } } stopwatch.Stop(); Console.Out.WriteLine(stopwatch.ElapsedMilliseconds); Console.Out.WriteLine("CollidesShip"); stopwatch.Restart(); ind = 0; for (int i = 0; i < 10000000; i++) { shipPosition.CollidesShip(ships[ind++]); if (ind >= indexes.Length) { ind = 0; } } stopwatch.Stop(); Console.Out.WriteLine(stopwatch.ElapsedMilliseconds); stopwatch.Restart(); ind = 0; for (int i = 0; i < 10000000; i++) { FastShipPosition.CollidesShip(fastShipPosition, fastShips[ind++]); if (ind >= indexes.Length) { ind = 0; } } stopwatch.Stop(); Console.Out.WriteLine(stopwatch.ElapsedMilliseconds); Console.Out.WriteLine("Move"); stopwatch.Restart(); ind = 0; for (int i = 0; i < 1_000_000; i++) { foreach (var moveCommand in ShipMoveCommands.all) { ships[ind].Apply(moveCommand); } if (++ind >= indexes.Length) { ind = 0; } } stopwatch.Stop(); Console.Out.WriteLine(stopwatch.ElapsedMilliseconds); stopwatch.Restart(); ind = 0; for (int i = 0; i < 1_000_000; i++) { foreach (var moveCommand in ShipMoveCommands.all) { var moved = FastShipPosition.Move(fastShips[ind], moveCommand); FastShipPosition.GetMovedPosition(moved); FastShipPosition.GetFinalPosition(moved); } if (++ind >= indexes.Length) { ind = 0; } } stopwatch.Stop(); Console.Out.WriteLine(stopwatch.ElapsedMilliseconds); stopwatch.Restart(); ind = 0; for (int i = 0; i < 1_000_000; i++) { foreach (var moveCommand in ShipMoveCommands.all) { uint myMovement; uint otherMovement; CollisionChecker.Move(fastShips[ind], moveCommand, fastShips[(ind + 1) % indexes.Length], moveCommand, out myMovement, out otherMovement); FastShipPosition.GetMovedPosition(myMovement); FastShipPosition.GetFinalPosition(myMovement); } if (++ind >= indexes.Length) { ind = 0; } } stopwatch.Stop(); Console.Out.WriteLine(stopwatch.ElapsedMilliseconds); }