Solved

Finding Neighbor "cells"

Posted on 2008-10-18
11
317 Views
Last Modified: 2013-12-13
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

0
Comment
Question by:cstormer
  • 3
  • 3
  • 2
  • +1
11 Comments
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 22751485
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
                     
 }
}
0
 

Author Comment

by:cstormer
ID: 22752431
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.
0
 
LVL 50

Expert Comment

by:Steve Bink
ID: 22752691
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

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 22753656
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

0
 

Author Comment

by:cstormer
ID: 22753670
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.
0
 
LVL 50

Expert Comment

by:Steve Bink
ID: 22753985
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.
0
 

Author Comment

by:cstormer
ID: 22754184
I'm trying to duplicate this:

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

using php and html checkboxes for the cells...
0
 
LVL 50

Accepted Solution

by:
Steve Bink earned 250 total points
ID: 22754649
That's exactly what I would do then.  Create a second array ($arr2) to hold the results of your testing.  Once all of the tests are complete, the second array becomes the real-time data.  Repeat from step 1.
0
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 250 total points
ID: 22755921
Try something along the outline shown in the attached snippet. I have not put your code in but the outline gives an idea of how to manage the arrays and so forth.
<?php
 
// Clear the array elements
//
function clearArray( & $arr ) {
    // some loops to set every array value to "unchecked"
}
 
 
// Load the initial configuration
//
function loadConfig( & $arr ) {
     // Code to do the loading
}
 
 
// Check for neighbours
//
function check( & $liveArray, & $resultsArray ) {
     //
     // Code here runs through the LIVE array and
     // checks for neighbours. The results are set
     // in the RESULTS array     
}
 
 
function showResults( $arr ) {
     // Code to "print" the array out
}
 
 
 
// ===== Main program =====
//
 
$arr1 = array();
$arr2 = array();
 
clearArray( $arr1 );
clearArray( $arr2 );
 
loadConfig( $arr1 );
 
// Do 10 generations
//
for ( $i=0; $i < 5; $i++ ) {
 
     showResults( $arr1 );
     check( $arr1, $arr2 );
     clearArray( $arr1 );
 
     showResults( $arr2 );
     check( $arr2, $arr1 );
     clearArray( $arr2 );
 
}
 
?>
 
If your "check" code does both set and unset then you could skip the "clearArray"s in the loop.
If it only sets values then you need to clear the arrays as shown

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How do uses indexes to maximize MySQL Searches 14 55
Doing something wrong with this PDO Select Statement... 3 18
Row insertion failed. Array 5 48
issue with DB import 1 18
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question