예제 #1
0
        void CreateAndRunReplication(uint replicationId)
        {
            // setup simulation run
            IReplication r = _replicationFactory.Create(
                new DataGatherer(replicationId),
                new EventQueueFactory(
                    _randomFactory.Create(),
                    _stationCount,
                    _highwayLength,
                    _channels,
                    _reserved)
                );

            // run simulation and measure how long it takes
            long before = DateTime.Now.Ticks;

            r.Run();
            long after = DateTime.Now.Ticks;

            // Collect and print required data
            _totalCollectedData += r.ReplicationData;
            _report(r.ReplicationData);
            Debug.WriteLine(
                string.Format("took {0}", new TimeSpan(after - before)));
        }
        public override InnerMap GoGenerate <M>(IInnerMapFactory <M> mapFactory, IRandomFactory randomFactory, Action <int, int, long, long> pixelChangedCallback)
        {
            var innerMap = mapFactory.Create();
            var random   = randomFactory.Create();

            return(GoGenerateInternal(innerMap, random, pixelChangedCallback));
        }
예제 #3
0
        public async Task <GameId> CreateGameAsync(
            PlayerList players,
            BoardType boardType,
            int numSequencesToWin,
            CancellationToken cancellationToken)
        {
            var random         = _randomFactory.Create();
            var seed           = new Seed(random.Next());
            var firstPlayerIdx = players.RandomFirstPlayer ? random.Next(players.Players.Count) : 0;
            var newGame        = new NewGame(players, firstPlayerIdx, seed, boardType, numSequencesToWin);

            return(await _gameStore.PersistNewGameAsync(newGame, cancellationToken));
        }
예제 #4
0
        public string HashPassword(string password)
        {
            // Implement the Tor password hashing algorithm. Source:
            // https://gitweb.torproject.org/tor.git/tree/src/or/main.c?id=1f679d4ae11cd976f5539bc4ddf36873132aeb00#n3217
            // https://gitweb.torproject.org/tor.git/tree/src/common/crypto_s2k.c?id=1f679d4ae11cd976f5539bc4ddf36873132aeb00#n172
            byte[] salt = new byte[8];
            using (var random = _randomFactory.Create())
            {
                random.GetBytes(salt);
            }

            var c            = 96;
            var s2KSpecifier = salt.Concat(new[] { (byte)c }).ToArray();

            var EXPBIAS = 6;
            var count   = (16 + (c & 15)) << ((c >> 4) + EXPBIAS);

            byte[] hash;
            using (var d = SHA1.Create())
            {
                var tmp = s2KSpecifier
                          .Take(8)
                          .Concat(Encoding.ASCII.GetBytes(password))
                          .ToArray();

                var secretLen = tmp.Length;
                while (count != 0)
                {
                    if (count > secretLen)
                    {
                        d.TransformBlock(tmp, 0, tmp.Length, null, -1);
                        count -= secretLen;
                    }
                    else
                    {
                        d.TransformBlock(tmp, 0, count, null, -1);
                        count = 0;
                    }
                }

                d.TransformFinalBlock(new byte[0], 0, 0);
                hash = d.Hash;
            }

            var s2KSpecifierHex = BytesToHex(s2KSpecifier);
            var hashHex         = BytesToHex(hash);

            return($"16:{s2KSpecifierHex}{hashHex}");
        }
