PHP在开关中组合方向以获得对角线文本

时间:2009-11-11 06:22:17

标签: php variable-assignment

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

我应该能够将两者结合起来(或将对角线文本输出到屏幕上)

但是,当我尝试这个时,它不起作用,我一直在尝试不同的组合 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;

1 个答案:

答案 0 :(得分:2)

运行break;的代码后,switch语句将突破case "E"子句。您需要为组合设置新的显式案例,以使其以这种方式工作。