2009-11-11 3 views
0

[Disclaimer: Je suis nouveau à PHP, et je suis en train d'apprendre, alors s'il vous plaît pas flamers, il entrave vraiment le processus d'apprentissage lorsque l'on cherche des solutions ou des informations, merci , et le code fonctionne très bien en termes de jeu de mots croisés, il est vraiment me déconcertant comment on obtient une orientation diagonale avec les informations données, ou ce que je fais de mal et de ne pas voir]PHP combinant les directions dans le commutateur pour obtenir le texte diagonal

Étant donné un interrupteur:

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; 

ET:

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; 

Je devrais être en mesure de combiner les deux pour obtenir NE (ou texte en diagonale étant sortie sur l'écran)

Cependant, quand j'essaie cela, il ne fonctionne pas, et j'ai essayé différentes combinaisons de N et E pour obtenir NE ou une orientation NE diagonale sans chance, que donne-t-on?

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

Répondre

2

La déclaration break; éclatera de la clause switch après l'exécution du code pour case "E". Vous devrez configurer de nouveaux cas explicites pour que les combinaisons fonctionnent de cette façon.

Questions connexes