PHP code example of aminyazdanpanah / rush-hour-solver

1. Go to this page and download the library: Download aminyazdanpanah/rush-hour-solver 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/ */

    

aminyazdanpanah / rush-hour-solver example snippets




use RushHourSolver\BoardLoader;

$board_array = [
    [".", ".", ".", ".", "A", "A"],
    [".", ".", "B", "B", "C", "C"],
    ["r", "r", ".", ".", "E", "F"],
    ["G", "G", "H", "H", "E", "F"],
    [".", ".", ".", "I", "E", "F"],
    [".", ".", ".", "I", "J", "J"],
];

$board_loader = new BoardLoader($board_array);
$board = $board_loader->getBoard();

use RushHourSolver\BoardLoader;

$board_loader = new BoardLoader();
$board_loader->loadBoardFromFile("/path/to/board.txt");
$board = $board_loader->getBoard();

use RushHourSolver\BoardSolver;
use RushHourSolver\Enums\Direction;
use RushHourSolver\Enums\Orientation;

$board_solver = new BoardSolver($board);
$solutions = $board_solver->getSolution();

foreach ($solutions as $index => $solution) {
    $vehicle = $solution[0];
    $direction = $solution[1];

    if ($vehicle->getOrientation() === Orientation::HORIZONTAL && $direction === Direction::FORWARD) {
        $direction_name = "Right";
    } elseif ($vehicle->getOrientation() === Orientation::HORIZONTAL && $direction === Direction::BACKWARD) {
        $direction_name = "Left";
    } elseif ($vehicle->getOrientation() === Orientation::VERTICAL && $direction === Direction::FORWARD) {
        $direction_name = "Down";
    } elseif ($vehicle->getOrientation() === Orientation::VERTICAL && $direction === Direction::BACKWARD) {
        $direction_name = "Up";
    } else {
        $direction_name = "Unknown";
    }

    echo "\n" . $index + 1 . ": " . $vehicle->getName() . " -> " . $direction_name;
}