@@ -8,9 +8,9 @@ |
||
8 | 8 | class Grid |
9 | 9 | { |
10 | 10 | |
11 | - /** |
|
12 | - * minimum word length for the puzzle |
|
13 | - */ |
|
11 | + /** |
|
12 | + * minimum word length for the puzzle |
|
13 | + */ |
|
14 | 14 | protected int $minWordLen; |
15 | 15 | |
16 | 16 | /** |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | |
71 | 71 | protected function setWordsCollection(Collection $wordsCollection): self |
72 | 72 | { |
73 | - $this->wordsCollection = $wordsCollection->filter(function ($word) { |
|
73 | + $this->wordsCollection = $wordsCollection->filter(function($word) { |
|
74 | 74 | return strlen($word) >= $this->minWordLen && strlen($word) <= $this->maxWordLen; |
75 | 75 | })->map(fn($word) => strtoupper($word)); |
76 | 76 | |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | $this->cells = array_fill(0, $this->gridSize * $this->gridSize, null); |
83 | 83 | |
84 | 84 | for ($i = 0; $i < (2 * $this->gridSize * $this->gridSize); $i++) { |
85 | - $this->columnArray[$i]=$this->getColumnDefault($i); |
|
85 | + $this->columnArray[$i] = $this->getColumnDefault($i); |
|
86 | 86 | } |
87 | 87 | |
88 | 88 | return $this; |
@@ -91,15 +91,15 @@ discard block |
||
91 | 91 | public function generate(): self |
92 | 92 | { |
93 | 93 | $blocks = $this->gridSize * $this->gridSize; |
94 | - $i=rand(0, $blocks-1); |
|
94 | + $i = rand(0, $blocks - 1); |
|
95 | 95 | |
96 | - $complete=0; |
|
96 | + $complete = 0; |
|
97 | 97 | while ($complete < $blocks) { |
98 | 98 | $this->placeWord($i); |
99 | 99 | $complete++; |
100 | 100 | $i++; |
101 | - if ($i==$blocks) { |
|
102 | - $i=0; |
|
101 | + if ($i == $blocks) { |
|
102 | + $i = 0; |
|
103 | 103 | } |
104 | 104 | } |
105 | 105 | |
@@ -116,7 +116,7 @@ discard block |
||
116 | 116 | $len = rand($this->minWordLen, $this->gridSize); |
117 | 117 | } while (in_array($len, $exclude)); |
118 | 118 | |
119 | - $available = $this->wordsCollection->filter(function ($word) use ($len) { |
|
119 | + $available = $this->wordsCollection->filter(function($word) use ($len) { |
|
120 | 120 | return strlen($word) === $len; |
121 | 121 | })->count(); |
122 | 122 | |
@@ -128,25 +128,25 @@ discard block |
||
128 | 128 | protected function addPlacedWord(Word $word, int $increment, int $len): void |
129 | 129 | { |
130 | 130 | $string = ''; |
131 | - $flag=false; |
|
131 | + $flag = false; |
|
132 | 132 | |
133 | - for ($i=$word->getStart(); $i<=$word->getEnd(); $i+=$increment) { |
|
134 | - if ($this->cells[$i]=== null) { |
|
133 | + for ($i = $word->getStart(); $i <= $word->getEnd(); $i += $increment) { |
|
134 | + if ($this->cells[$i] === null) { |
|
135 | 135 | $string .= '_'; |
136 | 136 | } else { |
137 | 137 | $string .= $this->cells[$i]; |
138 | - $flag=true; |
|
138 | + $flag = true; |
|
139 | 139 | } |
140 | 140 | } |
141 | 141 | |
142 | - if (! $flag) { |
|
142 | + if (!$flag) { |
|
143 | 143 | $randomWord = $this->getRandomWord($len); |
144 | 144 | $word->setLabel($word->getInversed() ? strrev($randomWord) : $randomWord); |
145 | 145 | $this->addWord($word); |
146 | 146 | return; |
147 | 147 | } |
148 | 148 | |
149 | - if (strpos($string, '_')===false) { |
|
149 | + if (strpos($string, '_') === false) { |
|
150 | 150 | return; |
151 | 151 | } |
152 | 152 | |
@@ -158,10 +158,10 @@ discard block |
||
158 | 158 | protected function placeWordHorizontally(Word $word, int $len): void |
159 | 159 | { |
160 | 160 | $inc = 1; |
161 | - $word->setEnd($word->getStart()+$len-1); |
|
161 | + $word->setEnd($word->getStart() + $len - 1); |
|
162 | 162 | while ($this->columnArray[$word->getEnd()] < $this->columnArray[$word->getStart()]) { |
163 | - $word->setStart($word->getStart()-1); |
|
164 | - $word->setEnd($word->getStart()+$len-1); |
|
163 | + $word->setStart($word->getStart() - 1); |
|
164 | + $word->setEnd($word->getStart() + $len - 1); |
|
165 | 165 | } |
166 | 166 | |
167 | 167 | $this->addPlacedWord($word, $inc, $len); |
@@ -169,11 +169,11 @@ discard block |
||
169 | 169 | |
170 | 170 | protected function placeWordVertical(Word $word, int $len): void |
171 | 171 | { |
172 | - $inc=$this->gridSize; |
|
173 | - $word->setEnd($word->getStart()+($len*$this->gridSize)-$this->gridSize); |
|
174 | - while ($word->getEnd()>($this->gridSize*$this->gridSize)-1) { |
|
175 | - $word->setStart($word->getStart()-$this->gridSize); |
|
176 | - $word->setEnd($word->getStart()+($len*$this->gridSize)-$this->gridSize); |
|
172 | + $inc = $this->gridSize; |
|
173 | + $word->setEnd($word->getStart() + ($len * $this->gridSize) - $this->gridSize); |
|
174 | + while ($word->getEnd() > ($this->gridSize * $this->gridSize) - 1) { |
|
175 | + $word->setStart($word->getStart() - $this->gridSize); |
|
176 | + $word->setEnd($word->getStart() + ($len * $this->gridSize) - $this->gridSize); |
|
177 | 177 | } |
178 | 178 | |
179 | 179 | $this->addPlacedWord($word, $inc, $len); |
@@ -181,30 +181,30 @@ discard block |
||
181 | 181 | |
182 | 182 | protected function placeWordDiagonallyLtr(Word $word, int $len): void |
183 | 183 | { |
184 | - $inc=$this->gridSize+1; |
|
185 | - $word->setEnd($word->getStart()+($len*($this->gridSize+1))-($this->gridSize+1)); |
|
184 | + $inc = $this->gridSize + 1; |
|
185 | + $word->setEnd($word->getStart() + ($len * ($this->gridSize + 1)) - ($this->gridSize + 1)); |
|
186 | 186 | while ($this->columnArray[$word->getEnd()] < $this->columnArray[$word->getStart()]) { |
187 | - $word->setStart($word->getStart()-1); |
|
188 | - $word->setEnd($word->getStart()+($len*($this->gridSize+1))-($this->gridSize+1)); |
|
187 | + $word->setStart($word->getStart() - 1); |
|
188 | + $word->setEnd($word->getStart() + ($len * ($this->gridSize + 1)) - ($this->gridSize + 1)); |
|
189 | 189 | } |
190 | - while ($word->getEnd()>($this->gridSize*$this->gridSize)-1) { |
|
191 | - $word->setStart($word->getStart()-$this->gridSize); |
|
192 | - $word->setEnd($word->getStart()+($len*($this->gridSize+1))-($this->gridSize+1)); |
|
190 | + while ($word->getEnd() > ($this->gridSize * $this->gridSize) - 1) { |
|
191 | + $word->setStart($word->getStart() - $this->gridSize); |
|
192 | + $word->setEnd($word->getStart() + ($len * ($this->gridSize + 1)) - ($this->gridSize + 1)); |
|
193 | 193 | } |
194 | 194 | $this->addPlacedWord($word, $inc, $len); |
195 | 195 | } |
196 | 196 | |
197 | 197 | protected function placeWordDiagonallyRtl(Word $word, int $len): void |
198 | 198 | { |
199 | - $inc=$this->gridSize-1; |
|
200 | - $word->setEnd($word->getStart()+(($len-1)*($this->gridSize-1))); |
|
199 | + $inc = $this->gridSize - 1; |
|
200 | + $word->setEnd($word->getStart() + (($len - 1) * ($this->gridSize - 1))); |
|
201 | 201 | while ($this->columnArray[$word->getEnd()] > $this->columnArray[$word->getStart()]) { |
202 | - $word->setStart($word->getStart()+1); |
|
203 | - $word->setEnd($word->getStart()+(($len-1)*($this->gridSize-1))); |
|
202 | + $word->setStart($word->getStart() + 1); |
|
203 | + $word->setEnd($word->getStart() + (($len - 1) * ($this->gridSize - 1))); |
|
204 | 204 | } |
205 | - while ($word->getEnd()>($this->gridSize*$this->gridSize)-1) { |
|
206 | - $word->setStart($word->getStart()-$this->gridSize); |
|
207 | - $word->setEnd($word->getStart()+(($len-1)*($this->gridSize-1))); |
|
205 | + while ($word->getEnd() > ($this->gridSize * $this->gridSize) - 1) { |
|
206 | + $word->setStart($word->getStart() - $this->gridSize); |
|
207 | + $word->setEnd($word->getStart() + (($len - 1) * ($this->gridSize - 1))); |
|
208 | 208 | } |
209 | 209 | $this->addPlacedWord($word, $inc, $len); |
210 | 210 | } |
@@ -235,19 +235,19 @@ discard block |
||
235 | 235 | |
236 | 236 | protected function getColumnDefault(int $x): int |
237 | 237 | { |
238 | - return ($x % $this->gridSize)+1; |
|
238 | + return ($x % $this->gridSize) + 1; |
|
239 | 239 | } |
240 | 240 | |
241 | 241 | protected function markWordUsed($word): void |
242 | 242 | { |
243 | - $this->wordsCollection = $this->wordsCollection->reject(function ($current) use ($word) { |
|
243 | + $this->wordsCollection = $this->wordsCollection->reject(function($current) use ($word) { |
|
244 | 244 | return $current === $word; |
245 | 245 | }); |
246 | 246 | } |
247 | 247 | |
248 | 248 | protected function getRandomWord(int $len): string |
249 | 249 | { |
250 | - $word = $this->wordsCollection->filter(function ($word) use ($len) { |
|
250 | + $word = $this->wordsCollection->filter(function($word) use ($len) { |
|
251 | 251 | return strlen($word) === $len; |
252 | 252 | })->random(1)->first(); |
253 | 253 | |
@@ -259,7 +259,7 @@ discard block |
||
259 | 259 | protected function getWordLike(string $pattern): ?string |
260 | 260 | { |
261 | 261 | $pattern = str_replace("_", ".", $pattern); |
262 | - $words = $this->wordsCollection->filter(function ($word) use ($pattern) { |
|
262 | + $words = $this->wordsCollection->filter(function($word) use ($pattern) { |
|
263 | 263 | return preg_match("/^$pattern\$/i", $word); |
264 | 264 | }); |
265 | 265 | |
@@ -280,37 +280,37 @@ discard block |
||
280 | 280 | return; |
281 | 281 | } |
282 | 282 | |
283 | - $j=0; |
|
283 | + $j = 0; |
|
284 | 284 | switch ($word->getOrientation()) { |
285 | 285 | case Word::HORIZONTAL: |
286 | - for ($i=$word->getStart(); $j<strlen($word->getLabel()); $i++) { |
|
287 | - $this->cells[$i]=substr($word->getLabel(), $j, 1); |
|
286 | + for ($i = $word->getStart(); $j < strlen($word->getLabel()); $i++) { |
|
287 | + $this->cells[$i] = substr($word->getLabel(), $j, 1); |
|
288 | 288 | $j++; |
289 | 289 | } |
290 | 290 | break; |
291 | 291 | |
292 | 292 | case Word::VERTICAL: |
293 | - for ($i=$word->getStart(); $j<strlen($word->getLabel()); $i+=$this->gridSize) { |
|
294 | - $this->cells[$i]=substr($word->getLabel(), $j, 1); |
|
293 | + for ($i = $word->getStart(); $j < strlen($word->getLabel()); $i += $this->gridSize) { |
|
294 | + $this->cells[$i] = substr($word->getLabel(), $j, 1); |
|
295 | 295 | $j++; |
296 | 296 | } |
297 | 297 | break; |
298 | 298 | |
299 | 299 | case Word::DIAGONAL_LEFT_TO_RIGHT: |
300 | - for ($i=$word->getStart(); $j<strlen($word->getLabel()); $i+=$this->gridSize+1) { |
|
301 | - $this->cells[$i]=substr($word->getLabel(), $j, 1); |
|
300 | + for ($i = $word->getStart(); $j < strlen($word->getLabel()); $i += $this->gridSize + 1) { |
|
301 | + $this->cells[$i] = substr($word->getLabel(), $j, 1); |
|
302 | 302 | $j++; |
303 | 303 | } |
304 | 304 | break; |
305 | 305 | |
306 | 306 | case Word::DIAGONAL_RIGHT_TO_LEFT: |
307 | - for ($i=$word->getStart(); $j<strlen($word->getLabel()); $i+=$this->gridSize-1) { |
|
308 | - $this->cells[$i]=substr($word->getLabel(), $j, 1); |
|
307 | + for ($i = $word->getStart(); $j < strlen($word->getLabel()); $i += $this->gridSize - 1) { |
|
308 | + $this->cells[$i] = substr($word->getLabel(), $j, 1); |
|
309 | 309 | $j++; |
310 | 310 | } |
311 | 311 | break; |
312 | 312 | } |
313 | - $this->wordsList[]=$word; |
|
313 | + $this->wordsList[] = $word; |
|
314 | 314 | } |
315 | 315 | |
316 | 316 | public function getTextGrid() |
@@ -344,7 +344,7 @@ discard block |
||
344 | 344 | |
345 | 345 | public function getPuzzleWords() |
346 | 346 | { |
347 | - return collect($this->wordsList)->map(function (Word $word) { |
|
347 | + return collect($this->wordsList)->map(function(Word $word) { |
|
348 | 348 | $label = $word->getLabel(); |
349 | 349 | if ($word->getInversed()) { |
350 | 350 | $label = strrev(/** @scrutinizer ignore-type */$label); |