Beispiel #1
0
 /// <summary>
 /// 从list中随机抽取count个元素
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="ran"></param>
 /// <param name="list"></param>
 /// <param name="count"></param>
 /// <returns></returns>
 public static List <T> Sample <T>(this Random ran, IList <T> list, int count)
 {
     return(new int[count].Select(x => ran.Choice(list)).ToList());
 }
Beispiel #2
0
        /// <summary>
        /// 获取图片bytes和宽高
        /// </summary>
        /// <returns></returns>
        public (byte[] bs, int width, int height) GetImageBytesAndSize()
        {
            if (CharCount <= 0)
            {
                throw new Exception("字符数必须大于0");
            }
            this.Code = string.Empty;

            var items = Com.Range(CharCount).Select(_ => new CharItem()
            {
                c    = random.Choice(chars).ToString(),
                font = new Font(random.Choice(fonts), FontSize)
            }).ToList();

            //把验证码保存起来
            this.Code = "".Join(items.Select(x => x.c));
            int Height = (int)(items.Select(x => x.font).Max(x => x.Height) * 1.3);
            int Width  = (int)(Height * 0.8 * CharCount);

            //获取随机字体,颜色
            using (var bm = new Bitmap(Width, Height))
            {
                using (var g = Graphics.FromImage(bm))
                {
                    g.Clear(Color.White);
                    using (var ms = new MemoryStream())
                    {
                        //判断是否画噪线
                        if (LineCount > 0)
                        {
                            for (int k = 0; k < LineCount; ++k)
                            {
                                var x1 = random.Next(bm.Width);
                                var y1 = random.Next(bm.Height);
                                var x2 = random.Next(bm.Width);
                                var y2 = random.Next(bm.Height);
                                g.DrawLine(new Pen(random.Choice(colors)), x1, y1, x2, y2);
                            }
                        }
                        //画验证码
                        var i = 0;
                        foreach (var itm in items)
                        {
                            //计算位置
                            var(x, y) = ComputePosition(i++, itm.font, bm);

                            var angle = random.Next(-5, 5);
                            g.RotateTransform(angle);

                            g.DrawString(itm.c, itm.font, new SolidBrush(random.Choice(colors)), x, y);

                            g.RotateTransform(-angle);
                        }

                        bm.Save(ms, ImageFormat.Png);
                        return(ms.ToArray(), Width, Height);

                        /*
                         * byte[] bs = ms.ToArray();
                         * response.OutputStream.Write(bs, 0, bs.Length);
                         * */
                    }
                }
            }
        }
Beispiel #3
0
 public static T Choice <T>(IList <T> a)
 {
     lock (Lock)
         return(gen.Choice(a));
 }
Beispiel #4
0
 public static T Choice <T>(this Random random, IEnumerable <T> items)
 {
     return(random.Choice(items.ToList()));
 }
Beispiel #5
0
 public RandomWalkStepResult Step()
 {
     return(DebugStep(_random.Choice(Actions)));
 }
