Exemplo n.º 1
0
        public static int randname_make(randname_type name_type, int min, int max, ref string word_buf, int buflen, string [][]sections)
        {
            int lnum = 0;
            bool found_word = false;

            Misc.assert(name_type > 0 && name_type < randname_type.RANDNAME_NUM_TYPES);

            /* To allow for a terminating character */
            Misc.assert(buflen > max);

            /* We cache one set of probabilities, only regenerate when
               the type changes.  It's as good a way as any for now.
               Frankly, we could probably regenerate every time. */
            if (cached_type != name_type)
            {
                string[] wordlist = null;
                //const char **wordlist = null;

                wordlist = sections[(int)name_type];

                build_prob(lprobs, wordlist);

                cached_type = name_type;
            }

            /* Generate the actual word wanted. */
            while (!found_word)
            {
                string cp = word_buf;
                //char *cp = word_buf;
                int c_prev = S_WORD;
                int c_cur = S_WORD;
                int tries = 0;
                bool contains_vowel = false;
                lnum = 0;

                /* We start the word again if we run out of space or have
                   had to have 10 goes to find a word that satisfies the
                   minimal conditions. */
                while (tries < 10 && lnum <= max && !found_word)
                {
                    /* Pick the next letter based on a simple weighting
                      of which letters can follow the previous two */
                    int r;
                    int c_next = 0;

                    Misc.assert(c_prev >= 0 && c_prev <= S_WORD);
                    Misc.assert(c_cur >= 0 && c_cur <= S_WORD);

                    r = (int)Random.randint0(lprobs[c_prev, c_cur, TOTAL]);

                    while (r >= lprobs[c_prev, c_cur, c_next])
                    {
                        r -= lprobs[c_prev, c_cur, c_next];
                        c_next++;
                    }

                    Misc.assert(c_next <= E_WORD);
                    Misc.assert(c_next >= 0);

                    if (c_next == E_WORD)
                    {
                        /* If we've reached the end, we check if we've
                           met the simple conditions, otherwise have
                           another go at choosing a letter for this
                           position. */
                        if (lnum >= min && contains_vowel)
                        {
                            //cp = "";// = '\0'; We don't need null terminator anymore...
                            word_buf = cp;
                            found_word = true;
                        }
                        else
                        {
                            tries++;
                        }
                    }
                    else
                    {
                        /* Add the letter to the word and move on. */
                        char curr = Basic.I2A(c_next);
                        cp += curr;
                        //*cp = I2A(c_next);

                        if (is_a_vowel(curr))
                            contains_vowel = true;

                        //cp++;//this was handled by cp += curr...
                        lnum++;
                        Misc.assert(c_next <= S_WORD);
                        Misc.assert(c_next >= 0);
                        c_prev = c_cur;
                        c_cur = c_next;
                    }
                }
            }

            return lnum;
        }
Exemplo n.º 2
0
        public static int randname_make(randname_type name_type, int min, int max, ref string word_buf, int buflen, string [][] sections)
        {
            int  lnum       = 0;
            bool found_word = false;

            Misc.assert(name_type > 0 && name_type < randname_type.RANDNAME_NUM_TYPES);

            /* To allow for a terminating character */
            Misc.assert(buflen > max);

            /* We cache one set of probabilities, only regenerate when
             * the type changes.  It's as good a way as any for now.
             * Frankly, we could probably regenerate every time. */
            if (cached_type != name_type)
            {
                string[] wordlist = null;
                //const char **wordlist = null;

                wordlist = sections[(int)name_type];

                build_prob(lprobs, wordlist);

                cached_type = name_type;
            }

            /* Generate the actual word wanted. */
            while (!found_word)
            {
                string cp = word_buf;
                //char *cp = word_buf;
                int  c_prev         = S_WORD;
                int  c_cur          = S_WORD;
                int  tries          = 0;
                bool contains_vowel = false;
                lnum = 0;

                /* We start the word again if we run out of space or have
                 * had to have 10 goes to find a word that satisfies the
                 * minimal conditions. */
                while (tries < 10 && lnum <= max && !found_word)
                {
                    /* Pick the next letter based on a simple weighting
                     * of which letters can follow the previous two */
                    int r;
                    int c_next = 0;

                    Misc.assert(c_prev >= 0 && c_prev <= S_WORD);
                    Misc.assert(c_cur >= 0 && c_cur <= S_WORD);

                    r = (int)Random.randint0(lprobs[c_prev, c_cur, TOTAL]);

                    while (r >= lprobs[c_prev, c_cur, c_next])
                    {
                        r -= lprobs[c_prev, c_cur, c_next];
                        c_next++;
                    }

                    Misc.assert(c_next <= E_WORD);
                    Misc.assert(c_next >= 0);

                    if (c_next == E_WORD)
                    {
                        /* If we've reached the end, we check if we've
                         * met the simple conditions, otherwise have
                         * another go at choosing a letter for this
                         * position. */
                        if (lnum >= min && contains_vowel)
                        {
                            //cp = "";// = '\0'; We don't need null terminator anymore...
                            word_buf   = cp;
                            found_word = true;
                        }
                        else
                        {
                            tries++;
                        }
                    }
                    else
                    {
                        /* Add the letter to the word and move on. */
                        char curr = Basic.I2A(c_next);
                        cp += curr;
                        //*cp = I2A(c_next);

                        if (is_a_vowel(curr))
                        {
                            contains_vowel = true;
                        }

                        //cp++;//this was handled by cp += curr...
                        lnum++;
                        Misc.assert(c_next <= S_WORD);
                        Misc.assert(c_next >= 0);
                        c_prev = c_cur;
                        c_cur  = c_next;
                    }
                }
            }

            return(lnum);
        }