Link to home
Start Free TrialLog in
Avatar of Chris Stormer
Chris Stormer

asked on

Finding Neighbor "cells"

Below is my code.  What I need to do is check each value in the array and see if that cell has exactly three neighboring cells that are also checked.  If that is the case then I need to keep this cell checked and move to the next value in the array and run the same test.  I've got the loop that shows me the value of each value in the array and it loops through.  I'm having a hard time figuring out the if statement to check to see if #1 the current cell is checked, and # if exactly three neighboring cells are also checked....
<?
$max_rows = 9;
$max_cols = 9;
 
 
if (isset($_POST['submit'])) {
  // $_POST['cb'] contains the array with selected checkboxes.
  $checked = $_POST['cb'];
        for ($y = 0; $y < $max_rows; $y++) {
                  for ($x = 0; $x < $max_cols; $x++) {
                                
                                // code modification here
                                echo ($y+1).",".($x+1)." is ";
                                echo ($checked[$y][$x] === 'check')?"checked<br>":"unchecked<br>";
                                
                                }
                  }
        }
 
?>
<form method='post'>
<?
 
for ($y = 0; $y < $max_rows; $y++) {
  for ($x = 0; $x < $max_cols; $x++) {
    echo "<input type='checkbox' name='cb[" . $y . "][" . $x. "]' value='check' ";
    if (isset($checked[$y][$x])) {
      echo "checked ";
    }
    echo "/>\n";
  }
  echo "<br />";
}
?>
 
<input type="submit" name="submit" />
</form>

Open in new window

Avatar of BrianGEFF719
BrianGEFF719
Flag of United States of America image

this is untested, but should get you on the right track

for($x=0;$x < $MAX;$x++)
{
 for($y=0;$y < $Y_MAX;$y++)
 {
     if($arr[$x][$y] == checked && 
              (
                    @$arr[$x - 1][$y] == checked ||
                    @$arr[$x + 1][$y] == checked ||
                    @$arr[$x][$y - 1] == checked ||
                    @$arr[$x][$y + 1] == checked
 
              )
         //ok...go ahead
                     
 }
}
Avatar of Chris Stormer
Chris Stormer

ASKER

The problem with the above is that I think each cell has a possible 8 neighbors.

Top left, Top, Top Right, left, Right, Bottom Left, Bottom, Bottom Right

Also I can only move on if "three" of them are checked.. not all of them.  So I need a way count how many are checked and if it is more than 3, I actually uncheck the cell and if it is less than 3 I uncheck the cell... if it is exactly 3 I move on.
Avatar of Steve Bink
In that case, you know that for cell x/y, you need to check the following:

(x-1)/(y-1)   (x)/(y-1)   (x+1)/(y-1)
(x-1)/(y)      no check   (x+1)/(y)
(x-1)/(y+1)  (x)/(y+1)  (x+1)/(y+1)

Using BrianGEFF719's code as a starting point:
for($x=0;$x < $MAX;$x++) {
  for($y=0;$y < $Y_MAX;$y++) {
    if ($arr[$x][$y] == checked) {
      $three_checks=0;
      for ($a=($x-1);$a<($x+2);$a++) { // $a will be the $x
        for ($b=($y-1);$b<($y+2);$b++) { // $b will be the $y
          if ($arr[$a][$b] == checked) { $three_checks++; }
        }
      }
      if $three_checks!=4) { 
        //uncheck the $arr[$x][$y]
        // use 4 because $arr[$x][$y] will be checked and included in the count
      }
    }
  }
}

Open in new window

Something that has not been mentioned is the effect of making updates to the array. If you change a value in the array because of the neighbouring cells then you can affect later tests. For example, consider the following rows (checked are shown as "x" and unchecked as "-") in the attached snippet. Consider the cell marked as 'o' which clearly has 3 neighbours and should be checked, but if you check it then the cell underneath marked '?' will be checked when you do it because it NOW has 3 neighbours.

What you need is two arrays - one for holding the values to check and an indentically sized one for setting/unsetting the checked values.


- - - x - - - 
- - - o - - -
- - x ? x - -

Open in new window

Yes Bport,

Your 100% right... I was trying to figure out how I was going to deal with that issue... because I'm showing the revised grid on the screen after every cell is checked.
You could take a count of which cells fail, then reset them after all the tests are done.  In that case, create a second array to mirror the $arr data.

Still, the recursive nature of your test will be problematic for you.  Can you explain a little more about what you're trying to do?  Maybe we can come up with a better strategy for you.
I'm trying to duplicate this:

http://www.bitstorm.org/gameoflife/

using php and html checkboxes for the cells...
ASKER CERTIFIED SOLUTION
Avatar of Steve Bink
Steve Bink
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial