1

this is my first question here and i hope you can help me ..

I am trying to find a soloution of the towers of hanoi problem by three search ways (BFS-DFS-IDS) so I use "state" class whitch defined by 5 variables as here :

class state {

var $tower1 = array();
var $tower2 = array();
var $tower3 = array();
var $depth;
var $neighbors = array();

and it also has many function one of them is getneighbors() which supposed to fill the array $neighbors with state neighbors and they are from the type "state"

and here is the function :

function getneighbors ()

{

    $temp=$this->copy();

    $neighbor1= $this->copy();

    $neighbor2= $this->copy();

    $neighbor3= $this->copy();

    $neighbor4= $this->copy();

    $neighbor5= $this->copy();

    $neighbor6= $this->copy();

    if(!Empty($temp->tower1))
    {

         if(!Empty($neighbor1->tower2))
         {
            if(end($neighbor1->tower1) < end($neighbor1->tower2))
            {
                array_unshift($neighbor1->tower2,array_pop($neighbor1->tower1));
                array_push($neighbors,$neighbor1);
         }}
         else
             {
         array_unshift($neighbor1->tower2, array_pop($neighbor1->tower1));             
          array_push($neighbors,$neighbor1);
             }



        if(!Empty($neighbor2->tower3))
         {
            if(end($neighbor2->tower1) < end($neighbor2->tower3))
            { array_unshift($neighbor2->tower3, array_pop($neighbor2->tower1));
              array_push($neighbors,$neighbor2);
        }}


        else
            {
         array_unshift($neighbor2->tower3,array_shift($neighbor2->tower1));
         array_push($neighbors,$neighbor2);
            }

        }
               if(!Empty($temp->tower2))
    {


         if(!Empty($neighbor3->tower1))
         {

           if(end($neighbor3->tower2) < end($neighbor3->tower1))
           {   array_unshift($neighbor3->tower1,array_shift($neighbor3->tower2));
               array_push($neighbors,$neighbor3);
         }

         }
             else
             {
         array_unshift($neighbor3->tower1,array_shift($neighbor3->tower2));
         array_push($neighbors,$neighbor3);

         }



        if(!Empty($neighbor4->tower3))
         {
            if(end($neighbor4->tower2) < end($neighbor4->tower3))
            { array_unshift($neighbor4->tower1,array_shift($neighbor4->tower2));
              array_push($neighbors,$neighbor4);
            }
        }
            else{
         array_unshift($neighbor4->tower3,array_shift($neighbor4->tower2));
            array_push($neighbors,$neighbor4);
         }

        }
          if(!Empty($temp->tower3))
    {


         if(!Empty($neighbor5->tower1))
         {
            if(end($neighbor5->tower3) < end($neighbor5->tower1))
            {array_unshift($neighbor5->tower1,array_shift($neighbor5->tower3));
             array_push($neighbors,$neighbor5);
            }
         }
             else{
         array_unshift($neighbor5->tower1,array_shift($neighbor5->tower3));
         array_push($neighbors,$neighbor5);}



        if(!Empty($neighbor6->tower2))
         {
            if(end($neighbor6->tower3) < end($neighbor6->tower2))
            {  array_unshift($neighbor6->tower2,array_shift($neighbor6->tower3));
             array_push($neighbors,$neighbor6);
        }}
            else{
         array_unshift($neighbor6->tower2,array_shift($neighbor6->tower3));
         array_push($neighbors,$neighbor6);}

        }
        return $neighbors;
}

note that toString and equals and copy are defined too

now the problem is that when I call getneighbors() it returns an empty $neighbors array

can you pleas tell me the problem ?

1
  • It would be helpful to see the larger declaration. getneighbors() is in a class I take it? What class? WHere are the above 5 variables defined? Are they class variables or globals? Edit: i see the class {} above, missed it. Commented May 30, 2010 at 15:32

1 Answer 1

3

You have a scoping issue. $neighbors is a class variable and not a PHP global. Everywhere you refer to $neighbors change it to $this->neighbors.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you but the output still the same : ( any body can help ?
Can you update the class in your posting so we can see the updated code? Please post the class verbatim. I'm not sure if it's 'allowed' with SO but maybe using a pastie service (pastie.org) would be better?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.