Announcement

Collapse
No announcement yet.

Very complicated loops

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • jrwap
    replied
    PHP Code:
     for($o=0$o<$number_obstacles$o++) {
        echo
    "    <tr>\n";
        echo
    "      <td width=\"278\">Enter obsticle $o coordinates: </td>\n";
        echo
    "      <td width=\"106\" align=\"right\"><input type=\"text\" name=\"obstacle[$o]['x]\" size=\"3\"> i</td>\n";
        echo
    "      <td width=\"106\" align=\"right\"><input type=\"text\" name=\"obstacle[$o]['y']\" size=\"3\"> j</td>\n";
        echo
    "    </tr>\n";

    One little problem, ['x'] and ['y'] won't work .... I changed it to [x] and [y] and everything works.

    Thats great work!!! Thanks so much!!!

    -JRW

    Leave a comment:


  • Mark Hensler
    replied
    alright, this is my test script...
    PHP Code:
    <?php

    function sort_x($a$b) {
        if (
    $a['x'] == $b['x']) return 0;
        return (
    $a['x'] < $b['x']) ? -1
    }
    function 
    sort_y($a$b) {
        if (
    $a['y'] == $b['y']) return 0;
        return (
    $a['y'] < $b['y']) ? -1
    }

    $i 7;
    $j 3;

    $obstacle = array(
                    array(
    'x' => 3'y' => 2),
                    array(
    'x' => 4'y' => 3),
                    array(
    'x' => 5'y' => 2),
                    array(
    'x' => 6'y' => 1)
                    );

    echo 
    "<html><body><pre>\n";

    // sort first
    usort($obstacle"sort_x");
    usort($obstacle"sort_y");

    print_r ($obstacle);

    for(
    $counter=0$counter<($i+2); $counter++) echo " *";
    echo 
    "\n";

    $z=0;
    for (
    $y=0$y<$j$y++) {
        print 
    " *";
        for (
    $x=0$x<$i$x++) {
            
            if ((
    $y == ($obstacle[$z]['y']-1)) && ($x == ($obstacle[$z]['x']-1))) {
                print 
    " X";
                
    $z++;
            }
            else {
                print 
    " 0";
            }
              
        } 
    //END for column
        
    print " *\n";
    //END for row

    for($counter=0$counter<($i+2); $counter++) echo " *";
    echo 
    "\n";

    echo 
    "</pre></body></html>";

    ?>
    Output:
    Code:
    Array
    (
        [0] => Array
            (
                [x] => 6
                [y] => 1
            )
    
        [1] => Array
            (
                [x] => 3
                [y] => 2
            )
    
        [2] => Array
            (
                [x] => 5
                [y] => 2
            )
    
        [3] => Array
            (
                [x] => 4
                [y] => 3
            )
    
    )
     * * * * * * * * *
     * 0 0 0 0 0 X 0 *
     * 0 0 X 0 X 0 0 *
     * 0 0 0 X 0 0 0 *
     * * * * * * * * *
    You'll need to fill your arrays like this:
    PHP Code:
    for($o=0$o<$number_obstacles$o++) {
        echo
    "    <tr>\n";
        echo
    "      <td width=\"278\">Enter obsticle $o coordinates: </td>\n";
        echo
    "      <td width=\"106\" align=\"right\"><input type=\"text\" name=\"obstacle[$o]['x]\" size=\"3\"> i</td>\n";
        echo
    "      <td width=\"106\" align=\"right\"><input type=\"text\" name=\"obstacle[$o]['y']\" size=\"3\"> j</td>\n";
        echo
    "    </tr>\n";

    Leave a comment:


  • jrwap
    replied
    Here is some updated code. The grid is now defined by a 2-dimensional array.

    PHP Code:
     <?php
    // ######################################## STEP 1 #
    if ((!$step) || ($step==1)) {   
        global 
    $grid_length$grid_width$number_obsticals$grid_name$PHP_SELF;
        
    ?>
        Welcome to the A.I. Path Planner.&nbsp;&nbsp;<br>
    Step 1 - Grid Setup<br>
    &nbsp;&nbsp;&nbsp; Please fill out the form below.&nbsp; The grid name will
    be used to pull up the project at a later date.<br>&nbsp;&nbsp;&nbsp; The grid
    length and width is measured in number of blocks and will determine the area of
    the&nbsp;<br>&nbsp;&nbsp;&nbsp; grid.&nbsp; The number of obstacles will be used
    in a future step, but it refers to the number of&nbsp;<br>&nbsp;&nbsp;&nbsp;
    blocks that will be taken up by obstacles.<br>

    <form method="GET" action="setup.php">
    <input type="hidden" name="step" value="2">
      <table border="0" width="500" cellspacing="1" cellpadding="0">
        <tr>
          <td width="278">Enter grid width (in number of blocks):&nbsp;</td>
          <td width="212"><input type="text" name="i" size="3"> blocks</td>
        </tr>
        <tr>
          <td width="278">Enter grid height (in number of blocks):&nbsp;</td>
          <td width="212"><input type="text" name="j" size="3"> blocks</td>
        </tr>
        <tr>
          <td width="278">Enter number of obstacles: </td>
          <td width="212"><input type="text" name="number_obstacles" size="3"> obstacles</td>
        </tr>
        <tr>
          <td width="278"><font size="1">&nbsp;</font></td>
          <td width="212"><font size="1">&nbsp;</font></td>
        </tr>
        <tr>
          <td width="278"></td>
          <td width="212"><input type="submit" value="Step 2"><input type="reset" value="Reset"></td>
        </tr>
      </table>
    </form>
    <?php
    }
    // ######################################## STEP 2 #line53
    if ($step==2) {  
    if ((!
    $i) || (!$j) || (!$number_obstacles))  {
    echo 
    "You have not filled out every field.  Please <a href='http://worldclassdesigns.com/AI/setup.php?step=1'>GO BACK</a> and fill out all the fields.";
    } else {
    ?>
    <table border="0" width="500" cellspacing="1" cellpadding="0">
         <tr>
          <td width="278">Grid width (in number of blocks):&nbsp;</td>
          <td width="212"><? echo "$i"?> blocks</td>
        </tr>
        <tr>
          <td width="278">Grid height (in number of blocks):&nbsp;</td>
          <td width="212"><? echo "$j"?> blocks</td>
        </tr>
        <tr>
          <td width="278">Number of obstacles: </td>
          <td width="212"><? echo "$number_obstacles"?> obstacles</td>
        </tr>
        <tr>
          <td width="278"><font size="1">&nbsp;</font></td>
          <td width="212"><font size="1">&nbsp;</font></td>
        </tr>
      </table>
    <?
    }
    $it $i 2;
    $jt $j 2;
    // print grid array
    for ($y=0$y<$jt$y++) { 
       for (
    $x=0$x<$it$x++) { 
       
    $grid_array[$it][$jt]="0";
       }
    }
    //print grid array
    for ($x=0$x<$it$x++)
       {
       echo 
    $grid_array[$x][0]=" * ";
       }
    echo 
    "<br>";
    for (
    $y=0$y<$j$y++)
       {
       echo 
    $grid_array[0][$j]=" * ";
       for (
    $x=0$x<$i$x++)
          {
          echo 
    $grid_array[$x][$y]=" 0 ";
          }
       echo 
    $grid_array[$it][$j]=" * ";
       echo 
    "<br>";
    }
    for (
    $x=0$x<$it$x++)
       {
       echo 
    $grid_array[$x][$it]=" * ";
       }
    // end print grid array
    echo "<br><br>Please enter the coordinates of the obsticles in the i / j format.<br>";
    ?>
    <form method="GET" action="setup.php">
    <input type="hidden" name="step" value="3">
    <input type="hidden" name="number_obstacles" value="<? echo "$number_obstacles?>">
    <input type="hidden" name="i" value="<? echo "$i?>">
    <input type="hidden" name="j" value="<? echo "$j?>">
    <table border="0" width="500" cellspacing="1" cellpadding="0">
    <tr>
    <td width="278">Enter starting point coordinates: </td>
    <td width="106" align="right"><input type="text" name="start_i" size="3"> i</td>
    <td width="106" align="right"><input type="text" name="start_j>" size="3"> j</td>
    </tr>
    <tr>
    <td width="278">Enter ending point coordinates: </td>
    <td width="106" align="right"><input type="text" name="end_i" size="3"> i</td>
    <td width="106" align="right"><input type="text" name="end_j" size="3"> j</td>
    </tr>
    <?
    for($o=0$o<$number_obstacles$o++) 

    echo
    " <tr>\n"
    echo
    " <td width=\"278\">Enter obsticle <?=$o?> coordinates: </td>\n"
    echo
    " <td width=\"106\" align=\"right\"><input type=\"text\" name=\"obstacle_i[$o]\" size=\"3\"> i</td>\n"
    echo
    " <td width=\"106\" align=\"right\"><input type=\"text\" name=\"obstacle_j[$o]\" size=\"3\"> j</td>\n"
    echo
    " </tr>\n"

    /*
    for($o=0; $o<$number_obstacles; $o++)
       {
        ?>
          <tr>
          <td width="278">Enter obsticle <?=$o?> coordinates: </td>
          <td width="106" align="right"><input type="text" name="obstacle_i<?=$o?>" size="3"> i</td>
        <td width="106" align="right"><input type="text" name="obstacle_j<?=$o?>" size="3"> j</td>
          </tr>
        <?
       }
    */
    ?>
         <tr>
           <td width="278"><font size="1">&nbsp;</font></td>
           <td width="106"><font size="1">&nbsp;</font></td>
         <td width="106"><font size="1">&nbsp;</font></td>
           </tr>
             <tr>
            <td width="500" colspan="3">
            <p align="right"><input type="submit" value="Step 3"><input type="reset" value="Reset"></p>
            </td>
      </table>
    </form>
    <?
    }
    // ######################################## STEP 3 #
    if ($step==3) {  
    /*
    $it = $i + 2;
    $jt = $j + 2;
    $total_blocks = $it * $jt;
    for($counter=1; $counter<=$total_blocks; counter++)
    {
       $block_number . $counter = R;
    }

    echo "$block_number6';
    */
    // sort the array first 
    asort($obstacle_j); 

    for (
    $y=0$y<$j$y++) { 
    for (
    $x=0$x<$i$x++) { 

    if ((
    $y == current($obstacle_j)) && ($x == $obstacle_i[key(current($obstacle_j))])) { 
    print 
    " *"
    array_shift($obstacle_j); 

    else { 
    print 
    " 0"


    //END for column 
    //END for row

    }
    ?>
    Still can't figure out that error in the code you posted.

    Thanks,

    -JRW

    Leave a comment:


  • jrwap
    replied
    The error is: Warning: Variable passed to key() is not an array or object in /home/worldcla/public_html/AI/setup.php on line 177

    I am going to chane it so that the grid is put into a 2-dimensional array like $grid_array[$i][$j];

    I tried looking up current and key functons to see what was causing the error, but I couldn't understand it very well.

    Thanks again,

    -JRW


    You can try it with your code at: http://worldclassdesigns.com/AI/setup.php

    Leave a comment:


  • Mark Hensler
    replied
    What are the errors?

    Leave a comment:


  • jrwap
    replied
    I pasted the code in... you can try it but Im getting some errors.

    Thanks you are really great for helping me.

    -JRW

    Leave a comment:


  • Mark Hensler
    replied
    untested

    load the inputs into arrays, it's easier to handle
    PHP Code:
    for($o=0; $o<$number_obstacles; $o++)
       {
        echo"    <tr>\n";
        echo"      <td width=\"278\">Enter obsticle <?=$o?> coordinates: </td>\n";
        echo"      <td width=\"106\" align=\"right\"><input type=\"text\" name=\"obstacle_i[$o]\" size=\"3\"> i</td>\n";
        echo"      <td width=\"106\" align=\"right\"><input type=\"text\" name=\"obstacle_j[$o]\" size=\"3\"> j</td>\n";
        echo"    </tr>\n";

       }
    then, to print...
    PHP Code:
    // sort the array first
    asort($obstacle_j);

    for (
    $y=0$y<$j$y++) {
        for (
    $x=0$x<$i$x++) {
            
            if ((
    $y == current($obstacle_j)) && ($x == $obstacle_i[key(current($obstacle_j))])) {
                print 
    " *";
                
    array_shift($obstacle_j);
            }
            else {
                print 
    " 0";
            }
            
        } 
    //END for column
    //END for row 

    Leave a comment:


  • jrwap
    started a topic Very complicated loops

    Very complicated loops

    I wonder if anyone could help me with this little problem. I am creating a grid based on some user defined dimentions. I have that working just fine. Now, I want the use to be able to enter points one the grid and have them show up... this is where I am getting hung up.

    An example can be seen here in the design stages: http://worldclassdesigns.com/AI/setup.php

    Example:
    If the user has entered the following info:
    Grid width (in number of blocks): 7 blocks
    Grid height (in number of blocks): 3 blocks

    it would print out like this.
    * * * * * * * * *
    * 0 0 0 0 0 0 0 *
    * 0 0 0 0 0 0 0 *
    * 0 0 0 0 0 0 0 *
    * * * * * * * * *
    This is a 7 by 3 grid with a surrounding box of *'s I will replace the 0's and #'s with pictures when this is completed.

    now the user can enter as many places to put *'s as they want. So it might look like this:
    * * * * * * * * *
    * 0 0 0 0 0 0 0 *
    * 0 0 0 * * 0 0 *
    * 0 0 0 0 0 0 0 *
    * * * * * * * * *
    The user has entered 2 stars at the locations:
    1st * i=4 and j=2
    2nd* i=5 and j=2

    This is where Im having the trouble. I need the code to print out the grid with the *'s in the selected areas.

    Code:
    PHP Code:
     <?php
    // ######################################## STEP 1 #
    if ((!$step) || ($step==1)) {   
        global 
    $grid_length$grid_width$number_obsticals$grid_name$PHP_SELF;
        
    ?>
        Welcome to the A.I. Path Planner.&nbsp;&nbsp;<br>
    Step 1 - Grid Setup<br>
    &nbsp;&nbsp;&nbsp; Please fill out the form below.&nbsp; The grid name will
    be used to pull up the project at a later date.<br>&nbsp;&nbsp;&nbsp; The grid
    length and width is measured in number of blocks and will determine the area of
    the&nbsp;<br>&nbsp;&nbsp;&nbsp; grid.&nbsp; The number of obstacles will be used
    in a future step, but it refers to the number of&nbsp;<br>&nbsp;&nbsp;&nbsp;
    blocks that will be taken up by obstacles.<br>

    <form method="GET" action="setup.php">
    <input type="hidden" name="step" value="2">
      <table border="0" width="500" cellspacing="1" cellpadding="0">
        <tr>
          <td width="278">Enter grid width (in number of blocks):&nbsp;</td>
          <td width="212"><input type="text" name="i" size="3"> blocks</td>
        </tr>
        <tr>
          <td width="278">Enter grid height (in number of blocks):&nbsp;</td>
          <td width="212"><input type="text" name="j" size="3"> blocks</td>
        </tr>
        <tr>
          <td width="278">Enter number of obstacles: </td>
          <td width="212"><input type="text" name="number_obstacles" size="3"> obstacles</td>
        </tr>
        <tr>
          <td width="278"><font size="1">&nbsp;</font></td>
          <td width="212"><font size="1">&nbsp;</font></td>
        </tr>
        <tr>
          <td width="278"></td>
          <td width="212"><input type="submit" value="Step 2"><input type="reset" value="Reset"></td>
        </tr>
      </table>
    </form>
    <?php
    }
    // ######################################## STEP 2 #line53
    if ($step==2) {  
    if ((!
    $i) || (!$j) || (!$number_obstacles))  {
    echo 
    "You have not filled out every field.  Please <a href='http://worldclassdesigns.com/AI/setup.php?step=1'>GO BACK</a> and fill out all the fields.";
    } else {
    ?>
    <table border="0" width="500" cellspacing="1" cellpadding="0">
         <tr>
          <td width="278">Grid width (in number of blocks):&nbsp;</td>
          <td width="212"><? echo "$i"?> blocks</td>
        </tr>
        <tr>
          <td width="278">Grid height (in number of blocks):&nbsp;</td>
          <td width="212"><? echo "$j"?> blocks</td>
        </tr>
        <tr>
          <td width="278">Number of obstacles: </td>
          <td width="212"><? echo "$number_obstacles"?> obstacles</td>
        </tr>
        <tr>
          <td width="278"><font size="1">&nbsp;</font></td>
          <td width="212"><font size="1">&nbsp;</font></td>
        </tr>
      </table>
    <?
    }
    echo 
    "The grid will look like this:<br>";
    // create grid
    $it $i 2;
    $jt $j 2;
    for(
    $counter=0$counter<$it$counter++)
    {
    echo 
    " * ";
    }
    echo 
    "<br>";
    $temp 0;
    while(
    $temp != $j)  {
       echo 
    " * ";
       for(
    $counter=0$counter<$i$counter++)
       {
          echo 
    "0";
       }
       echo 
    " * <br>";
       
    $temp++;
    }
    for(
    $counter=0$counter<$it$counter++)
    {
    echo 
    " * ";
    }
    echo 
    "<br><br>Please enter the coordinates of the obsticles in the i / j format.<br>";
    ?>
    <form method="GET" action="setup.php">
    <input type="hidden" name="step" value="3">
    <input type="hidden" name="number_obstacles" value="<? echo "$number_obstacles?>">
    <input type="hidden" name="i" value="<? echo "$i?>">
    <input type="hidden" name="j" value="<? echo "$j?>">
    <table border="0" width="500" cellspacing="1" cellpadding="0">
    <tr>
    <td width="278">Enter starting point coordinates: </td>
    <td width="106" align="right"><input type="text" name="start_i" size="3"> i</td>
    <td width="106" align="right"><input type="text" name="start_j>" size="3"> j</td>
    </tr>
    <tr>
    <td width="278">Enter ending point coordinates: </td>
    <td width="106" align="right"><input type="text" name="end_i" size="3"> i</td>
    <td width="106" align="right"><input type="text" name="end_j" size="3"> j</td>
    </tr>
    <?
    for($o=0$o<$number_obstacles$o++)
       {
        
    ?>
          <tr>
          <td width="278">Enter obsticle <?=$o?> coordinates: </td>
          <td width="106" align="right"><input type="text" name="obstacle_i<?=$o?>" size="3"> i</td>
        <td width="106" align="right"><input type="text" name="obstacle_j<?=$o?>" size="3"> j</td>
          </tr>
        <?
       
    }
    ?>
         <tr>
           <td width="278"><font size="1">&nbsp;</font></td>
           <td width="106"><font size="1">&nbsp;</font></td>
         <td width="106"><font size="1">&nbsp;</font></td>
           </tr>
             <tr>
            <td width="500" colspan="3">
            <p align="right"><input type="submit" value="Step 3"><input type="reset" value="Reset"></p>
            </td>
      </table>
    </form>
    <?
    }
    // ######################################## STEP 3 #
    if ($step==3) {  
    //create grid with start, end, and obstacles.
    $it $i 2;
    $jt $j 2;
    //create top border
    for($counter=0$counter<$it$counter++)
    {
    echo 
    " * ";
    }
    echo 
    "<br>";
    //create middle sections of grid
    $temp 0;
    while(
    $temp != $j)  {
       echo 
    " * ";
       if(
       for(
    $counter=0$counter<$i$counter++)
       {
        for($
    0_counter=0$o_counter<$number_obstacles$o_counter++)
        {
           
    $present_obstacle "obstacle_i" $o_counter;
           if(
    $present_obstacle       
                  
    echo "0";
       }
       echo 
    " * <br>";
       
    $temp++;
    }


    }
    ?>
    I am currently working ont he bottom so yes, there are problems with it. Part one asks the user for the grid dimensions and number of obstacles. Part two asks for the locations of each obstacle. And I need part three to print the grid with the obstacles int eh correct place.

    Please please please help,

    -JRW
widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
Working...
X