PHP结合开关中的方向来获取对角线文本(PHP combining directions in switch to get diagonal text)

编程入门 行业动态 更新时间:2024-10-24 02:38:53
PHP结合开关中的方向来获取对角线文本(PHP combining directions in switch to get diagonal text)

[免责声明:我是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;

[Disclaimer: I am new to PHP, and I am just learning, so please no flamers, it really hinders the learning process when one is trying to find solutions or information, thank you, and the code works fine in terms of the crossword puzzle, it's just really baffling me how one gets a diagonal orientation with the given information, or what I am doing wrong and not seeing?]

Given a switch:

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;

AND:

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;

I should be able to combine the two to get NE (or diagonal text being outputted onto the screen)

HOWEVER, when I try this, it's not working, and I have been trying different combinations of N and E to get NE or a diagonal NE orientation with no luck, what gives?

#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;

最满意答案

break; 语句将在运行case "E"跳出switch子句。 您需要为这些组合设置新的明确情况,以使其以这种方式工作。

The break; statement will break out of the switch clause after running the code for case "E". You'll need to set up new, explicit cases for the combinations to make it work that way.

更多推荐

本文发布于:2023-07-15 18:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1117519.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对角线   文本   PHP   方向来   combining

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!