@@ -151,18 +151,15 @@ public function initFunctionalPatterns():self{
151151 * @return int[][]|bool[][]
152152 */
153153 public function getMatrix (bool $ boolean = null ):array {
154-
155- if (!$ boolean ){
156- return $ this ->matrix ;
157- }
158-
159154 $ matrix = [];
160155
161156 foreach ($ this ->matrix as $ y => $ row ){
162157 $ matrix [$ y ] = [];
163158
164159 foreach ($ row as $ x => $ val ){
165- $ matrix [$ y ][$ x ] = ($ val & $ this ::IS_DARK ) === $ this ::IS_DARK ;
160+ $ matrix [$ y ][$ x ] = $ boolean === true
161+ ? $ this ->checkType ($ x , $ y , $ this ::IS_DARK )
162+ : $ this ->get ($ x , $ y );
166163 }
167164 }
168165
@@ -303,12 +300,13 @@ public function flip(int $x, int $y):self{
303300 * true => $value & $M_TYPE === $M_TYPE
304301 */
305302 public function checkType (int $ x , int $ y , int $ M_TYPE ):bool {
303+ $ val = $ this ->get ($ x , $ y );
306304
307- if (! isset ( $ this -> matrix [ $ y ][ $ x ]) ){
305+ if ($ val === - 1 ){
308306 return false ;
309307 }
310308
311- return ($ this -> matrix [ $ y ][ $ x ] & $ M_TYPE ) === $ M_TYPE ;
309+ return ($ val & $ M_TYPE ) === $ M_TYPE ;
312310 }
313311
314312 /**
@@ -343,21 +341,21 @@ public function check(int $x, int $y):bool{
343341 * 8 # 4
344342 * 7 6 5
345343 */
346- public function checkNeighbours (int $ x , int $ y , int $ M_TYPE_VALUE = null ):int {
344+ public function checkNeighbours (int $ x , int $ y , int $ M_TYPE = null ):int {
347345 $ bits = 0 ;
348346
349347 foreach ($ this ::neighbours as $ bit => $ coord ){
350348 [$ ix , $ iy ] = $ coord ;
351349
352- // check if the field is the same type
353- if (
354- $ M_TYPE_VALUE !== null
355- && ( $ this -> get (( $ x + $ ix ), ( $ y + $ iy )) | $ this :: IS_DARK ) !== ( $ M_TYPE_VALUE | $ this :: IS_DARK )
356- ){
350+ $ ix += $ x ;
351+ $ iy += $ y ;
352+
353+ // $M_TYPE is given, skip if the field is not the same type
354+ if ( $ M_TYPE !== null && ! $ this -> checkType ( $ ix , $ iy , $ M_TYPE ) ){
357355 continue ;
358356 }
359357
360- if ($ this ->checkType (( $ x + $ ix ), ( $ y + $ iy ) , $ this ::IS_DARK )){
358+ if ($ this ->checkType ($ ix , $ iy , $ this ::IS_DARK )){
361359 $ bits |= $ bit ;
362360 }
363361 }
0 commit comments