예제 #5
0
        static Default()
        {
            var systemRandomFactory = new SystemRandomFactory();

            SystemRandomFactory         = systemRandomFactory;
            SystemRandomWithSeedFactory = systemRandomFactory;

            var xorShift1024RandomFactory = new XorShift1024RandomFactory();

            XorShift1024RandomFactory         = xorShift1024RandomFactory;
            XorShift1024RandomWithSeedFactory = xorShift1024RandomFactory;
            RandomWithSeedFactory             = xorShift1024RandomFactory;
            RandomFactory = xorShift1024RandomFactory;

            IdGenerator = new IdGenerator(XorShift1024RandomFactory.Create());

            SnappyCompressor    = new SnappyCompressor();
            SnappyDecompressor  = new SnappyDecompressor();
            UtcDateTimeProvider = new UtcDateTimeProvider();
        }
        public InnerMap GenerateMapPart(int xStart, int yStart, int width, int height, int widthPart, int heightPart, IRandomFactory randomFactory)
        {
            var random = randomFactory.Create();

            InnerMap map = new BitArreintjeFastInnerMap(widthPart, heightPart)
            {
                StartX = xStart, StartY = yStart
            };

            //If the maze is out of screen
            var theRightEdge  = Math.Max(((xStart + widthPart) - width), 0);
            var theBottomEdge = Math.Max(((yStart + heightPart) - height), 0);

            map.FillMap(true);

            //Add walls
            if (xStart == 0)
            {
                for (int y = 0; y < heightPart - theBottomEdge; y++)
                {
                    map[0, y] = false;
                }
            }

            if (yStart == 0)
            {
                for (int x = 0; x < widthPart - theRightEdge; x++)
                {
                    map[x, 0] = false;
                }
            }

            if (xStart + widthPart >= width)
            {
                for (int y = 0; y < heightPart - theBottomEdge; y++)
                {
                    map[widthPart - 1 - theRightEdge, y] = false;
                }

                if (UnevenHelper.NumberIsEven(width))
                {
                    for (int y = 0; y < heightPart - theBottomEdge; y++)
                    {
                        map[widthPart - 2 - theRightEdge, y] = false;
                    }
                }
            }

            if (yStart + heightPart >= height)
            {
                for (int x = 0; x < widthPart - theRightEdge; x++)
                {
                    map[x, heightPart - 1 - theBottomEdge] = false;
                }

                if (UnevenHelper.NumberIsEven(height))
                {
                    for (int x = 0; x < widthPart - theRightEdge; x++)
                    {
                        map[x, heightPart - 2 - theBottomEdge] = false;
                    }
                }
            }


            var visibleRectangle = new Rectangle(xStart, yStart, widthPart, heightPart, 0);

            var rectangles = new Stack <Rectangle>();


            var startRect = new Rectangle(0, 0, UnevenHelper.MakeUneven(width), UnevenHelper.MakeUneven(height), random.Next());

            rectangles.Push(startRect);

            while (rectangles.Count > 0)
            {
                var curRect = rectangles.Pop();

                //Console.WriteLine($"X: {curRect.X} Y: {curRect.Y} Width: {curRect.Width} Height: {curRect.Height}");

                random.Reinitialise(curRect.Seed);

                bool horizontalSplit = true;

                if (curRect.Width > curRect.Height)
                {
                    horizontalSplit = false;
                }
                else if (curRect.Width < curRect.Height)
                {
                    horizontalSplit = true;
                }
                else
                {
                    if (random.Next(2) == 0)
                    {
                        horizontalSplit = false;
                    }
                }

                if (horizontalSplit)
                {
                    int splitnumber = 2 + random.Next((curRect.Height - 2) / 2) * 2;
                    int opening     = 1 + random.Next((curRect.Width) / 2) * 2 + curRect.X;

                    var rect1 = new Rectangle(curRect.X, curRect.Y, curRect.Width, splitnumber + 1, random.Next());
                    var rect2 = new Rectangle(curRect.X, curRect.Y + splitnumber, curRect.Width, curRect.Height - splitnumber, random.Next());

                    int xStartDraw = Math.Max(0, curRect.X - xStart);
                    int xEndDraw   = Math.Min(widthPart, curRect.X - xStart + curRect.Width);

                    int yPos = curRect.Y + splitnumber - yStart;

                    if (yPos >= 0 && yPos < heightPart - 1)
                    {
                        for (int i = xStartDraw; i < xEndDraw; i++)
                        {
                            if (i != opening - xStart)
                            {
                                map[i, yPos] = false;
                            }
                        }
                    }

                    if (IsValidRect(visibleRectangle, rect1))
                    {
                        rectangles.Push(rect1);
                    }
                    if (IsValidRect(visibleRectangle, rect2))
                    {
                        rectangles.Push(rect2);
                    }
                }
                else
                {
                    int splitnumber = 2 + random.Next((curRect.Width - 2) / 2) * 2;
                    int opening     = 1 + random.Next((curRect.Height) / 2) * 2 + curRect.Y;

                    var rect1 = new Rectangle(curRect.X, curRect.Y, splitnumber + 1, curRect.Height, random.Next());
                    var rect2 = new Rectangle(curRect.X + splitnumber, curRect.Y, curRect.Width - splitnumber, curRect.Height, random.Next());

                    var yStartDraw = Math.Max(0, curRect.Y - yStart);
                    int yEndDraw   = Math.Min(heightPart, curRect.Y - yStart + curRect.Height);

                    int xPos = curRect.X + splitnumber - xStart;

                    if (xPos >= 0 && xPos < widthPart - 1)
                    {
                        for (int i = yStartDraw; i < yEndDraw; i++)
                        {
                            if (i != opening - yStart)
                            {
                                map[xPos, i] = false;
                            }
                        }
                    }

                    if (IsValidRect(visibleRectangle, rect1))
                    {
                        rectangles.Push(rect1);
                    }
                    if (IsValidRect(visibleRectangle, rect2))
                    {
                        rectangles.Push(rect2);
                    }
                }
            }

            return(map);
        }
        public InnerMap GenerateMapPartWithPath(int xStart, int yStart, int width, int height, int widthPart, int heightPart, IRandomFactory randomFactory)
        {
            var visibleRectangle = new Rectangle(xStart, yStart, widthPart, heightPart, 0);
            //Console.WriteLine($"Generating rectangle: {visibleRectangle}");

            var random = randomFactory.Create();

            InnerMap map = new BitArreintjeFastInnerMap(widthPart, heightPart)
            {
                StartX = xStart, StartY = yStart
            };

            //If the maze is out of screen
            var theRightEdge  = Math.Max(((xStart + widthPart) - width), 0);
            var theBottomEdge = Math.Max(((yStart + heightPart) - height), 0);

            map.FillMap(true);

            var pathMap = new BitArreintjeFastInnerMap(widthPart, heightPart)
            {
                StartX = xStart, StartY = yStart
            };

            //Add walls
            if (xStart == 0)
            {
                for (int y = 0; y < heightPart - theBottomEdge; y++)
                {
                    map[0, y] = false;
                }
            }

            if (yStart == 0)
            {
                for (int x = 0; x < widthPart - theRightEdge; x++)
                {
                    map[x, 0] = false;
                }
            }

            if (xStart + widthPart >= width)
            {
                for (int y = 0; y < heightPart - theBottomEdge; y++)
                {
                    map[widthPart - 1 - theRightEdge, y] = false;
                }

                if (UnevenHelper.NumberIsEven(width))
                {
                    for (int y = 0; y < heightPart - theBottomEdge; y++)
                    {
                        map[widthPart - 2 - theRightEdge, y] = false;
                    }
                }
            }

            if (yStart + heightPart >= height)
            {
                for (int x = 0; x < widthPart - theRightEdge; x++)
                {
                    map[x, heightPart - 1 - theBottomEdge] = false;
                }

                if (UnevenHelper.NumberIsEven(height))
                {
                    for (int x = 0; x < widthPart - theRightEdge; x++)
                    {
                        map[x, heightPart - 2 - theBottomEdge] = false;
                    }
                }
            }

            var rectangles = new Stack <RectangleWithPath>();


            var startRect = new RectangleWithPath(0, 0, UnevenHelper.MakeUneven(width), UnevenHelper.MakeUneven(height), random.Next(), new MazePointClassLinkedList(1, 1), new MazePointClassLinkedList(width - 3, height - 3), true);

            rectangles.Push(startRect);

            while (rectangles.Count > 0)
            {
                var curRect = rectangles.Pop();

                //Console.WriteLine($"X: {curRect.X} Y: {curRect.Y} Width: {curRect.Width} Height: {curRect.Height}");

                random.Reinitialise(curRect.Seed);

                bool horizontalSplit = true;

                if (curRect.Width > curRect.Height)
                {
                    horizontalSplit = false;
                }
                else if (curRect.Width < curRect.Height)
                {
                    horizontalSplit = true;
                }
                else
                {
                    if (random.Next(2) == 0)
                    {
                        horizontalSplit = false;
                    }
                }

                if (horizontalSplit)
                {
                    int splitnumber = 2 + random.Next((curRect.Height - 2) / 2) * 2;
                    int opening     = 1 + random.Next((curRect.Width) / 2) * 2 + curRect.X;

                    var splitPos = new MazePointClassLinkedList(opening, curRect.Y + splitnumber);

                    var rect1 = new RectangleWithPath(curRect.X, curRect.Y, curRect.Width, splitnumber + 1, random.Next());
                    var rect2 = new RectangleWithPath(curRect.X, curRect.Y + splitnumber, curRect.Width, curRect.Height - splitnumber, random.Next());

                    if (curRect.PathPassesThroughThis)
                    {
                        var pathPassesThroughOpening = AreNumberOnTheSidesOfThisValue(splitPos.Y, curRect.MazePointLeft.Y, curRect.MazePointRight.Y);
                        DetermineRectanglePathPassingThrough(curRect, rect1, splitPos);
                        DetermineRectanglePathPassingThrough(curRect, rect2, splitPos);

                        if (pathPassesThroughOpening)
                        {
                            splitPos.InsertMeInBetweenTheseTwo(curRect.MazePointLeft, curRect.MazePointRight);
                            if (visibleRectangle.IntersectsWith(splitPos))
                            {
                                pathMap[splitPos.X - visibleRectangle.X, splitPos.Y - visibleRectangle.Y] = true;
                            }
                        }

                        //DrawRedStuff
                        if (rect1.PathPassesThroughThis && rect1.Height == 3)
                        {
                            FillInPathForRectangleX(visibleRectangle, pathMap, rect1.MazePointLeft, rect1.MazePointRight, rect1);
                        }

                        if (rect2.PathPassesThroughThis && rect2.Height == 3)
                        {
                            //FillInPathForRectangle(visibleRectangle, pathMap, curRect.MazePointRight.Previous, curRect.MazePointRight, rect2);
                            FillInPathForRectangleX(visibleRectangle, pathMap, rect2.MazePointLeft, rect2.MazePointRight, rect2);
                        }
                    }

                    int xStartDraw = Math.Max(0, curRect.X - xStart);
                    int xEndDraw   = Math.Min(widthPart, curRect.X - xStart + curRect.Width);

                    int yPos = curRect.Y + splitnumber - yStart;

                    if (yPos >= 0 && yPos < heightPart - 1)
                    {
                        for (int i = xStartDraw; i < xEndDraw; i++)
                        {
                            if (i != opening - xStart)
                            {
                                map[i, yPos] = false;
                            }
                        }
                    }

                    if (IsValidRect(visibleRectangle, rect1))
                    {
                        rectangles.Push(rect1);
                    }
                    if (IsValidRect(visibleRectangle, rect2))
                    {
                        rectangles.Push(rect2);
                    }
                }
                else
                {
                    int splitnumber = 2 + random.Next((curRect.Width - 2) / 2) * 2;
                    int opening     = 1 + random.Next((curRect.Height) / 2) * 2 + curRect.Y;

                    var splitPos = new MazePointClassLinkedList(curRect.X + splitnumber, opening);

                    var rect1 = new RectangleWithPath(curRect.X, curRect.Y, splitnumber + 1, curRect.Height, random.Next());
                    var rect2 = new RectangleWithPath(curRect.X + splitnumber, curRect.Y, curRect.Width - splitnumber, curRect.Height, random.Next());

                    if (curRect.PathPassesThroughThis)
                    {
                        var pathPassesThroughOpening = AreNumberOnTheSidesOfThisValue(splitPos.X, curRect.MazePointLeft.X, curRect.MazePointRight.X);
                        DetermineRectanglePathPassingThrough(curRect, rect1, splitPos);
                        DetermineRectanglePathPassingThrough(curRect, rect2, splitPos);

                        if (pathPassesThroughOpening)
                        {
                            splitPos.InsertMeInBetweenTheseTwo(curRect.MazePointLeft, curRect.MazePointRight);
                            if (visibleRectangle.IntersectsWith(splitPos))
                            {
                                pathMap[splitPos.X - visibleRectangle.X, splitPos.Y - visibleRectangle.Y] = true;
                            }
                        }

                        //DrawRedStuff
                        if (rect1.PathPassesThroughThis && rect1.Width == 3)
                        {
                            FillInPathForRectangleY(visibleRectangle, pathMap, rect1.MazePointLeft, rect1.MazePointRight, rect1);
                        }

                        if (rect2.PathPassesThroughThis && rect2.Width == 3)
                        {
                            //FillInPathForRectangle(visibleRectangle, pathMap, curRect.MazePointRight.Previous, curRect.MazePointRight, rect2);
                            FillInPathForRectangleY(visibleRectangle, pathMap, rect2.MazePointLeft, rect2.MazePointRight, rect2);
                        }
                    }

                    var yStartDraw = Math.Max(0, curRect.Y - yStart);
                    int yEndDraw   = Math.Min(heightPart, curRect.Y - yStart + curRect.Height);

                    int xPos = curRect.X + splitnumber - xStart;

                    if (xPos >= 0 && xPos < widthPart - 1)
                    {
                        for (int i = yStartDraw; i < yEndDraw; i++)
                        {
                            if (i != opening - yStart)
                            {
                                map[xPos, i] = false;
                            }
                        }
                    }

                    if (IsValidRect(visibleRectangle, rect1))
                    {
                        rectangles.Push(rect1);
                    }
                    if (IsValidRect(visibleRectangle, rect2))
                    {
                        rectangles.Push(rect2);
                    }
                }

                //using (var fs = new FileStream("DivisionDynamicWithPath2.png", FileMode.Create))
                //{
                //    WithPath.SaveMazeAsImageDeluxePng(map, pathMap, fs);
                //}
            }

            map.PathData = pathMap;
            return(map);
        }