1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Crossword\Features\Constructor\Scanner\Grid; |
6
|
|
|
|
7
|
|
|
use ArrayIterator; |
8
|
|
|
use IteratorAggregate; |
9
|
|
|
|
10
|
|
|
final class Grid implements IteratorAggregate |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var Cell[] |
14
|
|
|
*/ |
15
|
|
|
private array $cells; |
16
|
|
|
|
17
|
|
|
public function __construct(Line ...$lines) |
18
|
|
|
{ |
19
|
|
|
$this->cells = []; |
20
|
|
|
|
21
|
|
|
foreach ($lines as $line) { |
22
|
|
|
$this->fillLine($line); |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function getIterator(): ArrayIterator |
27
|
|
|
{ |
28
|
|
|
return new ArrayIterator($this->cells); |
29
|
|
|
} |
30
|
|
|
|
31
|
1 |
|
public function isEmpty(): bool |
32
|
|
|
{ |
33
|
1 |
|
return 0 === count(array_filter($this->cells, static fn (Cell $cell) => null !== $cell->letter())); |
34
|
|
|
} |
35
|
|
|
|
36
|
3 |
|
public function shiftCell(Coordinate $coordinate): Cell |
37
|
|
|
{ |
38
|
3 |
|
if ($coordinate->inFrame() && $this->exist($coordinate)) { |
39
|
2 |
|
return $this->cells[(string) $coordinate]; |
40
|
|
|
} |
41
|
|
|
|
42
|
3 |
|
return new Cell($coordinate, null); |
|
|
|
|
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
public function fillLine(Line $line): void |
46
|
|
|
{ |
47
|
|
|
/** @var Cell $cell */ |
48
|
1 |
|
foreach ($line as $cell) { |
49
|
1 |
|
if ($cell->isLetter()) { |
50
|
1 |
|
$this->cells[(string) $cell->coordinate()] = $cell; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
array_map([$this, 'fillBlackSquare'], $this->cells); |
55
|
1 |
|
} |
56
|
|
|
|
57
|
4 |
|
private function fillBlackSquare(Cell $cell): void |
58
|
|
|
{ |
59
|
4 |
|
if ($cell->isLetter()) { |
60
|
4 |
|
$this->searchBlackSquareByYRow($cell->coordinate()); |
61
|
4 |
|
$this->searchBlackSquareByXRow($cell->coordinate()); |
62
|
|
|
} |
63
|
4 |
|
} |
64
|
|
|
|
65
|
|
|
private function searchBlackSquareByYRow(Coordinate $coordinate): void |
66
|
|
|
{ |
67
|
|
|
$topCell = $this->shiftCell($coordinate->top()); |
68
|
|
|
$downCell = $this->shiftCell($coordinate->down()); |
69
|
|
|
|
70
|
|
|
$topCoordinate = $coordinate->top(); |
71
|
|
|
if ($topCoordinate->inFrame() && !$topCell->letter() && $downCell->letter()) { |
72
|
|
|
$this->fillCellBlack($coordinate->top()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if ($topCell->letter() && !$downCell->letter()) { |
76
|
|
|
$this->fillCellBlack($coordinate->down()); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function searchBlackSquareByXRow(Coordinate $coordinate): void |
81
|
|
|
{ |
82
|
|
|
$leftCell = $this->shiftCell($coordinate->left()); |
83
|
|
|
$rightCell = $this->shiftCell($coordinate->right()); |
84
|
|
|
|
85
|
|
|
$leftCoordinate = $coordinate->left(); |
86
|
|
|
if ($leftCoordinate->inFrame() && !$leftCell->letter() && $rightCell->letter()) { |
87
|
|
|
$this->fillCellBlack($coordinate->left()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if ($leftCell->letter() && !$rightCell->letter()) { |
91
|
|
|
$this->fillCellBlack($coordinate->right()); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function fillCellBlack(Coordinate $coordinate): void |
96
|
|
|
{ |
97
|
|
|
$this->cells[(string) $coordinate] = new Cell($coordinate, ''); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
private function exist(Coordinate $coordinate): bool |
101
|
|
|
{ |
102
|
|
|
return array_key_exists((string) $coordinate, $this->cells); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|