1 | <?php declare(strict_types=1); |
||
2 | |||
3 | namespace Stratadox\PuzzleSolver\Puzzle\WolfGoatCabbage; |
||
4 | |||
5 | use Stratadox\PuzzleSolver\Moves; |
||
6 | use function assert; |
||
7 | use function sprintf; |
||
8 | |||
9 | final class Farmer |
||
10 | { |
||
11 | /** @var Riverbank */ |
||
12 | private $currentBank; |
||
13 | /** @var Riverbank */ |
||
14 | private $otherBank; |
||
15 | |||
16 | public function __construct( |
||
17 | Riverbank $currentBank, |
||
18 | Riverbank $otherBank |
||
19 | ) { |
||
20 | $this->currentBank = $currentBank; |
||
21 | $this->otherBank = $otherBank; |
||
22 | } |
||
23 | |||
24 | public function hasMovedAllPurchasesAlong(): bool |
||
25 | { |
||
26 | return $this->currentBank->isGoal() && $this->currentBank->isFull(); |
||
27 | } |
||
28 | |||
29 | public static function withWolfGoatAndCabbage(): self |
||
30 | { |
||
31 | return new self( |
||
32 | new Riverbank(true, Purchase::cabbage(), Purchase::goat(), Purchase::wolf()), |
||
33 | new Riverbank(false) |
||
34 | ); |
||
35 | } |
||
36 | |||
37 | public function possibleCrossings(): Moves |
||
38 | { |
||
39 | return Crossing::allCrossingsFor($this->currentBank) |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
40 | ->filterWith(function (Crossing $crossing) { |
||
41 | return $this->currentBank->canTakeAway($crossing->bringAlong()); |
||
42 | }); |
||
43 | } |
||
44 | |||
45 | public function after(Crossing $crossing): self |
||
46 | { |
||
47 | assert($this->currentBank->canTakeAway($crossing->bringAlong())); |
||
48 | return new self( |
||
49 | $this->otherBank->with($crossing->bringAlong()), |
||
50 | $this->currentBank->without($crossing->bringAlong()) |
||
51 | ); |
||
52 | } |
||
53 | |||
54 | public function __toString(): string |
||
55 | { |
||
56 | return sprintf( |
||
57 | 'Current bank: %s; Other bank: %s', |
||
58 | $this->currentBank, |
||
59 | $this->otherBank |
||
60 | ); |
||
61 | } |
||
62 | } |
||
63 |