Пример #1
0
        public void ScanCountries()
        {
            //if there is no place on the map - there is nothing to be scanned
            if (_worldMap.RowCount == 0 || _worldMap.ColumnCount == 0)
            {
                return;
            }

            //let's create a country based on the very first territory
            var currentTerritory        = _worldMap.GetTerritory(0, 0);
            var currentTerritoryCountry = new Country(currentTerritory);

            _worldMap.AddCountry(currentTerritoryCountry);

            for (int i = 0; i < _worldMap.RowCount; i++)
            {
                for (int j = 0; j < _worldMap.ColumnCount; j++)
                {
                    //skipping the first territory which was already added to a new country
                    if (i == 0 && j == 0)
                    {
                        continue;
                    }

                    var westTerritory  = _worldMap.GetTerritory(i, j - 1);
                    var northTerritory = _worldMap.GetTerritory(i - 1, j);
                    currentTerritory        = _worldMap.GetTerritory(i, j);
                    currentTerritoryCountry = null;

                    //column >= 1
                    if (westTerritory != null)
                    {
                        //checking left cell
                        //assigning territory to the left side country if they are of the same nations
                        var westTerritoryCountry = _worldMap.GetCountryByTerritory(westTerritory);
                        if (currentTerritory.NationId == westTerritory.NationId)
                        {
                            westTerritoryCountry.Territories.Add(currentTerritory);
                            currentTerritoryCountry = westTerritoryCountry;
                        }

                        //checking top cell
                        if (northTerritory != null)
                        {
                            var northTerritoryCountry = _worldMap.GetCountryByTerritory(northTerritory);
                            if (currentTerritoryCountry != null && northTerritory.NationId == currentTerritory.NationId)
                            {
                                //merge countries
                                //country & northTerritoryCountry
                                _worldMap.MergeCountries(currentTerritoryCountry, ref northTerritoryCountry);
                            }
                            else if (currentTerritoryCountry == null && northTerritory.NationId == currentTerritory.NationId)
                            {
                                northTerritoryCountry.Territories.Add(currentTerritory);
                                currentTerritoryCountry = northTerritoryCountry;
                            }
                            else if (currentTerritoryCountry == null)
                            {
                                currentTerritoryCountry = new Country(currentTerritory);
                                _worldMap.AddCountry(currentTerritoryCountry);
                            }
                        }

                        if (currentTerritoryCountry == null) //territory is still unassigned
                        {
                            currentTerritoryCountry = new Country(currentTerritory);
                            _worldMap.AddCountry(currentTerritoryCountry);
                        }
                    }
                    else //column index == 0 (westTerritory == null)
                    {
                        if (currentTerritory.NationId == northTerritory.NationId)
                        {
                            var northTerritoryCountry = _worldMap.GetCountryByTerritory(northTerritory);
                            northTerritoryCountry.AddTerritory(currentTerritory);
                        }
                        else
                        {
                            currentTerritoryCountry = new Country(currentTerritory);
                            _worldMap.AddCountry(currentTerritoryCountry);
                        }
                    }
                }
            }
        }