-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
33 lines (26 loc) · 994 Bytes
/
index.php
File metadata and controls
33 lines (26 loc) · 994 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
declare(strict_types=1);
namespace Chikeet\HorsePath;
use Chikeet\HorsePath\Model\ValueObjects\ChessCoordinate;
use Chikeet\HorsePath\Model\ValueObjects\ChessPosition;
use function array_map;
use function implode;
// ensure classes autoloading
if (@!include __DIR__ . '/vendor/autoload.php') {
echo "Please run `composer install` first.\n";
exit(1);
}
// set start and end positions - feel free to play
$startPosition = new ChessPosition(
ChessCoordinate::createFromLetter('a'),
ChessCoordinate::createFromHumanReadableInt(1),
);
$endPosition = new ChessPosition(
ChessCoordinate::createFromLetter('b'),
ChessCoordinate::createFromHumanReadableInt(2),
);
// get shortest path
$path = PathFinder::findShortestHorsePath($startPosition, $endPosition);
// get human readable result
$positionNames = array_map(fn(ChessPosition $position) => $position->toHumanReadableString(), $path);
echo "Path from $startPosition to $endPosition: " . implode(', ', $positionNames);