【问题标题】:Why am I getting the warning "missing argument" on my Boggle solver?为什么我的 Boggle 求解器会收到警告“缺少参数”?
【发布时间】:2012-01-24 07:40:04
【问题描述】:

几年前,有人在他们用 PHP 制作的 Boggle 求解器上发布了一些 PHP 代码。

How to find list of possible words from a letter matrix [Boggle Solver]

我一直在尝试让它工作,但我收到“警告:find_words() 缺少参数 7”的错误,我已经知道这是因为他似乎忘记将变量传递给函数。我一直在努力让它工作,但我似乎无法弄清楚,其他人可以做到吗?有没有人有一些更高级的东西?也许可以跟踪单词路径的东西?无论如何,如果有人能提供一些很棒的意见。

$boggle = "fxie
       amlo
       ewbx
       astu";

$alphabet = str_split(str_replace(array("\n", " ", "\r"), "", strtolower($boggle)));
$rows = array_map('trim', explode("\n", $boggle));
$dictionary = file("C:/dict.txt");
$prefixes = array(''=>'');
$words = array();
$regex = '/[' . implode('', $alphabet) . ']{3,}$/S';
foreach($dictionary as $k=>$value) {
$value = trim(strtolower($value));
$length = strlen($value);
if(preg_match($regex, $value)) {
    for($x = 0; $x < $length; $x++) {
        $letter = substr($value, 0, $x+1);
        if($letter == $value) {
            $words[$value] = 1;
        } else {
            $prefixes[$letter] = 1;
        }
    }
}
}

$graph = array();
$chardict = array();
$positions = array();
$c = count($rows);
for($i = 0; $i < $c; $i++) {
$l = strlen($rows[$i]);
for($j = 0; $j < $l; $j++) {
    $chardict[$i.','.$j] = $rows[$i][$j];
    $children = array();
    $pos = array(-1,0,1);
    foreach($pos as $z) {
        $xCoord = $z + $i;
        if($xCoord < 0 || $xCoord >= count($rows)) {
            continue;
        }
        $len = strlen($rows[0]);
        foreach($pos as $w) {
            $yCoord = $j + $w;
            if(($yCoord < 0 || $yCoord >= $len) || ($z == 0 && $w == 0)) {
                continue;
            }
            $children[] = array($xCoord, $yCoord);
        }
    }
    $graph['None'][] = array($i, $j);
    $graph[$i.','.$j] = $children;
}
}

function to_word($chardict, $prefix) {
$word = array();
foreach($prefix as $v) {
    $word[] = $chardict[$v[0].','.$v[1]];
}
return implode("", $word);
}

function find_words($graph, $chardict, $position, $prefix, $prefixes, &$results, $words) {
$word = to_word($chardict, $prefix);
if(!isset($prefixes[$word])) return false;

if(isset($words[$word])) {
    $results[] = $word;
}

foreach($graph[$position] as $child) {
    if(!in_array($child, $prefix)) {
        $newprefix = $prefix;
        $newprefix[] = $child;
        find_words($graph, $chardict, $child[0].','.$child[1], $newprefix, $prefixes,         $results, $words);
    }
}
}

$solution = array();
find_words($graph, $chardict, 'None', array(), $prefixes, $solution);
print_r($solution);

【问题讨论】:

    标签: php boggle


    【解决方案1】:

    尝试将底部的 find_words 调用替换为:

    find_words($graph, $chardict, 'None', array(), $prefixes, $solution, $words);
    

    他只是忘了传入一个参数,代码看起来……可以接受。

    【讨论】:

    • 天哪,我知道我已经尝试过一百万次了,这次成功了?????? :) 谢谢人
    猜你喜欢
    • 2012-12-16
    • 1970-01-01
    • 2021-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多