Exemple #1
0
        static void Main(string[] args)
        {
            Console.WriteLine($"Runs {RUNS} with {MAX_DEPTH} color layers");
            int colorObjectCount = (int)Math.Pow(2, MAX_DEPTH);

            // This is the code that a thread haz to run:
            // The arrays are being created here to reduce the number of useless object allocations
            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();
            byte[]        resultingColors = new byte[colorObjectCount];
            ColorObject[] colorObjects    = new ColorObject[colorObjectCount];

            GreyscaleColor[] colorLayers = new GreyscaleColor[MAX_DEPTH + 1];
            colorLayers[0] = new GreyscaleColor {
                Greyscale = 0, Alpha = 255
            };
            colorLayers[1] = new GreyscaleColor {
                Greyscale = 255, Alpha = 255
            };

            double       maxScore  = 0;
            ColorsResult maxResult = null;
            Random       rng       = new Random(); // This could be a source of derp, since the C# RNG != the Java RNG

            for (int i = 0; i < RUNS; i++)
            {
                ColorsResult result = DoRun(maxScore, resultingColors, colorObjects, colorLayers, rng);
                if (result != null)
                {
                    Console.WriteLine($"Best score {result.Score}");
                    maxScore  = result.Score;
                    maxResult = result;
                }
            }
            stopWatch.Stop();
            Console.WriteLine($"Completed in {stopWatch.ElapsedMilliseconds}ms");

            Console.ReadLine();

            // And then output it or return it from the thread
            var jsonSerializerOptions = new JsonSerializerOptions
            {
                WriteIndented = true
            };

            jsonSerializerOptions.Converters.Add(new GreyscaleColorConverter());
            Console.WriteLine(JsonSerializer.Serialize(new ColorsResultOutput(maxResult), jsonSerializerOptions));
            Console.ReadLine();
        }
Exemple #2
0
 public ColorsResultOutput(ColorsResult colorsResult)
 {
     Score = colorsResult.Score;
     ResultingColorObjects = new FullColorObject[colorsResult.ResultingColorObjects.Length];
     for (int i = 0; i < ResultingColorObjects.Length; i++)
     {
         ResultingColorObjects[i] = new FullColorObject()
         {
             ResultingColor = colorsResult.ResultingColors[i],
             BitPattern     = colorsResult.ResultingColorObjects[i].BitPattern,
             Depth          = colorsResult.ResultingColorObjects[i].Depth,
         };
     }
     ColorLayers = colorsResult.ColorLayers;
 }