拼字游戏单词生成器

时间:2012-04-21 13:18:07

标签: php sql algorithm

看着我的兄弟在像拼字游戏这样的iphone游戏中作弊后,我想知道什么是algotithm behing it。

给出一些字母:A B C T E E

并且SQL表中充满了正确的单词。

我如何创建所有字母组合,以便制作一个像以下的选择: 从单词IN('A','AT',...)的单词中选择*,只是从这些组合中取出那些正确的组合?¿

另一种可能的方法是SQL表,每个单词的列中包含每个字母。 但之后系统应该验证选择的任何单词是否有更多时间给出相同的字母。

例如:

c1 c2 c3 c4 是的 a i r

这个问题只是为了提供好奇心,并且可以用它来学习所有这些组合(带有完整和部分给定的字母),以便在事后检查它们是否存在。

谢谢!

font:http://icon.cat/worder/wordsfinder

5 个答案:

答案 0 :(得分:2)

要查找所有可能的有效字,请执行以下步骤

  1. 查找所有可能的组合
  2. 查找组合中每个单词的每个排列
  3. 在数据库中搜索单词
  4. 列出单词
  5. 脚本

    $tiles  = array( "A", "B", "C", "T", "E", "E") ;
    $words = array();
    $set = powerSet($tiles,2);
    
    $mysql = new mysqli("localhost","root","","word");
    $sql = "SELECT id from dic WHERE word = '%s'" ;
    
    foreach ($set as $key => $value)
    {
        $word = implode("", $value);
        $wordPermutation = permute($word);
    
        foreach($wordPermutation as $keyWord)
        {
            if(!in_array($keyWord, $words))
            {
                //if($result = $mysql->query(sprintf($sql,$keyWord)))
                //{
                    //var_dump(sprintf($sql,$keyWord));
                    //if($result->num_rows > 0)
                    //{
                        $words[] = $keyWord ;
                    //}
                //}
            }
        }
    }
    
    
    print_r($words);
    

    功能

    function powerSet($in, $minLength = 1, $max = 10) {
        $count = count ( $in );
        $members = pow ( 2, $count );
        $return = array ();
        for($i = 0; $i < $members; $i ++) {
            $b = sprintf ( "%0" . $count . "b", $i );
            $out = array ();
            for($j = 0; $j < $count; $j ++) {
                if ($b {$j} == '1')
                    $out [] = $in [$j];
            }
            if (count ( $out ) >= $minLength && count ( $out ) <= $max) {
                $return [] = $out;
            }
    
        }
        return $return;
    }
    
    
    function permute($str) {
        if (strlen($str) < 2) {
            return array($str);
        }
        $permutations = array();
        $tail = substr($str, 1);
        foreach (permute($tail) as $permutation) {
            $length = strlen($permutation);
            for ($i = 0; $i <= $length; $i++) {
                $permutations[] = substr($permutation, 0, $i) . $str[0] . substr($permutation, $i);
            }
        }
        return $permutations;
    }
    

    请注意,我commented退出数据库验证部分,以便演示可以正常工作

    参见演示

    http://codepad.viper-7.com/oG6E6w

答案 1 :(得分:1)

我会尝试像

这样的东西
WHERE (word like '%A%' and not word like '%A%A%')
  AND (word like '%B%' and not word like '%B%B%')

等等。但我确信必须有更专业的解决方案!

答案 2 :(得分:1)

我终于明白了。

如果有人对制作自己的单词生成器感兴趣,那么我就是这样做的。

MySQL,一个表格:

[id] , [Word]

每个长度的视图:

V1 = Select Word from TABLE where LENGTH(Word) = 1
V2 = Select Word from TABLE where LENGTH(Word) = 2
[...]

php方面:

使用baba的功能,我创建了一个数组:array [2]是长度为2的字母组合,依此类推。

最后,我所要做的就是为视图中的每个数组选择

Select Word from V3 where Word like ('asd','dsa',....);

必须有一种更快的方式,但是不到一秒钟(本地主机)和700K的单词指令就会成功。

答案 3 :(得分:1)

实现解扰的更好方法是使用字谜。因此,不要使用包含所有可能单词的库,而是使用组成单词的字母作为索引的关联数组。

anagram['aer'] = ['are', 'ear', 'era']

要实现这一点,请遍历所有词典单词并将每个单词推送到一个数组中,其中索引是字母顺序的字母。

for(var i = 0; i < dictionary.length; i++) {
//Loop through dictionary array
    var str = words[i].split('').sort().join('');
    //break apart the word and sort it alphabetically
    if(!anagram[str]) {
        //check if there is already an index with that same anagram
        anagram[str] = [];
    }

    anagram[str].push(words[i]);
    //Add the word to the anagram array

}

这种方式允许您快速索引库,而无需经过数千种可能的排列。

javascript中此方法的一个示例:Word Unscrambler

答案 4 :(得分:0)

以下是关于World fastest scrabble program

的精彩文章

你应该对Descrete Math(Word Automats)有一些了解。希望它能帮助你:))

相关问题