示例#1
0
文件: Map.cs 项目: eldan-dex/randio
        //Returns a new Zone placed randomly on the map
        protected Zone GetNewZone(GraphicsDevice device, Color zoneColor, Tile selectedTile = null)
        {
            Tile tile    = (selectedTile == null) ? tiles[AlgorithmHelper.BiasedRandom(0, tiles.Count - 1, 1.3)] : selectedTile;
            int  xblocks = tile.Coords.Width / Block.Width;
            int  yblocks = tile.Coords.Height / Block.Height;

            List <int> valX = new List <int>();
            List <int> valY = new List <int>();

            for (int x = 1; x < xblocks; ++x)
            {
                for (int y = 1; y < tile.GroundLevel + 1; ++y)
                {
                    int rand = AlgorithmHelper.GetRandom(0, (xblocks * yblocks) / 20);
                    if (rand % 24 <= 3 && tile.Blocks[x, y] != null && x + 1 < tile.Blocks.GetLength(0) && y + 1 < tile.Blocks.GetLength(1))
                    {
                        rand = AlgorithmHelper.GetRandom(0, 5);
                        //Allways check whether adjacent tiles are empty -> whether the the Zone is to be created is not completely enclosed by blocks
                        if (rand == 1 && tile.Blocks[x, y - 1] == null && tile.Blocks[x, y] != null && (tile.Blocks[x - 1, y - 1] == null || tile.Blocks [x + 1, y - 1] == null))
                        {
                            valX.Add(x);
                            valY.Add(y - 1);
                        }
                        else if (rand == 2 && tile.Blocks[x, y + 1] == null && tile.Blocks.GetLength(1) > y + 2 && tile.Blocks[x, y + 2] != null && (tile.Blocks[x - 1, y + 1] == null || tile.Blocks[x + 1, y + 1] == null))
                        {
                            valX.Add(x);
                            valY.Add(y + 1);
                        }
                        else if (rand == 3 && tile.Blocks[x - 1, y] == null && tile.Blocks[x - 1, y + 1] != null && tile.Blocks[x - 2, y] == null)
                        {
                            valX.Add(x - 1);
                            valY.Add(y);
                        }
                        else if (rand == 4 && tile.Blocks[x + 1, y] == null && tile.Blocks[x + 1, y + 1] != null && tile.Blocks.GetLength(0) > x + 2 && tile.Blocks[x + 2, y] == null)
                        {
                            valX.Add(x + 1);
                            valY.Add(y);
                        }
                    }
                }
            }

            if (valX.Count == 0)
            {
                return(GetNewZone(device, zoneColor));
            }

            int rnd = AlgorithmHelper.GetRandom(0, valX.Count);

            return(new Zone(device, new Rectangle(tile.Coords.X + valX[rnd] * Block.Width, tile.Coords.Y + valY[rnd] * Block.Height, Block.Width, Block.Height), zoneColor));
        }
示例#2
0
        //Generates a word of a given length with the use of some basic linguistic rules
        public static string GenerateWord(int len)
        {
            string result = "";

            string[] syllables  = new string[] { "e", "a", "o", "i", "u", "y", "r", "l" };
            string[] consonants = new string[] { "t", "n", "s", "h", "r", "d", "l", "c", "m", "f", "g", "p", "b", "v", "w", "k", "j", "x", "z", "qu" };
            string   disabled   = "";
            bool     lastType   = AlgorithmHelper.GetRandom(0, 2) == 0;
            string   next       = "?";

            for (int i = 0; i < len; ++i)
            {
                lastType = !lastType;
                string last = next;
                if (lastType)
                {
                    do
                    {
                        next = syllables[AlgorithmHelper.BiasedRandom(0, syllables.Length - 1, 1.7)];
                    }while (disabled.Contains(next));


                    //prevent weird sounding combinations
                    if (next == "r" || next == "l")
                    {
                        disabled = "gxzqusj";
                    }
                    else
                    {
                        disabled = "";
                    }

                    if ((last == "r" || last == "l") && (next == "r" || next == "l"))
                    {
                        disabled += "rl";
                    }

                    if (next == "l")
                    {
                        disabled += "r";
                    }
                }
                else
                {
                    do
                    {
                        next = consonants[AlgorithmHelper.BiasedRandom(0, consonants.Length - 1, 1.6)];
                    }while (disabled.Contains(next));


                    if (next == "r")
                    {
                        disabled = "rl";
                    }
                    else if (next == "l")
                    {
                        disabled = "r";
                    }
                    else if (next == "w")
                    {
                        disabled = "l";
                    }
                    else
                    {
                        disabled = "";
                    }

                    if (i == len - 2) //last letter
                    {
                        disabled += "w";
                    }
                }
                result += next;

                //keep the correct length even when adding multiple letters
                if (next.Length > 1)
                {
                    i += next.Length - 1;
                }
            }

            return(result);
        }