Exemple #1
0
        public void StartVaporizing(string outFile, int numToVaporize)
        {
            CellContentsStruct asteroidBase = asteroidField[visibleIndex];
            StreamWriter       sw           = new StreamWriter(outFile);
            Vector2            bestSpot     = Helpers.GetCoordinatesFromIndex(visibleIndex, xSize);
            string             outLine      = "";

            outLine = "VISIBLE INDEX at " + bestSpot.x + "," + bestSpot.y + " can see " + visibleCount + " asteroids";
            sw.WriteLine(outLine);
            outLine = "";
            List <double> destroyedAsteroids = new List <double>();
            int           numVaporized       = 0;
            bool          doneDestroying     = false;

            while (!doneDestroying)
            {
                destroyedAsteroids = new List <double>();
                foreach (KeyValuePair <double, List <angleCheckStruct> > kvp in asteroidBase.angleDistance.OrderBy(i => i.Key))
                {
                    numVaporized++;
                    int removedIndex = GetLowestDistanceIndex(kvp.Value);
                    outLine = "#" + numVaporized + " from " + bestSpot.x + "," + bestSpot.y + " Angle is " + kvp.Value[removedIndex].angle;
                    outLine = outLine + "Location is " + kvp.Value[removedIndex].asteroidLocation.x + "," + kvp.Value[removedIndex].asteroidLocation.y + " remaing on line " + (kvp.Value.Count - 1);
                    sw.WriteLine(outLine);
                    outLine = "";
                    int  removeIndex = Helpers.GetIndexFromCoordinate(kvp.Value[removedIndex].asteroidLocation, xSize);
                    int  curNum      = numVaporized % 10;
                    char repChar     = curNum.ToString()[0];
                    //asteroidField[removeIndex].curChar = repChar;
                    asteroidField[removeIndex].curChar = '.';
                    destroyedAsteroids.Add(kvp.Key);
                    if (numVaporized >= numToVaporize)
                    {
                        doneDestroying = true;
                        break;
                    }
                }
                WriteAsteroidField(sw);
                foreach (double angle in destroyedAsteroids)
                {
                    // cleanup dictionary;
                    if (asteroidBase.angleDistance[angle].Count > 1)
                    {
                        int indexToRemove = GetLowestDistanceIndex(asteroidBase.angleDistance[angle]);
                        asteroidBase.angleDistance[angle].RemoveAt(indexToRemove);
                    }
                    else
                    {
                        asteroidBase.angleDistance.Remove(angle);
                    }
                }
            }
            sw.Close();
        }
Exemple #2
0
        public AsteroidField(string[] sourceData)
        {
            ySize         = sourceData.Length;
            xSize         = sourceData[0].Length;
            asteroidField = new CellContentsStruct[xSize * ySize];

            for (int intJ = 0; intJ < sourceData.Length; intJ++)
            {
                for (int intI = 0; intI < sourceData[intJ].Length; intI++)
                {
                    CellContentsStruct curStruct = new CellContentsStruct();
                    curStruct.curChar = sourceData[intJ][intI];
                    asteroidField[(intJ * xSize) + intI] = curStruct;
                }
            }



            for (int intI = 0; intI < asteroidField.Length; intI++)
            {
                if (asteroidField[intI].curChar == '.')
                {
                    continue;
                }
                Vector2 startVec = new Vector2();
                startVec = Helpers.GetCoordinatesFromIndex(intI, xSize);
                asteroidField[intI].curAngles     = new Dictionary <Vector2, angleCheckStruct>();
                asteroidField[intI].angleDistance = new Dictionary <double, List <angleCheckStruct> >();
                for (int intJ = 0; intJ < asteroidField.Length; intJ++)
                {
                    if (intI != intJ) // skip if it's us
                    {
                        if (asteroidField[intJ].curChar != '.')
                        {
                            Vector2          endVec       = Helpers.GetCoordinatesFromIndex(intJ, xSize);
                            angleCheckStruct curAngleData = GetAngleCheckStruct(startVec, endVec);
                            asteroidField[intI].curAngles[endVec] = curAngleData;
                            if (asteroidField[intI].angleDistance.ContainsKey(curAngleData.angle))
                            {
                                asteroidField[intI].angleDistance[curAngleData.angle].Add(curAngleData);
                            }
                            else
                            {
                                List <angleCheckStruct> newAngleList = new List <angleCheckStruct>();
                                newAngleList.Add(curAngleData);
                                asteroidField[intI].angleDistance.Add(curAngleData.angle, newAngleList);
                            }
                        }
                    }
                }

                /*int visibleAsteroids = asteroidField[intI].angleDistance.Count;
                 * if(visibleAsteroids> visibleCount)
                 * {
                 *  visibleCount= visibleAsteroids;
                 *  visibleIndex= intI;
                 * }*/
            }
            for (int intI = 0; intI < asteroidField.Length; intI++)
            {
                if (asteroidField[intI].curChar == '#')
                {
                    if (asteroidField[intI].angleDistance.Count > visibleCount)
                    {
                        visibleCount = asteroidField[intI].angleDistance.Count;
                        visibleIndex = intI;
                    }
                }
            }
        }