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 $neighb开发者_高级运维ors 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 ?
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
.
精彩评论