首先获取一行的所有字母。然后将第一个字母复制到一个字符串中并与字典进行比较。接下来,你取前两个字母并做同样的事情。当您达到 7 个字符长度时,您将最后 6 个字母与字典进行比较。当您完成该行中的所有检查后,请转到下一行。当所有行都完成后,对列执行相同的操作。
伪代码:
for ($currentCollumn=1; $gridWidth > $currentCollumn; $currentCollumn++) {
$collumn = get collumn as string
for ($i=1;$i!=7;$i++) {
get first $i characters of $collumn and compare to dictionary
}
for ($i=1;$i!=7;$i++) {
get last $i characters of $collumn and compare to dictionary
}
}
for ($currentRow=1; $gridHeight > $currentRow; $currentRow++) {
$row = get row as string
for ($i=1;$i!=7;$i++) {
get first $i characters of $row and compare to dictionary
}
for ($i=1;$i!=7;$i++) {
get last $i characters of $row and compare to dictionary
}
}
编辑:第 2 版,因为我没有意识到这些词不仅限于直线。
从每个可能的位置开始。
// define variables:
booleanArray path[x][y] = false;
p = pointerlocation;
stack[] = p;
currentString = "";
p.up/.down/.left/.right 检查 path[][] where y+1, y-1, x+1, x-1 分别。如果超出范围,它将返回 false。
The stack[] works like an x86 stack. Last in, first out.
push() adds a new object onto the stack
pop() gets the last object added to the stack and removes it from the stack
function getAllTheStrings(p.x, p.y) {
while (p) {
path (p.x, p.y) = true;
currentString += p.getCharacter();
// check neighbors
if (p.up) {
stack.push(p.up, path, currentString);
}
if (p.down) {
stack.push(p.down, path, currentString);
}
if (p.left) {
stack.push(p.left, path, currentString);
}
if (p.right) {
stack.push(p.right, path, currentString);
}
// add current string to list possible words
foundWords.push(currentString);
// pop next item from stack
overwrite variables with stored values of: p, path, currentString
if (stack is empty) {
p = false; // end loop
}
}
}
And that would be called by:
function getWords() {
for ($y=1; $gridHeight > $y; $y++) {
for ($x=1; $gridWidth > $x; $x++) {
getAllTheStrings(x,y);
}
}
This function scales very badly with grid size since it has to check every single combination of possible paths. A 1x1 grid would take one test. A 2x2 would take 28 tests. At 3x3 I lost count at about 270 tests.
After the loop is finished foundWords
would be checked word for word against the whole dictionary. 5 found words and 100 words in the dictionary would give 500 comparisons. In reality the dictionary would have at least a thousand words.