if ($this->d1 != 1 && !isset($this->visited[join(",", array($this->d1 - 1, $this->d2))])) { array_push($x, array($this->d1 - 1, $this->d2)); } if ($this->d1 != 4 && !isset($this->visited[join(",", array($this->d1 + 1, $this->d2))])) { array_push($x, array($this->d1 + 1, $this->d2)); } if ($this->d2 != 1 && !isset($this->visited[join(",", array($this->d1, $this->d2 - 1))])) { array_push($x, array($this->d1, $this->d2 - 1)); } if ($this->d2 != 4 && !isset($this->visited[join(",", array($this->d1, $this->d2 + 1))])) { array_push($x, array($this->d1, $this->d2 + 1)); } $l = count($x); $new_point_array = array(0, 0); //Default is no choice if ($l > 0) { //If the length of X is > 0, we have choices. shuffle($x); $new_point_array = $x[array_rand($x, 1)]; $this->d1 = $new_point_array[0]; $this->d2 = $new_point_array[1]; $new_point_array_as_string = join(",", $new_point_array); $this->visited[$new_point_array_as_string] = true; $this->path .= $new_point_array_as_string . " "; } return $new_point_array; } } $g = new GridHelper(); print $g->seek() . "\n"; exit;