Beispiel #6
0
        public override void Process(TileMap map)
        {
            var dirt = new DirtLookup((y, x) =>
            {
                if (y < 0 || y >= map.Height || x < 0 || x >= map.Width)
                {
                    return(false);
                }
                var cell     = map[y, x];
                var material = cell.Block as Material;
                if (material != null && material.Type == MaterialType.Dirt)
                {
                    return(true);
                }
                var tile = cell.Block as Tile;
                if (tile != null && DirtTypes.Contains(tile.Id))
                {
                    return(true);
                }
                return(false);
            });

            var random = new Random();

            for (var y = 0; y < map.Height; y++)
            {
                for (var x = 0; x < map.Width; x++)
                {
                    var cell = map[y, x];
                    if (dirt.Build(map, y, x))
                    {
                        var block = cell.Block;
                        if (dirt.Match(null, false, null, true, null, true, null, true))
                        {
                            // case #1
                            block = new Tile(random.Choice(50, 55));
                        }
                        else if (dirt.Match(null, false, null, false, null, true, null, true))
                        {
                            // case #2
                            block = new Tile(random.Choice(50, 55));
                        }
                        else if (dirt.Match(null, false, null, null, null, true, null, false))
                        {
                            // case #3
                            block = new Tile(60);
                        }
                        else if (dirt.Match(null, true, null, true, null, true, null, false))
                        {
                            // case #4
                            block = new Tile(random.Choice(54, 59));
                        }
                        else if (dirt.Match(false, true, null, true, null, true, null, true))
                        {
                            // case #5
                            block = new Tile(56);
                        }
                        else if (dirt.Match(null, true, null, true, null, false, null, false))
                        {
                            // case #6
                            block = new Tile(69);
                        }
                        else if (dirt.Match(null, false, null, null, null, false, null, false))
                        {
                            // case #7
                            block = new Tile(70);
                        }
                        else if (dirt.Match(null, false, null, null, null, false, null, true))
                        {
                            // case #8
                            block = new Tile(65);
                        }
                        else if (dirt.Match(null, false, null, null, null, true, false, true))
                        {
                            // case #9
                            block = new Tile(51);
                        }
                        else if (dirt.Match(null, true, null, null, null, false, null, true))
                        {
                            // case #10
                            block = new Tile(64);
                        }
                        cell.Block = block;

                        // add decorations
                        if (dirt.Match(null, false, null, null, null, null, null, null))
                        {
                            var above = map[y - 1, x];
                            // add grass etc.
                            var grass = random.Choice(43, 44);
                            above.Foreground.Add(new Tile(grass));
                            if (grass == 44)
                            {
                                // we can optionally add flowers
                                if (random.Next(5) == 0)
                                {
                                    above.Foreground.Add(new Tile(random.Choice(32, 39, 46)));
                                }
                            }
                        }
                    }
                }
            }
        }
Beispiel #7
0
        /*private static HashSet<int> DirtTypes = new HashSet<int>
         * {
         *  0, 1, 2, 7, 8, 9, 14, 15, 16, 21, 28, 35, 42
         * };*/

        public override void Process(TileMap map)
        {
            var dirt = new DirtLookup((y, x) =>
            {
                if (y < 0 || y >= map.Height || x < 0 || x >= map.Width)
                {
                    return(false);
                }
                var cell = map[y, x];

                /*var material = cell.Block as Material;
                 * if (material != null && material.Type == MaterialType.Dirt) return true;
                 * var tile = cell.Block as Block;
                 * if (tile != null && DirtTypes.Contains(tile.Id)) return true;
                 * return false;*/
                return(cell.Block != null);
            });

            var random = new Random();

            for (var y = 0; y < map.Height; y++)
            {
                for (var x = 0; x < map.Width; x++)
                {
                    var cell = map[y, x];
                    if (dirt.Build(map, y, x))
                    {
                        var block = cell.Block;
                        if (dirt.Match(null, false, null, true, null, true, null, false))
                        {
                            block = new Tile(0);
                        }
                        else if (dirt.Match(null, false, null, false, null, true, null, true))
                        {
                            block = new Tile(14);
                        }
                        else if (dirt.Match(null, false, null, true, null, true, null, true))
                        {
                            block = new Tile(7);
                        }
                        else if (dirt.Match(null, true, null, true, null, true, null, false))
                        {
                            block = new Tile(1);
                        }
                        else if (dirt.Match(null, true, null, true, null, false, null, false))
                        {
                            block = new Tile(2);
                        }
                        else if (dirt.Match(null, true, null, false, null, false, null, true))
                        {
                            block = new Tile(16);
                        }
                        else if (dirt.Match(null, true, null, true, null, false, null, true))
                        {
                            block = new Tile(9);
                        }
                        else if (dirt.Match(null, true, null, false, null, true, null, true))
                        {
                            block = new Tile(15);
                        }
                        else if (dirt.Match(null, false, null, false, null, false, null, false))
                        {
                            block = new Tile(42);
                        }
                        else if (dirt.Match(null, false, null, true, null, false, null, false))
                        {
                            block = new Tile(21);
                        }
                        else if (dirt.Match(null, false, null, false, null, false, null, true))
                        {
                            block = new Tile(35);
                        }
                        else if (dirt.Match(null, false, null, true, null, false, null, true))
                        {
                            block = new Tile(28);
                        }
                        cell.Block = block;

                        // add decorations
                        if (dirt.Match(null, false, null, null, null, null, null, null))
                        {
                            var above = map[y - 1, x];
                            // add grass etc.
                            var grass = random.Choice(43, 44);
                            above.Foreground.Add(new Tile(grass));
                            if (random.Next(10) == 0 && grass == 43)
                            {
                                // randomly add background grass
                                above.Background.Add(new Tile(44));
                            }
                            if (grass == 44)
                            {
                                // we can optionally add flowers
                                if (random.Next(5) == 0)
                                {
                                    above.Foreground.Add(new Tile(random.Choice(32, 39, 46)));
                                }
                            }
                        }
                    }
                }
            }
        }
