1 | <?php declare(strict_types=1); |
||||
2 | |||||
3 | namespace Stratadox\PuzzleSolver\Puzzle\NQueens; |
||||
4 | |||||
5 | use Stratadox\PuzzleSolver\Move; |
||||
6 | use Stratadox\PuzzleSolver\Puzzle; |
||||
7 | use Stratadox\PuzzleSolver\Moves; |
||||
8 | use function explode; |
||||
9 | use function implode; |
||||
10 | use const PHP_EOL; |
||||
11 | |||||
12 | final class NQueensPuzzle implements Puzzle |
||||
13 | { |
||||
14 | /** @var Board */ |
||||
15 | private $board; |
||||
16 | /** @var Moves */ |
||||
17 | private $moves; |
||||
18 | |||||
19 | public function __construct(Board $board, Moves $moves) |
||||
20 | { |
||||
21 | $this->board = $board; |
||||
22 | $this->moves = $moves; |
||||
23 | } |
||||
24 | |||||
25 | public static function forQueens(int $n): Puzzle |
||||
26 | { |
||||
27 | return new self(Board::ofSize($n), Moves::none()); |
||||
28 | } |
||||
29 | |||||
30 | public function representation(): string |
||||
31 | { |
||||
32 | $board = explode(PHP_EOL, (string) $this->board); |
||||
33 | /** @var QueenPlacement $queen */ |
||||
34 | foreach ($this->moves as $queen) { |
||||
35 | $board[1 + $queen->row()][2 + $queen->column() * 3] = 'Q'; |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() The method
row() does not exist on Stratadox\PuzzleSolver\Move . It seems like you code against a sub-type of Stratadox\PuzzleSolver\Move such as Stratadox\PuzzleSolver\P...\NQueens\QueenPlacement or Stratadox\PuzzleSolver\Puzzle\Sudoku\Entry .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
36 | } |
||||
37 | return implode(PHP_EOL, $board); |
||||
38 | } |
||||
39 | |||||
40 | public function afterMaking(Move ...$moves): Puzzle |
||||
41 | { |
||||
42 | return new self($this->board, $this->moves->add(...$moves)); |
||||
43 | } |
||||
44 | |||||
45 | public function isSolved(): bool |
||||
46 | { |
||||
47 | return $this->board->isSolvedWith(...$this->moves); |
||||
48 | } |
||||
49 | |||||
50 | public function movesSoFar(): Moves |
||||
51 | { |
||||
52 | return $this->moves; |
||||
53 | } |
||||
54 | |||||
55 | public function possibleMoves(): Moves |
||||
56 | { |
||||
57 | return $this->board->possibleMovesAfter(...$this->moves); |
||||
58 | } |
||||
59 | } |
||||
60 |