Example #1
0
        public AsteroidMap VisibilityMap(int stationRow, int stationCol)
        {
            // Create a map of what other asteroids are visible if i put a station on the inputted asteroid.
            Asteroid    station = new Asteroid(stationRow, stationCol, null);
            AsteroidMap map     = new AsteroidMap((string[])this.rows.Clone(), station);

            station.map = map;

            // If there is no asteroid in the inputted area, return null;
            if (map.GetCell(stationRow, stationCol) != ASTEROID)
            {
                Console.WriteLine("ERROR! You asked for a visibility map for a location that has no asteroid.");
                return(null);
            }

            // Logic:
            // Look directly to the left of me, find the first asteroid, and consider everything else blocked.
            // Look directly to the right of me, find the first asteroid, and consider everything else blocked.
            // Look at the entire row above me.  Find asteroids, and block everything behind it.
            // And then move up a row.
            // Look at the entire row below me.  Find any asteroids, and block everything behind it.


            for (int columnId = (stationCol - 1); columnId >= 0; columnId--)
            {
                int rowId = stationRow;
                if (map.GetCell(rowId, columnId) == ASTEROID)
                {
                    map.SetCellVisible(rowId, columnId, stationRow, stationCol);
                    // break;
                }
            }

            for (int columnId = (stationCol + 1); columnId < this.mapWidth; columnId++)
            {
                int rowId = stationRow;
                if (map.GetCell(rowId, columnId) == ASTEROID)
                {
                    map.SetCellVisible(rowId, columnId, stationRow, stationCol);
                    //break;
                }
            }

            for (int rowId = (stationRow - 1); rowId >= 0; rowId--)
            {
                for (int colId = 0; colId < this.mapWidth; colId++)
                {
                    if (map.GetCell(rowId, colId) == ASTEROID)
                    {
                        map.SetCellVisible(rowId, colId, stationRow, stationCol);
                        //break;
                    }
                }
            }

            for (int rowId = (stationRow + 1); rowId < this.mapHeight; rowId++)
            {
                for (int colId = 0; colId < this.mapWidth; colId++)
                {
                    if (map.GetCell(rowId, colId) == ASTEROID)
                    {
                        map.SetCellVisible(rowId, colId, stationRow, stationCol);
                        //break;
                    }
                }
            }

            return(map);
        }