Rush Hour is a tiny sliding board game played on a board of 6x6 squares(it can be larger). The objective in the game is to free the red car from the surrounding traffic by sliding the cars out of the way onto free squares until the red car can drive out of the exit on the right side of the board. Note that cars can only go forward of reverse, not to the side. Use google or this video for an impression.
Contents
This version of the package is only compatible with PHP 8.1 or higher.
Install the package via composer:
composer require aminyazdanpanah/rush-hour-solver
Alternatively, add the dependency directly to your composer.json
file:
"require": {
"aminyazdanpanah/rush-hour-solver": "^0.1"
}
First of all, you need to include the package in your code:
require 'vendor/autoload.php'; // path to the autoload file
There two ways to load a board.
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;
}
steps to solve the board
I'd love your help in improving, correcting, adding to the specification. Please file an issue or submit a pull request.
- See Contributing File for more information.
- If you discover a security vulnerability within this package, please see SECURITY File for more information.
The MIT License (MIT). See License File for more information.