首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我在Boggle求解器上得到警告“缺少参数”?

为什么我在Boggle求解器上得到警告“缺少参数”?
EN

Stack Overflow用户
提问于 2012-01-24 15:40:05
回答 1查看 3.7K关注 0票数 0

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

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

我一直在尝试让它工作,但我得到了一个错误"Warning: Missing argument 7 for find_words()“,我已经知道了,因为他似乎忘记了将变量传递给函数。我一直在试着让它工作,但我似乎无法弄清楚,其他人能做到吗?有没有人有更花哨的东西?也许是一些可以追踪单词路径的东西?无论如何,如果有人能提供一些意见,那就太棒了。

代码语言:javascript
复制
$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);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-01-24 15:43:56

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

代码语言:javascript
复制
find_words($graph, $chardict, 'None', array(), $prefixes, $solution, $words);

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8983455

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档