PHP code example of jmgq / a-star

1. Go to this page and download the library: Download jmgq/a-star library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jmgq / a-star example snippets


    use JMGQ\AStar\DomainLogicInterface;

    class DomainLogic implements DomainLogicInterface
    {
        // ...

        public function getAdjacentNodes(mixed $node): iterable
        {
            // Return a collection of adjacent nodes
        }

        public function calculateRealCost(mixed $node, mixed $adjacent): float | int
        {
            // Return the actual cost between two adjacent nodes
        }

        public function calculateEstimatedCost(mixed $fromNode, mixed $toNode): float | int
        {
            // Return the heuristic estimated cost between the two given nodes
        }

        // ...
    }
    

    use JMGQ\AStar\AStar;

    $domainLogic = new DomainLogic();

    $aStar = new AStar($domainLogic);
    

    $solution = $aStar->run($start, $goal);
    

    $node1 = ['x' => 4, 'y' => 5];
    $node2 = ['y' => 5, 'x' => 4];
    serialize($node1); // a:2:{s:1:"x";i:4;s:1:"y";i:5;}
    serialize($node2); // a:2:{s:1:"y";i:5;s:1:"x";i:4;}
    

interface NodeIdentifierInterface
{
    public function getUniqueNodeId(): string;
}

use JMGQ\AStar\Node\NodeIdentifierInterface;

class Position implements NodeIdentifierInterface
{
    private int $row;
    private int $column;

    // ...

    public function getUniqueNodeId(): string
    {
        return $this->row . 'x' . $this->column;
    }

    // ...
}