@@ -56,7 +56,7 @@ public function testPattern():int{
56
56
$ penalty = 0 ;
57
57
58
58
for ($ level = 1 ; $ level <= 4 ; $ level ++){
59
- $ penalty += call_user_func ([$ this , 'testLevel ' .$ level ]);
59
+ $ penalty += call_user_func_array ([$ this , 'testLevel ' .$ level], [ $ this -> matrix -> matrix ( true ) ]);
60
60
}
61
61
62
62
return (int )$ penalty ;
@@ -65,12 +65,12 @@ public function testPattern():int{
65
65
/**
66
66
* Checks for each group of five or more same-colored modules in a row (or column)
67
67
*
68
- * @return float
68
+ * @return int
69
69
*/
70
- protected function testLevel1 (): float {
70
+ protected function testLevel1 (array $ m ): int {
71
71
$ penalty = 0 ;
72
72
73
- foreach ($ this -> matrix -> matrix () as $ y => $ row ){
73
+ foreach ($ m as $ y => $ row ){
74
74
foreach ($ row as $ x => $ val ){
75
75
$ count = 0 ;
76
76
@@ -82,11 +82,11 @@ protected function testLevel1():float{
82
82
83
83
for ($ rx = -1 ; $ rx <= 1 ; $ rx ++){
84
84
85
- if (($ ry === 0 && $ rx === 0 ) || ($ x + $ rx < 0 || $ this ->moduleCount <= $ x + $ rx )){
85
+ if (($ ry === 0 && $ rx === 0 ) || (( $ x + $ rx) < 0 || $ this ->moduleCount <= ( $ x + $ rx) )){
86
86
continue ;
87
87
}
88
88
89
- if ($ this -> matrix -> check ( $ x + $ rx , $ y + $ ry ) === ( $ val >> 8 > 0 ) ){
89
+ if ($ m [ $ y + $ ry ][ $ x + $ rx ] === $ val ){
90
90
$ count ++;
91
91
}
92
92
@@ -106,107 +106,90 @@ protected function testLevel1():float{
106
106
/**
107
107
* Checks for each 2x2 area of same-colored modules in the matrix
108
108
*
109
- * @return float
109
+ * @return int
110
110
*/
111
- protected function testLevel2 (): float {
111
+ protected function testLevel2 (array $ m ): int {
112
112
$ penalty = 0 ;
113
113
114
- foreach ($ this -> matrix -> matrix () as $ y => $ row ){
114
+ foreach ($ m as $ y => $ row ){
115
115
116
- if ($ y > $ this ->moduleCount - 2 ){
116
+ if ($ y > ( $ this ->moduleCount - 2 ) ){
117
117
break ;
118
118
}
119
119
120
120
foreach ($ row as $ x => $ val ){
121
121
122
- if ($ x > $ this ->moduleCount - 2 ){
122
+ if ($ x > ( $ this ->moduleCount - 2 ) ){
123
123
break ;
124
124
}
125
125
126
- $ count = 0 ;
127
-
128
- if ($ val >> 8 > 0 ){
129
- $ count ++;
130
- }
131
-
132
- if ($ this ->matrix ->check ($ y , $ x + 1 )){
133
- $ count ++;
134
- }
135
-
136
- if ($ this ->matrix ->check ($ y + 1 , $ x )){
137
- $ count ++;
138
- }
139
-
140
- if ($ this ->matrix ->check ($ y + 1 , $ x + 1 )){
141
- $ count ++;
126
+ if (
127
+ $ val === $ m [$ y ][$ x + 1 ]
128
+ && $ val === $ m [$ y + 1 ][$ x ]
129
+ && $ val === $ m [$ y + 1 ][$ x + 1 ]
130
+ ){
131
+ $ penalty ++;
142
132
}
143
-
144
- if ($ count === 0 || $ count === 4 ){
145
- $ penalty += 3 ;
146
- }
147
-
148
133
}
149
134
}
150
135
151
- return $ penalty ;
136
+ return 3 * $ penalty ;
152
137
}
153
138
154
139
/**
155
- * Checks if there are patterns that look similar to the finder patterns
140
+ * Checks if there are patterns that look similar to the finder patterns (1:1:3:1:1 ratio)
156
141
*
157
- * @return float
142
+ * @return int
158
143
*/
159
- protected function testLevel3 (): float {
160
- $ penalty = 0 ;
144
+ protected function testLevel3 (array $ m ): int {
145
+ $ penalties = 0 ;
161
146
162
- foreach ($ this -> matrix -> matrix () as $ y => $ row ){
147
+ foreach ($ m as $ y => $ row ){
163
148
foreach ($ row as $ x => $ val ){
164
149
165
- if ($ x <= $ this ->moduleCount - 7 ){
166
- if (
167
- $ this ->matrix ->check ($ x , $ y )
168
- && !$ this ->matrix ->check ($ x + 1 , $ y )
169
- && $ this ->matrix ->check ($ x + 2 , $ y )
170
- && $ this ->matrix ->check ($ x + 3 , $ y )
171
- && $ this ->matrix ->check ($ x + 4 , $ y )
172
- && !$ this ->matrix ->check ($ x + 5 , $ y )
173
- && $ this ->matrix ->check ($ x + 6 , $ y )
174
- ){
175
- $ penalty += 40 ;
176
- }
150
+ if (
151
+ ($ x + 6 ) < $ this ->moduleCount
152
+ && $ val
153
+ && !$ m [$ y ][$ x + 1 ]
154
+ && $ m [$ y ][$ x + 2 ]
155
+ && $ m [$ y ][$ x + 3 ]
156
+ && $ m [$ y ][$ x + 4 ]
157
+ && !$ m [$ y ][$ x + 5 ]
158
+ && $ m [$ y ][$ x + 6 ]
159
+ ){
160
+ $ penalties ++;
177
161
}
178
162
179
- if ($ y <= $ this ->moduleCount - 7 ){
180
- if (
181
- $ this ->matrix ->check ($ x , $ y )
182
- && !$ this ->matrix ->check ($ x , $ y + 1 )
183
- && $ this ->matrix ->check ($ x , $ y + 2 )
184
- && $ this ->matrix ->check ($ x , $ y + 3 )
185
- && $ this ->matrix ->check ($ x , $ y + 4 )
186
- && !$ this ->matrix ->check ($ x , $ y + 5 )
187
- && $ this ->matrix ->check ($ x , $ y + 6 )
188
- ){
189
- $ penalty += 40 ;
190
- }
163
+ if (
164
+ ($ y + 6 ) < $ this ->moduleCount
165
+ && $ val
166
+ && !$ m [$ y + 1 ][$ x ]
167
+ && $ m [$ y + 2 ][$ x ]
168
+ && $ m [$ y + 3 ][$ x ]
169
+ && $ m [$ y + 4 ][$ x ]
170
+ && !$ m [$ y + 5 ][$ x ]
171
+ && $ m [$ y + 6 ][$ x ]
172
+ ){
173
+ $ penalties ++;
191
174
}
192
175
193
176
}
194
177
}
195
178
196
- return $ penalty ;
179
+ return $ penalties * 40 ;
197
180
}
198
181
199
182
/**
200
183
* Checks if more than half of the modules are dark or light, with a larger penalty for a larger difference
201
184
*
202
185
* @return float
203
186
*/
204
- protected function testLevel4 ():float {
187
+ protected function testLevel4 (array $ m ):float {
205
188
$ count = 0 ;
206
189
207
- foreach ($ this -> matrix -> matrix () as $ y => $ row ){
190
+ foreach ($ m as $ y => $ row ){
208
191
foreach ($ row as $ x => $ val ){
209
- if ($ val >> 8 > 0 ){
192
+ if ($ val ){
210
193
$ count ++;
211
194
}
212
195
}
0 commit comments