public function takeTurn($roll) { if ($this->isValid() && !$this->isParentComplete()) { //echo "Taking turn: $roll"; if (is_null($this->children)) { //If no children then make children and take the turns $this->children = array(); //echo "Roll: $roll"; $numbers = new Numbers($roll); $numbers->spawn(); $permutations = $numbers->getChildren(); //Loop through all the permutations and take the turns foreach ($permutations as $p) { $child = new ShutTheBox($this); $child->turn = $p; $toDrop = $p->values; foreach ($toDrop as $d) { $child->dropNumber($d); } $this->children[] = $child; if ($child->isComplete()) { $child->setSuccess(); } } } else { //If has children then make them take the turn foreach ($this->children as $c) { $c->takeTurn($roll); } } } //If not valid then no point in taking the turn. }