Beispiel #8
0
        private void SyncDataSet(Dom.Action action)
        {
            System.Diagnostics.Debug.Assert(_iteration != 0);

            // Only sync <Data> elements if the action has a data model
            if (action.dataModel == null)
            {
                return;
            }

            string         key = GetDataModelName(action);
            DataSetTracker val = null;

            if (!_dataSets.TryGetValue(key, out val))
            {
                return;
            }

            // If the last switch was within the current iteration range then we don't have to switch.
            uint switchIteration = GetSwitchIteration();

            if (switchIteration == val.iteration)
            {
                return;
            }

            // Don't switch files if we are only using a single file :)
            if (val.options.Count < 2)
            {
                return;
            }

            DataModel dataModel = null;

            // Some of our sample files may not crack.  Loop through them until we
            // find a good sample file.
            while (val.options.Count > 0 && dataModel == null)
            {
                Data option = _randomDataSet.Choice(val.options);

                if (option.DataType == DataType.File)
                {
                    try
                    {
                        dataModel = ApplyFileData(action, option);
                    }
                    catch (CrackingFailure)
                    {
                        logger.Debug("Removing " + option.FileName + " from sample list.  Unable to crack.");
                        val.options.Remove(option);
                    }
                }
                else if (option.DataType == DataType.Fields)
                {
                    try
                    {
                        dataModel = AppleFieldData(action, option);
                    }
                    catch (PeachException)
                    {
                        logger.Debug("Removing " + option.name + " from sample list.  Unable to apply fields.");
                        val.options.Remove(option);
                    }
                }
            }

            if (dataModel == null)
            {
                throw new PeachException("Error, RandomStrategy was unable to load data for model \"" + action.dataModel.fullName + "\"");
            }

            // Set new data model
            action.dataModel = dataModel;

            // Generate all values;
            var ret = action.dataModel.Value;

            System.Diagnostics.Debug.Assert(ret != null);

            // Store copy of new origional data model
            action.origionalDataModel = action.dataModel.Clone() as DataModel;

            // Save our current state
            val.iteration = switchIteration;
        }
