-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.php
More file actions
53 lines (40 loc) · 1.31 KB
/
example.php
File metadata and controls
53 lines (40 loc) · 1.31 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
use Chessboard\Chessboard;
use Chessboard\Field\ChessboardCoords;
use Chessboard\Field\OutOfChessboardException;
use Chessboard\ShortestPathFinderFacade;
include_once 'vendor/autoload.php';
printf('Enter start X coordinates: ');
$startX = (int) fgets(STDIN);
printf('Enter start Y coordinates: ');
$startY = (int) fgets(STDIN);
printf('Enter target X coordinates: ');
$targetX = (int) fgets(STDIN);
printf('Enter target Y coordinates: ');
$targetY = (int) fgets(STDIN);
$chessboard = new Chessboard();
$finderFacade = new ShortestPathFinderFacade($chessboard);
try {
$targetCoords = new ChessboardCoords($startX, $startY);
$startCoords = new ChessboardCoords($targetX, $targetY);
} catch (OutOfChessboardException $e) {
echo 'Start or target coordinates is out of chessboard!';
exit;
}
$path = $finderFacade->findShortestKnightPath($targetCoords, $startCoords);
echo ' 1 2 3 4 5 6 7 8 '. "\n";
for ($j = 1; $j <= 8; $j++) {
echo ' ' . $j .' ';
for ($i = 1; $i <= 8; $i++) {
if ($i === $startX && $j === $startY) {
echo '| S ';
} elseif ($i === $targetX && $j === $targetY) {
echo '| C ';
} elseif (isset($path[$i . $j])) {
echo '| x ';
} else {
echo '| ';
}
}
echo '|' . "\n";
}