为什么我会收到“缺少参数”警告?在我的 Boggle 解算器上?

发布于 2024-12-28 15:08:46 字数 2624 浏览 2 评论 0原文

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

如何查找可能的列表来自字母矩阵的单词 [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);

Some years ago here someone had posted some PHP code on a Boggle solver they made with PHP.

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

I have been trying to get it to work but I get an error of "Warning: Missing argument 7 for find_words()" which I already know its because he seemed to forget to pass a variable into the function. I have been trying to get it to work but I cant seem to figure it out, can anyone else do it? Any chance anyone have something a little fancier? Maybe something that can track the path of the words? Anyways if anyone could have some input that would be awesome.

$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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酷到爆炸 2025-01-04 15:08:46

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

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

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

Try replacing the find_words call at the bottom with:

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

He just forgot to pass in one argument, the code looks... acceptable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文