Beispiel #1
0
<?php

//Automatically load the necessary classes from the classes directory
function __autoload($class_name)
{
    include 'classes/' . $class_name . '.php';
}
//If valid minimum and maximums have been given then use them, if not just use the default 1-9
if (isset($_POST['min']) && isset($_POST['max']) && $_POST['min'] < $_POST['max']) {
    $stb = new ShutTheBox(null, $_POST['min'], $_POST['max']);
} else {
    $stb = new ShutTheBox();
}
//Get the turns, iterate through them and make the moves
if (isset($_POST['dice']) && is_array($_POST['dice'])) {
    $turns = $_POST['dice'];
    //print_r($turns);
    foreach ($turns as $t) {
        $stb->takeTurn($t);
    }
}
//If the root is marked as successful then a root to success has been found
if ($stb->getSuccess()) {
    echo '<h2>Complete</h2>';
    echo '<p>Here are the moves you could have made:</p><hr>';
    $next = $stb->next();
    $min = $stb->min;
    $max = $stb->max;
    //Display the beginning state:
    echo '<div class="tiles">';
    for ($i = $min; $i <= $max; $i++) {
 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.
 }