public Map(Map map)
        {
            Width = map.Width;
            Height = map.Height;
            UpdateManager = new UpdateManager();
            Init(false);

            // Incomplete: doesn't attempt to copy the map contents, should only used in CopyAndFlip...
        }
 private Match(List<BuildingInfo> buildingsAvailable, Map map, List<Player> players, int roundNumber,
     int roundLimit)
 {
     BuildingsAvailable = buildingsAvailable;
     Map = map;
     Players = players;
     RoundNumber = roundNumber;
     RoundLimit = roundLimit;
 }
        public void TestGetEntityOutOfBoundsReturnsNullTopLeft()
        {
            // Given
            var map = new Map(5, 5);

            // When
            var result = map.GetEntity(-1, -1);

            // Then
            Assert.IsNull(result, "Result should be null. No exception should be thrown.");
        }
        public void TestGetEntityOutOfBoundsReturnsNullBottomRight()
        {
            // Given
            var map = new Map(5, 5);

            // When
            var result = map.GetEntity(map.Width, map.Height);

            // Then
            Assert.IsNull(result, "Result should be null. No exception should be thrown.");
        }
        public void TestAddEntity()
        {
            // Given
            var game = Match.GetInstance();
            game.StartNewGame();
            var map = new Map(11, 11);
            game.Map = map;
            var ship = new Ship(1) {X = 1, Y = 2};

            // When
            map.AddEntity(ship);

            // Then
            Assert.IsNotNull(map.GetEntity(ship.X, ship.Y), "Entity is null when it should be a space ship");
            Assert.IsNotNull(map.GetEntity(ship.X + 1, ship.Y), "Entity is null when it should be a space ship");
            Assert.IsNotNull(map.GetEntity(ship.X + 2, ship.Y), "Entity is null when it should be a space ship");

            Assert.IsNull(map.GetEntity(ship.X + 3, ship.Y), "Entity is not null when it should be");
        }
        public void TestMoveOffMapDoesNotDeleteEntity()
        {
            // Given
            var map = new Map(5, 5);
            var wall = map.GetEntity(1, 0);

            // When
            MoveNotOnMapException exception = null;
            try
            {
                map.MoveEntity(wall, 1, -1);
            }
            catch (MoveNotOnMapException ex)
            {
                exception = ex;
            }

            wall = map.GetEntity(1, 0);

            // Then
            Assert.IsNotNull(exception, "MoveNotOnMapException was not thrown.");
            Assert.IsNotNull(wall, "Wall was deleted from the map by the attempt to move off it.");
        }
        public void TestRemoveEntity()
        {
            // Given
            var game = Match.GetInstance();
            game.StartNewGame();
            var map = new Map(11, 11);
            game.Map = map;
            var ship = new Ship(1) {X = 1, Y = 2};
            map.AddEntity(ship);

            // When
            map.RemoveEntity(ship);

            // Then
            Assert.IsNull(map.GetEntity(ship.X, ship.Y), "Entity should be null");
            Assert.IsNull(map.GetEntity(ship.X + 1, ship.Y), "Entity should be null");
            Assert.IsNull(map.GetEntity(ship.X + 2, ship.Y), "Entity should be null");
        }
        public Map CopyAndFlip(Map map, CoordinateFlipper flipper, Dictionary<int, Entity> flippedEntities)
        {
            var copy = new Map(map);

            // Copy all entities including walls
            for (var y = 0; y < map.Height; y++)
            {
                for (var x = 0; x < map.Width; x++)
                {
                    var entity = map.GetEntity(x, y);

                    if (entity == null) continue;

                    Entity flippedEntity = null;
                    if (entity.GetType() == typeof (Alien))
                    {
                        flippedEntity = Alien.CopyAndFlip((Alien) entity, flipper, flippedEntities);
                    }
                    else if (entity.GetType() == typeof (Missile))
                    {
                        flippedEntity = Missile.CopyAndFlip((Missile) entity, flipper, flippedEntities);
                    }
                    else if (entity.GetType() == typeof (Bullet))
                    {
                        flippedEntity = Bullet.CopyAndFlip((Bullet) entity, flipper, flippedEntities);
                    }
                    else if (entity.GetType() == typeof (Shield))
                    {
                        flippedEntity = Shield.CopyAndFlip((Shield) entity, flipper, flippedEntities);
                    }
                    else if (entity.GetType() == typeof (Ship))
                    {
                        flippedEntity = Ship.CopyAndFlip((Ship) entity, flipper, flippedEntities);
                    }
                    else if (entity.GetType() == typeof (AlienFactory))
                    {
                        flippedEntity = AlienFactory.CopyAndFlip((AlienFactory) entity, flipper, flippedEntities);
                    }
                    else if (entity.GetType() == typeof (MissileController))
                    {
                        flippedEntity = MissileController.CopyAndFlip((MissileController) entity, flipper,
                            flippedEntities);
                    }
                    else if (entity.GetType() == typeof (Wall))
                    {
                        flippedEntity = Wall.CopyAndFlip((Wall) entity, flipper, flippedEntities);
                    }

                    if ((flippedEntity != null) && (copy.GetEntity(flippedEntity.X, flippedEntity.Y) == null))
                    {
                        copy.AddEntity(flippedEntity);
                    }
                }
            }

            return copy;
        }
        public void StartNewGame(bool aliensDisabled = false)
        {
            Players.Clear();
            Players.Add(new Player(1));
            Players.Add(new Player(2));

            if (aliensDisabled)
            {
                Players[0].AlienManager.Disabled = true;
                Players[1].AlienManager.Disabled = true;
            }

            Map = new Map(Settings.Default.MapWidth, Settings.Default.MapHeight);

            UpdateRespawnPlayersIfNecessary();
            Map.UpdateEntities();

            ShieldFactory.BuildInitial(1);
            ShieldFactory.BuildInitial(2);
        }
 protected void RenderMap(Map map, StringBuilder output)
 {
     for (var y = 0; y < map.Height; y++)
     {
         for (var x = 0; x < map.Width; x++)
         {
             output.Append(GetEntitySymbol(map.GetEntity(x, y)).ToString());
         }
         output.Append(Environment.NewLine);
     }
 }