[免责声明:我是PHP的新手,我只是在学习,所以请不要喷,当人们试图寻找解决方案或信息时,它确实阻碍了学习过程,谢谢,代码在填字游戏方面工作正常,这真的让我很困惑,一个人如何通过给定的信息获得对角线方向,或者我做错了什么而没有看到?]
给定一个开关:
switch ($dir){
case "E":
//col from 0 to board width - word width
//row from 0 to board height
$newCol = rand(0, $boardData["width"] - 1 - strlen($theWord));
$newRow = rand(0, $boardData["height"]-1);
for ($i = 0; $i < strlen($theWord); $i++){
//new character same row, initial column + $i
$boardLetter = $board[$newRow][$newCol + $i];
$wordLetter = substr($theWord, $i, 1);
//check for legal values in current space on board
if (($boardLetter == $wordLetter) ||
($boardLetter == ".")){
$board[$newRow][$newCol + $i] = $wordLetter;
} else {
$itWorked = FALSE;
} // end if
} // end for loop
break;
和:
case "N":
//col from 0 to board width
//row from word length to board height
$newCol = rand(0, $boardData["width"] -1);
$newRow = rand(strlen($theWord), $boardData["height"]-1);
for ($i = 0; $i < strlen($theWord); $i++){
//check for a legal move
$boardLetter = $board[$newRow - $i][$newCol];
$wordLetter = substr($theWord, $i, 1);
if (($boardLetter == $wordLetter) ||
($boardLetter == ".")){
$board[$newRow - $i][$newCol] = $wordLetter;
} else {
$itWorked = FALSE;
} // end if
} // end for loop
break;
我应该能够将两者结合起来得到 NE(或对角线文本输出到屏幕上)
但是,当我尝试这个时,它不起作用,我一直在尝试 N 和 E 的不同组合以获得 NE 或对角 NE 方向,但没有运气,是什么给出的?
#Tried multiple different combination's of N & E, I am out of ideas
switch ($dir){
case "E":
$newCol = rand(0, $boardData["width"] - 1 - strlen($theWord));
$newRow = rand(strlen($theWord), $boardData["height"]-1);
for ($i = 0; $i < strlen($theWord); $i++){
#Combined but no luck, WTF:
$boardLetter = $board[$newRow][$newRow - $i];
$boardLetter = $board[$newCol][$newCol + $i];
$wordLetter = substr($theWord, $i, 1);
//check for legal values in current space on board
if (($boardLetter == $wordLetter) ||
($boardLetter == ".")){
$board[$newRow][$newRow - $i] = $wordLetter;
$board[$newCol][$newCol + $i] = $wordLetter;
} else {
$itWorked = FALSE;
} // end if
} // end for loop
break;