Exemple #1
0
                    $closest_distance = $dist;
                    $closest_target = array($tRow, $tCol);
                }
            }
            if ($closest_target === null) {
                # no target found, mark ant as not moving so we don't run into it
                $destinations[] = $ant;
                continue;
            }
            $directions = $ants->direction($aRow, $aCol, $closest_target[0], $closest_target[1]);
            shuffle($directions);
            foreach ($directions as $direction) {
                list($nRow, $nCol) = $ants->destination($aRow, $aCol, $direction);
                $nKey = $nRow . '_' . $nCol;
                if ($ants->unoccupied($nRow, $nCol) and !isset($destinations[$nKey])) {
                    $destinations[$nKey] = 1;
                    $ants->issueOrder($aRow, $aCol, $direction);
                    continue 2;
                }
            }
            //ant can't move
            $destinations[$aRow . '_' . $aCol] = 1;
        }
    }
}
/**
 * Don't run bot when unit-testing
 */
if (!defined('PHPUnit_MAIN_METHOD')) {
    Ants::run(new HunterBot());
}
                $this->diffusion_map[$scent[0]][$scent[1]]['food'] += $boost;
            }
        }
        foreach ($this->enemy_hills as $hill) {
            $hill_boost = 10000 + rand(0, 2500);
            list($hill_row, $hill_col) = $hill;
            // Diffuse Food Scent in a radius of 10 units
            $scentable = $this->getScentableTiles($hill_row, $hill_col, 20);
            foreach ($scentable as $scent) {
                $distance = $this->ants->distance($hill_row, $hill_col, $scent[0], $scent[1]);
                if ($distance == 0) {
                    $boost = $hill_boost;
                } else {
                    $boost = $hill_boost / $distance;
                }
                if ($this->diffusion_map[$scent[0]][$scent[1]]['hill'] > $boost) {
                    continue;
                }
                $this->diffusion_map[$scent[0]][$scent[1]]['hill'] = $boost;
            }
        }
        $end = microtime(true);
        $this->debug("Diffusion Time: " . ($end - $start) . "\n");
    }
}
/**
 * Don't run bot when unit-testing
 */
if (!defined('PHPUnit_MAIN_METHOD')) {
    Ants::run(new MyBot());
}
Exemple #3
0
<?php

require_once 'Ants.php';
class RandomBot
{
    public function doTurn($ants)
    {
        $destinations = array();
        foreach ($ants->myAnts as $ant) {
            list($aRow, $aCol) = $ant;
            // try all directions randomly until one is passable and not occupied
            $directions = array_keys($ants->AIM);
            shuffle($directions);
            foreach ($directions as $direction) {
                list($nRow, $nCol) = $ants->destination($aRow, $aCol, $direction);
                $nKey = $nRow . '_' . $nCol;
                if (!isset($destinations[$nKey]) && $ants->passable($nRow, $nCol)) {
                    $ants->issueOrder($aRow, $aCol, $direction);
                    $destinations[$nKey] = 1;
                    continue 2;
                }
            }
            //ant can't move
            $destinations[$aRow . '_' . $aCol] = 1;
        }
    }
}
Ants::run(new RandomBot());
Exemple #4
0
            // send ants following a wall, keeping it on their left
            if (isset($this->antsLefty[$aKey])) {
                $direction = $this->antsLefty[$aKey];
                $directions = array($ants->LEFT[$direction], $direction, $ants->RIGHT[$direction], $ants->BEHIND[$direction]);
                // try 4 directions in order, attempting to turn left at corners
                foreach ($directions as $newDirection) {
                    list($nRow, $nCol) = $ants->destination($aRow, $aCol, $newDirection);
                    $nKey = $nRow . '_' . $nCol;
                    if ($ants->passable($nRow, $nCol)) {
                        if ($ants->unoccupied($nRow, $nCol) && !isset($destinations[$nKey])) {
                            $ants->issueOrder($aRow, $aCol, $newDirection);
                            $newLefty[$nKey] = $newDirection;
                            $destinations[$nKey] = 1;
                            break;
                        } else {
                            # have ant wait until it is clear
                            $newStraight[$aKey] = $ants->RIGHT[$direction];
                            $destinations[$aKey] = 1;
                            break;
                        }
                    }
                }
            }
        }
        # reset lists
        $this->antsStraight = $newStraight;
        $this->antsLefty = $newLefty;
    }
}
Ants::run(new LeftyBot());