到目前为止,我已经发布了我所做的并遵循以下原则,但我似乎弄错了。我在 2-3 年前生成填字游戏的方式是:
- 我有一个代表一个词的类。它有坐标 (x, y) - 它从哪里开始、方向(水平、垂直)和单词本身。
- 我有一个单词列表,如果需要,我可以在算法开始之前将其混合。
- 我取第一个单词并将其水平放置在 (0,0) 上,然后将其放入解决方案列表中。
- “我往里走一层”(递归),我接下一个词。
- 我浏览了解决方案列表并尝试将其放在某个地方(条件:两个单词有一个共同的字母并且方向不同)
- 如果我找到它,我会将它添加到解决方案中 - 如果它们有一个共同的字母,您可以轻松计算起点和方向。
- 我检查部分解决方案是否正确(所有相交的单词都有相同的字母)——也许这是最复杂的。也许这将有助于保留它相交的每个单词。8.1。如果OK,继续(直到单词列表或其他条件,例如填字游戏的大小,完成) 8.2。如果它是错误的,我从解决方案中删除该单词,删除一级递归(回溯) - 例如返回 null,然后尝试下一个单词。
有人会帮助我或指导我解决问题的错误吗?我目前正在生成一个带有单词的填字游戏(不是正确的填字游戏箭头类型)
<?php
class Board {
private $arrBoard;
private $arrWords;
private $iCols;
private $iRows;
public function Board($nRows, $nCols) {
$this->iCols = $nCols;
$this->iRows = $nRows;
for ($col = 0; $col < $this->iCols; $col++) {
for ($row = 0; $row < $this->iRows; $row++) {
$this->arrBoard[$row][$col] = '*';
}
}
}
public function visualize() {
echo "<table border='1'>";
for ($col = 0; $col < $this->iCols; $col++) {
echo "<tr c='$col'>";
for ($row = 0; $row < $this->iRows; $row++) {
echo "<td r='$row'>" . $this->arrBoard[$row][$col] . '</td>';
}
echo "</tr>";
}
echo "</table>";
}
private function fillRubish() {
}
public function assignWordList($arrList) {
$this->arrWords = $arrList;
}
private function writeHorizontalWord($sWord) {
$arrWord = preg_split('//u', $sWord, 0, PREG_SPLIT_NO_EMPTY);
echo "Horizontal word: $sWord </br>";
$arrPos = $this->getRandPosH($sWord);
$col = $arrPos['col'];
$row = $arrPos['row'];
foreach ($arrWord as $sLetter) {
$this->arrBoard[$col][$row] = $sLetter;
$col++;
}
}
private function writeVerticalWord($sWord) {
$arrWord = preg_split('//u', $sWord, 0, PREG_SPLIT_NO_EMPTY);
echo "Vertical word: $sWord </br>";
$arrPos = $this->getRandPosV($sWord);
$col = $arrPos['col'];
$row = $arrPos['row'];
foreach ($arrWord as $sLetter) {
$this->arrBoard[$col][$row] = $sLetter;
$row++;
}
}
private function getRandPosH($sWord) {
$iWordLength = strlen($sWord);
$row = rand(0, $this->iRows-1);
$col = rand(0, ($this->iCols-$iWordLength)-1);
return array('col' => $col,'row' => $row);
}
private function getRandPosV($sWord) {
$iWordLength = strlen($sWord);
$row = rand(0, ($this->iRows-$iWordLength)-1);
$col = rand(0, $this->iCols-1);
return array('col' => $col,'row' => $row);
}
public function writeWords() {
foreach($this->arrWords as $sWord => $sArrange) {
$i++;
if ($sArrange == 'v') {
$this->writeVerticalWord($sWord);
} else if ($sArrange == 'h') {
$this->writeHorizontalWord($sWord);
}
}
}
}
$oBoard = new Board(6,6);
//$oBoard->visualize();
$arrWords = array('smart' => 'h',
'one' => 'h',
'two' => 'h',
'market' => 'h',
'zoomarket' => 'h',
'store' => 'h',
'shop' => 'h',
'everyday' => 'h',
'everyone' => 'h',
'underground' => 'h',
'business' => 'h',
'small' => 'h',
'okay' => 'h',
'big' => 'h',
'climate' => 'v',
'cocacola' => 'v',
'talk' => 'v',
'me' => 'v',
'tibel' => 'v',
'maxima' => 'v',
'dog' => 'v',
'cat' => 'v',
'frog' => 'v',
'human' => 'v',
'blue' => 'v',
'red' => 'v',
'phone' => 'v',
'telephone' => 'v',
);
$oBoard->assignWordList($arrWords);
$oBoard->writeWords();
echo '</br>================================================== </br>';
$oBoard->visualize();
输出:
s l t p r m
* e i h e a
s p b o d x
e h e n l i
o o l e k m
b n t l n a