function fight($simulation)
{
    $Clocation = array();
    foreach ($simulation as $location => $key) {
        if ($key['type'] == 'C') {
            $Clocation[] = $location;
        }
    }
    foreach ($Clocation as $carnivore) {
        if (hasRightNeighbour($carnivore) && rightNeighbour($simulation, $carnivore) == "C" && $simulation[$carnivore]['life'] != $simulation[$carnivore + 1]['life'] && !$_SESSION["action"][$carnivore]) {
            if ($simulation[$carnivore]['life'] > $simulation[$carnivore + 1]['life']) {
                $simulation[$carnivore]['life'] += $simulation[$carnivore + 1]['life'];
                $simulation[$carnivore + 1] = null;
                $_SESSION["action"][$carnivore] = true;
            } else {
                $simulation[$carnivore + 1]['life'] += $simulation[$carnivore]['life'];
                $simulation[$carnivore] = null;
                $_SESSION["action"][$carnivore + 1] = true;
            }
        }
    }
    return $simulation;
}
function move($simulation)
{
    $Clocation = array();
    $Elocation = array();
    foreach ($simulation as $location => $organism) {
        if ($organism['type'] == 'C') {
            $Clocation[] = $location;
        }
        if ($organism == null) {
            $Elocation[] = $location;
        }
    }
    foreach ($Clocation as $carnivore) {
        if (hasRightNeighbour($carnivore) && rightNeighbour($simulation, $carnivore) == null && !$_SESSION["action"][$carnivore]) {
            $moveCandidates = array($carnivore - $_SESSION["dimension"], $carnivore + $_SESSION["dimension"], $carnivore - 1, $carnivore + 1);
            do {
                $moveloc = array_rand($moveCandidates, 1);
                $moveloc = $moveCandidates[$moveloc];
            } while (!in_array($moveloc, $Elocation));
            $simulation[$moveloc] = $simulation[$carnivore];
            $Clocation[$carnivore] = $moveloc;
            $_SESSION["action"][$moveloc] = true;
            $simulation[$carnivore] = null;
            //            $Elocation[$moveloc] = $carnivore;
            $Elocation[] = $carnivore;
            $Elocation = array_diff($Elocation, array($moveloc));
            $Elocation = array_values($Elocation);
        }
    }
    $Elocation = array();
    $Hlocation = array();
    foreach ($simulation as $location => $organism) {
        if ($organism == null) {
            $Elocation[] = $location;
        }
        if ($organism['type'] == 'H') {
            $Hlocation[] = $location;
        }
    }
    foreach ($Hlocation as $herbivore) {
        if (hasRightNeighbour($herbivore) && rightNeighbour($simulation, $herbivore) == null && !$_SESSION["action"][$herbivore]) {
            $moveCandidates = array($herbivore - $_SESSION["dimension"], $herbivore + $_SESSION["dimension"], $herbivore - 1, $herbivore + 1);
            do {
                $moveloc = array_rand($moveCandidates, 1);
                $moveloc = $moveCandidates[$moveloc];
            } while (!in_array($moveloc, $Elocation));
            $simulation[$moveloc] = $simulation[$herbivore];
            $Hlocation[$herbivore] = $moveloc;
            $_SESSION["action"][$moveloc] = true;
            $simulation[$herbivore] = null;
            $Elocation[] = $herbivore;
            $Elocation = array_diff($Elocation, array($moveloc));
            $Elocation = array_values($Elocation);
        }
    }
    unset($_SESSION["action"]);
    return $simulation;
}
Beispiel #3
0
function move($simulation)
{
    $Clocation = OrganismLocations($simulation, 'C');
    $Elocation = OrganismLocations($simulation, '');
    foreach ($Clocation as $carnivore) {
        if (hasRightNeighbour($carnivore) && rightNeighbour($simulation, $carnivore) == null && !$_SESSION["action"][$carnivore]) {
            $moveCandidates = array($carnivore - $_SESSION["dimension"], $carnivore + $_SESSION["dimension"], $carnivore + 1);
            if ($carnivore % $_SESSION["dimension"] != 0) {
                $moveCandidates[] = $carnivore - 1;
            }
            do {
                $moveloc = array_rand($moveCandidates, 1);
                $moveloc = $moveCandidates[$moveloc];
            } while (!in_array($moveloc, $Elocation));
            $simulation[$moveloc] = $simulation[$carnivore];
            $Clocation[$carnivore] = $moveloc;
            $_SESSION["action"][$moveloc] = true;
            $simulation[$carnivore] = null;
            $Elocation[] = $carnivore;
            $Elocation = array_diff($Elocation, array($moveloc));
        }
    }
    $Elocation = OrganismLocations($simulation, '');
    $Hlocation = OrganismLocations($simulation, 'H');
    foreach ($Hlocation as $herbivore) {
        if (hasRightNeighbour($herbivore) && rightNeighbour($simulation, $herbivore) == null && !$_SESSION["action"][$herbivore]) {
            $moveCandidates = array($herbivore - $_SESSION["dimension"], $herbivore + $_SESSION["dimension"], $herbivore + 1);
            if ($herbivore % $_SESSION["dimension"] != 0) {
                $moveCandidates[] = $herbivore - 1;
            }
            do {
                $moveloc = array_rand($moveCandidates, 1);
                $moveloc = $moveCandidates[$moveloc];
            } while (!in_array($moveloc, $Elocation));
            $simulation[$moveloc] = $simulation[$herbivore];
            $Hlocation[$herbivore] = $moveloc;
            $_SESSION["action"][$moveloc] = true;
            $simulation[$herbivore] = null;
            $Elocation[] = $herbivore;
            $Elocation = array_diff($Elocation, array($moveloc));
        }
    }
    unset($_SESSION["action"]);
    return $simulation;
}