No no. return $map[$yCur-1][$xCur-1] eq $wall; is fine.
But, ouch. You're right about my simplification with grep, I looked far too briefly. Now let's see.. I always get confused by what conditions to use for grep in boolean context, so I'll think aloud.
!$cond1 and !$cond2 means that neither of them must be true. So the number of matches when testing for truth must be zero. That's not grep { $_ } $cond1, $cond2. So I was using the wrong one to begin with.
And I also overlooked that you weren't simply testing all coordinates for falseness - unraveled, it looks like this:
That looks decidedly more complex than a simple grep. But since the inner parens only contain &&s, the first two can be combined:if ( ( !&test2($xCur, $yCur, $wall) && !&test3($xCur, $yCur, $wall) && &test5($xCur, $yCur, $wall) && &test6($xCur, $yCur, $wall) && !&test9($xCur, $yCur, $wall) ) && ( !&test1($xCur, $yCur, $wall) && !&test7($xCur, $yCur, $wall) ) || ( &test1($xCur, $yCur, $wall) && &test4($xCur, $yCur, $wall) && &test7($xCur, $yCur, $wall) && !&test8($xCur, $yCur, $wall) ) )
Now that looks almost like a datastructure, so let's make it one.if ( ( !&test2($xCur, $yCur, $wall) && !&test3($xCur, $yCur, $wall) && &test5($xCur, $yCur, $wall) && &test6($xCur, $yCur, $wall) && !&test9($xCur, $yCur, $wall) && !&test1($xCur, $yCur, $wall) && !&test7($xCur, $yCur, $wall) ) || ( &test1($xCur, $yCur, $wall) && &test4($xCur, $yCur, $wall) && &test7($xCur, $yCur, $wall) && !&test8($xCur, $yCur, $wall) ) )
Now we have good reason to modify the behaviour of the check_coordinates function: it isn't returning true/false, it's returning a comparison value. So have it return 0 if $lots_of_checks; and return 1 if $map[$y][$x] eq $wall;. Then we can rewrite that if as:my %test1 = ( 2 => 0, 3 => 0, 5 => 1, 6 => 1, 9 => 0, 1 => 0, 7 => 0, ); my %test2 = ( 1 => 0, 4 => 0, 7 => 1, 8 => 0, );
That looks bulky, but remember it's as bulky as it's going to get. If you need to add more conditions, you add them in the hashes, not the if. And rather than %test1 and %test2 you should use meaningful names - maybe %corner_pixels f.ex or some such, so your variables express your intent.if( not(grep { check_coord($xCur, $yCur, $wall, $_) ne $test1{$_} } ke +ys %test1) or not(grep { check_coord($xCur, $yCur, $wall, $_) ne $test2{$_} } + keys %test2) ) { }
Makeshifts last the longest.
In reply to Re^3: large expression formating
by Aristotle
in thread large expression formating
by Dr.Altaica
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |