# File: example.php

File: example.php Example script text/plain demonstration file included, a working example Path finder Find the shortest path in a map By Kristo Vaher Examples of calculating from previous location (like checkpoints) as well as getting the weights of previously calculated path. 11 years ago 4,367 bytes

## Contents

 ``` With diagonal movement:

';                 \$path=new PathFinder();         \$path->setOrigin(1,1); //This is the origin coordinate, aka 1x1         \$path->setDestination(5,5); //Destination coordinate         \$path->setMap(\$map);         \$result=\$path->returnPath(); //returns an array with coordinates (like 1x2 etc)                 //This is only for display purposes, I will be checking the array keys to see if the square was walked on or not         \$modifiedresult=array_flip(\$result);                 echo '';         for(\$x=1;\$x<=5;\$x++){             echo '
';                     //marking it with an X if it was included in path or if it was starting position                     if(isset(\$modifiedresult[\$x.'x'.\$y]) || \$x.'x'.\$y=='1x1'){                         echo 'x';                     } else {                         echo ' ';                     }                 echo '
';             for(\$y=1;\$y<=5;\$y++){                 echo '';             }             echo '';         }         echo '';         //---             echo '

Without diagonal movement:

';                 \$path=new PathFinder();         \$path->noDiagonalMovement(); //This supresses diagonal movement, only recommended for strict square maps         \$path->setOrigin(1,1); //This is the origin coordinate, aka 1x1         \$path->setDestination(5,5); //Destination coordinate         \$path->setMap(\$map);         \$result=\$path->returnPath(); //returns an array with coordinates (like 1x2 etc)                 //This is only for display purposes, I will be checking the array keys to see if the square was walked on or not         \$modifiedresult=array_flip(\$result);                 echo '';         for(\$x=1;\$x<=5;\$x++){             echo '
';                     //marking it with an X if it was included in path or if it was starting position                     if(isset(\$modifiedresult[\$x.'x'.\$y]) || \$x.'x'.\$y=='1x1'){                         echo 'x';                     } else {                         echo ' ';                     }                 echo '
';             for(\$y=1;\$y<=5;\$y++){                 echo '';             }             echo '';         }         echo '';         //---             echo '

Continuing from previous destination:

';                 //You can also continue calculating a new position after the previous solution, in this case it now continues from the last point         \$path->setDestination(5,2); //Destination coordinate         \$result=\$path->returnPath(); //returns an array with coordinates (like 1x2 etc)                 //This is only for display purposes, I will be checking the array keys to see if the square was walked on or not         \$modifiedresult=array_flip(\$result);                 echo '';         for(\$x=1;\$x<=5;\$x++){             echo '
';                     //marking it with an X if it was included in path or if it was starting position                     if(isset(\$modifiedresult[\$x.'x'.\$y]) || \$x.'x'.\$y=='5x5'){                         echo 'x';                     } else {                         echo ' ';                     }                 echo '
';             for(\$y=1;\$y<=5;\$y++){                 echo '';             }             echo '';         }         echo '';         //---             echo '

Example printout of path (top) and weight (bottom) arrays:

';         echo '
';         print_r(\$result);         print_r(\$result=\$path->returnWeights()); //this only returns the weights of the last-solved path         echo '
';             ?> ```
For more information send a message to `info at phpclasses dot org`.