Beispiel #9
0
        public override void SetUp()
        {
            base.SetUp();

            //this.Context.Map = BinTileMapSerializer.Load("editor.map");

            /*if (File.Exists("landscape.map"))
             * {
             *  this.Context.Map = BinTileMapSerializer.Load("landscape.map");
             * }
             * else
             * {
             *  this.GenerateTerrain();
             *  BinTileMapSerializer.Save("landscape.map", this.Context.Map);
             * }*/
            this.Context.Map.SaveToImage(this.Graphics, "map.png");

            this.characters.GetOrAdd("Cat", (name) => new Character(name)
            {
                JumpPower     = 500f,
                WalkSpeed     = 400f,
                RunSpeed      = 500f,
                ClimbSpeed    = 300f,
                WalkMaxSpeed  = 150f,
                RunMaxSpeed   = 250f,
                ClimbMaxSpeed = 100f,
                WaterModifier = 0.4f,
                SwimPower     = 200f,
                Sprite        = Store.Instance.Sprites <NamedAnimatedSpriteSheetTemplate>("Base", "player.cat"),
                Bounds        = new RectangleF(Point2.Zero, new Size2(8, 16)),
            });

            this.characters.GetOrAdd("Bear", (name) => new Character(name)
            {
                JumpPower     = 400f,
                WalkSpeed     = 300f,
                RunSpeed      = 400f,
                ClimbSpeed    = 300f,
                WalkMaxSpeed  = 100f,
                RunMaxSpeed   = 150f,
                ClimbMaxSpeed = 100f,
                WaterModifier = 0.4f,
                SwimPower     = 200f,
                Sprite        = Store.Instance.Sprites <NamedAnimatedSpriteSheetTemplate>("Base", "player.bear"),
                Bounds        = new RectangleF(Point2.Zero, new Size2(8, 16)),
            });

            this.characters.GetOrAdd("Pig", (name) => new Character(name)
            {
                JumpPower     = 450f,
                WalkSpeed     = 350f,
                RunSpeed      = 450f,
                ClimbSpeed    = 300f,
                WalkMaxSpeed  = 125f,
                RunMaxSpeed   = 175f,
                ClimbMaxSpeed = 100f,
                WaterModifier = 0.4f,
                SwimPower     = 200f,
                Sprite        = Store.Instance.Sprites <NamedAnimatedSpriteSheetTemplate>("Base", "player.pig"),
                Bounds        = new RectangleF(Point2.Zero, new Size2(8, 16)),
            });

            this.characters.GetOrAdd("Girl", (name) => new Character(name)
            {
                JumpPower     = 450f,
                WalkSpeed     = 350f,
                RunSpeed      = 450f,
                ClimbSpeed    = 300f,
                WalkMaxSpeed  = 125f,
                RunMaxSpeed   = 175f,
                ClimbMaxSpeed = 100f,
                WaterModifier = 0.4f,
                SwimPower     = 200f,
                Sprite        = Store.Instance.Sprites <NamedAnimatedSpriteSheetTemplate>("Base", "player.girl"),
                Bounds        = new RectangleF(Point2.Zero, new Size2(12, 20)),
            });

            this.characters.GetOrAdd("Worm", (name) => new Enemy(name)
            {
                WalkSpeed     = 270f,
                RunSpeed      = 270f,
                WalkMaxSpeed  = 25f,
                RunMaxSpeed   = 25f,
                WaterModifier = 0.4f,
                Sprite        = Store.Instance.Sprites <NamedAnimatedSpriteSheetTemplate>("Base", "enemy.worm"),
                Bounds        = new RectangleF(Point2.Zero, new Size2(17, 18)),
            });

            var controller = new HumanCharacterController();

            controller[HumanActions.Jump]        = new KeyboardAction(Keys.Space);
            controller[HumanActions.Swim]        = new KeyboardAction(Keys.Space);
            controller[HumanActions.WalkLeft]    = new KeyboardAction(Keys.A);
            controller[HumanActions.WalkRight]   = new KeyboardAction(Keys.D);
            controller[HumanActions.RunModifier] = new OrAction(new KeyboardAction(Keys.LeftShift), new KeyboardAction(Keys.RightShift));
            controller[HumanActions.Squat]       = new KeyboardAction(Keys.S);

            this.player            = new CharacterObject(this.Context);
            this.player.Character  = this.characters["Cat"];
            this.player.Controller = controller;
            if (this.Context.Spawn.Any())
            {
                var random = new Random();
                var spawn  = random.Choice(this.Context.Spawn);
                this.player.Position3D = new Vector3(spawn.World.X, spawn.World.Y, 0.5f);
            }
            else
            {
                this.player.Position3D = new Vector3(0, 0, 0.5f);
            }
            this.player.IsGravityEnabled = !this.godMode;
            this.Context.AddObject(this.player);
            this.Context.AttachLightSource(this.player, new Light
            {
                RelativePosition = new Vector2(this.player.Bounds.Width / 2, this.player.Bounds.Height / 2),
                Colour           = Color.Yellow
            });

            var enemy = new CharacterObject(this.Context);

            enemy.Character        = this.characters["Worm"];
            enemy.Controller       = new EnemyController();
            enemy.Position3D       = this.player.Position3D;
            enemy.IsGravityEnabled = true;
            enemy.Direction        = CharacterObject.Facing.Right;
            this.Context.AddObject(enemy);

            /*var npc = new CharacterObject(this.Context);
             * npc.Character = this.characters["Bear"];
             * npc.Controller = new ComputerController();
             * npc.Position3D = new Vector3(1280, startY, 0.5f);
             * npc.IsGravityEnabled = true;
             * npc.Direction = CharacterObject.Facing.Right;
             * this.Context.AddObject(npc);
             * this.Context.AttachLightSource(npc, new Light
             * {
             *  RelativePosition = new Vector2(this.player.Bounds.Width / 2, this.player.Bounds.Height / 2),
             *  Colour = Color.White
             * });*/
        }
Beispiel #10
0
 public void SelectDefault()
 {
     Clear();
     this.Add(random.Choice(choiceElements).Value);
     _selectedElement = this[0];
 }