@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | |
5 | 5 | class ServiceProvider extends \Illuminate\Support\ServiceProvider |
6 | 6 | { |
7 | - const CONFIG_PATH = __DIR__ . '/../config/word-finder.php'; |
|
7 | + const CONFIG_PATH = __DIR__.'/../config/word-finder.php'; |
|
8 | 8 | |
9 | 9 | public function boot() |
10 | 10 | { |
@@ -20,7 +20,7 @@ discard block |
||
20 | 20 | 'word-finder' |
21 | 21 | ); |
22 | 22 | |
23 | - $this->app->bind('word-finder', function () { |
|
23 | + $this->app->bind('word-finder', function() { |
|
24 | 24 | return new WordFinder(config('word-finder')); |
25 | 25 | }); |
26 | 26 | } |
@@ -8,9 +8,9 @@ discard block |
||
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 | /** |
@@ -148,7 +148,7 @@ discard block |
||
148 | 148 | { |
149 | 149 | $inc = 1; |
150 | 150 | $word->setEnd($word->getStart()+$len-1); |
151 | - // si mot placé sur 2 lignes on décale à gauche |
|
151 | + // si mot placé sur 2 lignes on décale à gauche |
|
152 | 152 | while ($this->columnArray[$word->getEnd()] < $this->columnArray[$word->getStart()]) { |
153 | 153 | $word->setStart($word->getStart()-1); |
154 | 154 | $word->setEnd($word->getStart()+$len-1); |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | |
61 | 61 | protected function setWordsCollection(Collection $wordsCollection): self |
62 | 62 | { |
63 | - $this->wordsCollection = $wordsCollection->filter(function ($word) { |
|
63 | + $this->wordsCollection = $wordsCollection->filter(function($word) { |
|
64 | 64 | return strlen($word) >= $this->minWordLen && strlen($word) <= $this->maxWordLen; |
65 | 65 | })->map(fn($word) => strtoupper($word)); |
66 | 66 | |
@@ -72,7 +72,7 @@ discard block |
||
72 | 72 | $this->cells = array_fill(0, $this->gridSize * $this->gridSize, null); |
73 | 73 | |
74 | 74 | for ($i = 0; $i < (2 * $this->gridSize * $this->gridSize); $i++) { |
75 | - $this->columnArray[$i]=$this->getColumnDefault($i); |
|
75 | + $this->columnArray[$i] = $this->getColumnDefault($i); |
|
76 | 76 | } |
77 | 77 | |
78 | 78 | return $this; |
@@ -81,15 +81,15 @@ discard block |
||
81 | 81 | public function generate(): self |
82 | 82 | { |
83 | 83 | $blocks = $this->gridSize * $this->gridSize; |
84 | - $i=rand(0, $blocks-1); |
|
84 | + $i = rand(0, $blocks - 1); |
|
85 | 85 | |
86 | - $complete=0; |
|
86 | + $complete = 0; |
|
87 | 87 | while ($complete < $blocks) { |
88 | 88 | $this->placeWord($i); |
89 | 89 | $complete++; |
90 | 90 | $i++; |
91 | - if ($i==$blocks) { |
|
92 | - $i=0; |
|
91 | + if ($i == $blocks) { |
|
92 | + $i = 0; |
|
93 | 93 | } |
94 | 94 | } |
95 | 95 | |
@@ -106,7 +106,7 @@ discard block |
||
106 | 106 | $len = rand($this->minWordLen, $this->gridSize); |
107 | 107 | } while (in_array($len, $exclude)); |
108 | 108 | |
109 | - $available = $this->wordsCollection->filter(function ($word) use ($len) { |
|
109 | + $available = $this->wordsCollection->filter(function($word) use ($len) { |
|
110 | 110 | return strlen($word) === $len; |
111 | 111 | })->count(); |
112 | 112 | |
@@ -118,24 +118,24 @@ discard block |
||
118 | 118 | protected function addPlacedWord(Word $word, int $increment, int $len): void |
119 | 119 | { |
120 | 120 | $string = ''; |
121 | - $flag=false; |
|
121 | + $flag = false; |
|
122 | 122 | |
123 | - for ($i=$word->getStart(); $i<=$word->getEnd(); $i+=$increment) { |
|
124 | - if ($this->cells[$i]=='') { |
|
123 | + for ($i = $word->getStart(); $i <= $word->getEnd(); $i += $increment) { |
|
124 | + if ($this->cells[$i] == '') { |
|
125 | 125 | $string .= '_'; |
126 | 126 | } else { |
127 | 127 | $string .= $this->cells[$i]; |
128 | - $flag=true; |
|
128 | + $flag = true; |
|
129 | 129 | } |
130 | 130 | } |
131 | 131 | |
132 | - if (! $flag) { |
|
132 | + if (!$flag) { |
|
133 | 133 | $word->setLabel($word->getInversed() ? strrev($word->getLabel()) : $word->getLabel()); |
134 | 134 | $this->addWord($word); |
135 | 135 | return; |
136 | 136 | } |
137 | 137 | |
138 | - if (strpos($string, '_')===false) { |
|
138 | + if (strpos($string, '_') === false) { |
|
139 | 139 | return; |
140 | 140 | } |
141 | 141 | |
@@ -147,11 +147,11 @@ discard block |
||
147 | 147 | protected function placeWordHorizontally(Word $word, int $len): void |
148 | 148 | { |
149 | 149 | $inc = 1; |
150 | - $word->setEnd($word->getStart()+$len-1); |
|
150 | + $word->setEnd($word->getStart() + $len - 1); |
|
151 | 151 | // si mot placé sur 2 lignes on décale à gauche |
152 | 152 | while ($this->columnArray[$word->getEnd()] < $this->columnArray[$word->getStart()]) { |
153 | - $word->setStart($word->getStart()-1); |
|
154 | - $word->setEnd($word->getStart()+$len-1); |
|
153 | + $word->setStart($word->getStart() - 1); |
|
154 | + $word->setEnd($word->getStart() + $len - 1); |
|
155 | 155 | } |
156 | 156 | |
157 | 157 | $this->addPlacedWord($word, $inc, $len); |
@@ -159,12 +159,12 @@ discard block |
||
159 | 159 | |
160 | 160 | protected function placeWordVertical(Word $word, int $len): void |
161 | 161 | { |
162 | - $inc=$this->gridSize; |
|
163 | - $word->setEnd($word->getStart()+($len*$this->gridSize)-$this->gridSize); |
|
162 | + $inc = $this->gridSize; |
|
163 | + $word->setEnd($word->getStart() + ($len * $this->gridSize) - $this->gridSize); |
|
164 | 164 | // si le mot dépasse la grille en bas, on décale vers le haut |
165 | - while ($word->getEnd()>($this->gridSize*$this->gridSize)-1) { |
|
166 | - $word->setStart($word->getStart()-$this->gridSize); |
|
167 | - $word->setEnd($word->getStart()+($len*$this->gridSize)-$this->gridSize); |
|
165 | + while ($word->getEnd() > ($this->gridSize * $this->gridSize) - 1) { |
|
166 | + $word->setStart($word->getStart() - $this->gridSize); |
|
167 | + $word->setEnd($word->getStart() + ($len * $this->gridSize) - $this->gridSize); |
|
168 | 168 | } |
169 | 169 | |
170 | 170 | $this->addPlacedWord($word, $inc, $len); |
@@ -172,34 +172,34 @@ discard block |
||
172 | 172 | |
173 | 173 | protected function placeWordDiagonallyLtr(Word $word, int $len): void |
174 | 174 | { |
175 | - $inc=$this->gridSize+1; |
|
176 | - $word->setEnd($word->getStart()+($len*($this->gridSize+1))-($this->gridSize+1)); |
|
175 | + $inc = $this->gridSize + 1; |
|
176 | + $word->setEnd($word->getStart() + ($len * ($this->gridSize + 1)) - ($this->gridSize + 1)); |
|
177 | 177 | // si le mot dépasse la grille à droite, on décale à gauche |
178 | 178 | while ($this->columnArray[$word->getEnd()] < $this->columnArray[$word->getStart()]) { |
179 | - $word->setStart($word->getStart()-1); |
|
180 | - $word->setEnd($word->getStart()+($len*($this->gridSize+1))-($this->gridSize+1)); |
|
179 | + $word->setStart($word->getStart() - 1); |
|
180 | + $word->setEnd($word->getStart() + ($len * ($this->gridSize + 1)) - ($this->gridSize + 1)); |
|
181 | 181 | } |
182 | 182 | // si le mot dépasse la grille en bas, on décale vers le haut |
183 | - while ($word->getEnd()>($this->gridSize*$this->gridSize)-1) { |
|
184 | - $word->setStart($word->getStart()-$this->gridSize); |
|
185 | - $word->setEnd($word->getStart()+($len*($this->gridSize+1))-($this->gridSize+1)); |
|
183 | + while ($word->getEnd() > ($this->gridSize * $this->gridSize) - 1) { |
|
184 | + $word->setStart($word->getStart() - $this->gridSize); |
|
185 | + $word->setEnd($word->getStart() + ($len * ($this->gridSize + 1)) - ($this->gridSize + 1)); |
|
186 | 186 | } |
187 | 187 | $this->addPlacedWord($word, $inc, $len); |
188 | 188 | } |
189 | 189 | |
190 | 190 | protected function placeWordDiagonallyRtl(Word $word, int $len): void |
191 | 191 | { |
192 | - $inc=$this->gridSize-1; |
|
193 | - $word->setEnd($word->getStart()+(($len-1)*($this->gridSize-1))); |
|
192 | + $inc = $this->gridSize - 1; |
|
193 | + $word->setEnd($word->getStart() + (($len - 1) * ($this->gridSize - 1))); |
|
194 | 194 | // si le mot sort de la grille à gauche, on décale à droite |
195 | 195 | while ($this->columnArray[$word->getEnd()] > $this->columnArray[$word->getStart()]) { |
196 | - $word->setStart($word->getStart()+1); |
|
197 | - $word->setEnd($word->getStart()+(($len-1)*($this->gridSize-1))); |
|
196 | + $word->setStart($word->getStart() + 1); |
|
197 | + $word->setEnd($word->getStart() + (($len - 1) * ($this->gridSize - 1))); |
|
198 | 198 | } |
199 | 199 | // si le mot dépasse la grille en bas, on décale vers le haut |
200 | - while ($word->getEnd()>($this->gridSize*$this->gridSize)-1) { |
|
201 | - $word->setStart($word->getStart()-$this->gridSize); |
|
202 | - $word->setEnd($word->getStart()+(($len-1)*($this->gridSize-1))); |
|
200 | + while ($word->getEnd() > ($this->gridSize * $this->gridSize) - 1) { |
|
201 | + $word->setStart($word->getStart() - $this->gridSize); |
|
202 | + $word->setEnd($word->getStart() + (($len - 1) * ($this->gridSize - 1))); |
|
203 | 203 | } |
204 | 204 | $this->addPlacedWord($word, $inc, $len); |
205 | 205 | } |
@@ -230,19 +230,19 @@ discard block |
||
230 | 230 | |
231 | 231 | protected function getColumnDefault(int $x): int |
232 | 232 | { |
233 | - return ($x % $this->gridSize)+1; |
|
233 | + return ($x % $this->gridSize) + 1; |
|
234 | 234 | } |
235 | 235 | |
236 | 236 | protected function markWordUsed($word): void |
237 | 237 | { |
238 | - $this->wordsCollection = $this->wordsCollection->reject(function ($current) use ($word) { |
|
238 | + $this->wordsCollection = $this->wordsCollection->reject(function($current) use ($word) { |
|
239 | 239 | return $current === $word; |
240 | 240 | }); |
241 | 241 | } |
242 | 242 | |
243 | 243 | protected function getRandomWord(int $len): string |
244 | 244 | { |
245 | - $word = $this->wordsCollection->filter(function ($word) use ($len) { |
|
245 | + $word = $this->wordsCollection->filter(function($word) use ($len) { |
|
246 | 246 | return strlen($word) === $len; |
247 | 247 | })->random(1)->first(); |
248 | 248 | |
@@ -254,7 +254,7 @@ discard block |
||
254 | 254 | protected function getWordLike(string $pattern): ?string |
255 | 255 | { |
256 | 256 | $pattern = str_replace("_", ".", $pattern); |
257 | - $words = $this->wordsCollection->filter(function ($word) use ($pattern) { |
|
257 | + $words = $this->wordsCollection->filter(function($word) use ($pattern) { |
|
258 | 258 | return preg_match("/^$pattern\$/i", $word); |
259 | 259 | }); |
260 | 260 | |
@@ -275,37 +275,37 @@ discard block |
||
275 | 275 | return; |
276 | 276 | } |
277 | 277 | |
278 | - $j=0; |
|
278 | + $j = 0; |
|
279 | 279 | switch ($word->getOrientation()) { |
280 | 280 | case Word::HORIZONTAL: |
281 | - for ($i=$word->getStart(); $j<strlen($word->getLabel()); $i++) { |
|
282 | - $this->cells[$i]=substr($word->getLabel(), $j, 1); |
|
281 | + for ($i = $word->getStart(); $j < strlen($word->getLabel()); $i++) { |
|
282 | + $this->cells[$i] = substr($word->getLabel(), $j, 1); |
|
283 | 283 | $j++; |
284 | 284 | } |
285 | 285 | break; |
286 | 286 | |
287 | 287 | case Word::VERTICAL: |
288 | - for ($i=$word->getStart(); $j<strlen($word->getLabel()); $i+=$this->gridSize) { |
|
289 | - $this->cells[$i]=substr($word->getLabel(), $j, 1); |
|
288 | + for ($i = $word->getStart(); $j < strlen($word->getLabel()); $i += $this->gridSize) { |
|
289 | + $this->cells[$i] = substr($word->getLabel(), $j, 1); |
|
290 | 290 | $j++; |
291 | 291 | } |
292 | 292 | break; |
293 | 293 | |
294 | 294 | case Word::DIAGONAL_LEFT_TO_RIGHT: |
295 | - for ($i=$word->getStart(); $j<strlen($word->getLabel()); $i+=$this->gridSize+1) { |
|
296 | - $this->cells[$i]=substr($word->getLabel(), $j, 1); |
|
295 | + for ($i = $word->getStart(); $j < strlen($word->getLabel()); $i += $this->gridSize + 1) { |
|
296 | + $this->cells[$i] = substr($word->getLabel(), $j, 1); |
|
297 | 297 | $j++; |
298 | 298 | } |
299 | 299 | break; |
300 | 300 | |
301 | 301 | case Word::DIAGONAL_RIGHT_TO_LEFT: |
302 | - for ($i=$word->getStart(); $j<strlen($word->getLabel()); $i+=$this->gridSize-1) { |
|
303 | - $this->cells[$i]=substr($word->getLabel(), $j, 1); |
|
302 | + for ($i = $word->getStart(); $j < strlen($word->getLabel()); $i += $this->gridSize - 1) { |
|
303 | + $this->cells[$i] = substr($word->getLabel(), $j, 1); |
|
304 | 304 | $j++; |
305 | 305 | } |
306 | 306 | break; |
307 | 307 | } |
308 | - $this->wordsList[]=$word; |
|
308 | + $this->wordsList[] = $word; |
|
309 | 309 | } |
310 | 310 | |
311 | 311 | public function getTextGrid() |
@@ -319,18 +319,18 @@ discard block |
||
319 | 319 | } |
320 | 320 | return $r; |
321 | 321 | |
322 | - $cpt=0; |
|
322 | + $cpt = 0; |
|
323 | 323 | foreach ($this->cells as $letter) { |
324 | - if ($letter=='') { |
|
325 | - $r.=chr(rand(65, 90)); |
|
324 | + if ($letter == '') { |
|
325 | + $r .= chr(rand(65, 90)); |
|
326 | 326 | } else { |
327 | - $r.=$letter; |
|
327 | + $r .= $letter; |
|
328 | 328 | } |
329 | - $r.=' '; |
|
329 | + $r .= ' '; |
|
330 | 330 | $cpt++; |
331 | - if ($cpt==$this->gridSize) { |
|
332 | - $r.="\n"; |
|
333 | - $cpt=0; |
|
331 | + if ($cpt == $this->gridSize) { |
|
332 | + $r .= "\n"; |
|
333 | + $cpt = 0; |
|
334 | 334 | } |
335 | 335 | } |
336 | 336 | |
@@ -356,7 +356,7 @@ discard block |
||
356 | 356 | |
357 | 357 | public function getPuzzleWords() |
358 | 358 | { |
359 | - return collect($this->wordsList)->map(function (Word $word) { |
|
359 | + return collect($this->wordsList)->map(function(Word $word) { |
|
360 | 360 | $label = $word->getLabel(); |
361 | 361 | if ($word->getInversed()) { |
362 | 362 | $label = strrev($label); |
@@ -45,7 +45,7 @@ |
||
45 | 45 | |
46 | 46 | public function setOrientation(int $orientation): self |
47 | 47 | { |
48 | - if (! in_array($orientation, [0,1,2,3])) { |
|
48 | + if (!in_array($orientation, [0, 1, 2, 3])) { |
|
49 | 49 | throw new RuntimeException("Orientation not valid"); |
50 | 50 | } |
51 | 51 | $this->orientation = $orientation; |