minesweeper: reduce duplicated code in count-neighbors.
parent
02e231633e
commit
2dc44cd360
|
@ -45,17 +45,16 @@ TUPLE: cell #adjacent mined? state ;
|
||||||
: place-mines ( cells n -- cells )
|
: place-mines ( cells n -- cells )
|
||||||
[ dup unmined-cell t >>mined? drop ] times ;
|
[ dup unmined-cell t >>mined? drop ] times ;
|
||||||
|
|
||||||
|
:: count-neighbors ( cells row col quot: ( cell -- ? ) -- n )
|
||||||
|
cells neighbors [
|
||||||
|
first2 [ row + ] [ col + ] bi* cell-at quot [ f ] if*
|
||||||
|
] with count ; inline
|
||||||
|
|
||||||
: adjacent-mines ( cells row col -- #mines )
|
: adjacent-mines ( cells row col -- #mines )
|
||||||
neighbors [
|
[ mined?>> ] count-neighbors ;
|
||||||
first2 [ + ] bi-curry@ bi* cell-at
|
|
||||||
[ mined?>> ] [ f ] if*
|
|
||||||
] with with with count ;
|
|
||||||
|
|
||||||
: adjacent-flags ( cells row col -- #mines )
|
: adjacent-flags ( cells row col -- #mines )
|
||||||
neighbors [
|
[ state>> +flagged+ = ] count-neighbors ;
|
||||||
first2 [ + ] bi-curry@ bi* cell-at
|
|
||||||
[ state>> +flagged+ = ] [ f ] if*
|
|
||||||
] with with with count ;
|
|
||||||
|
|
||||||
:: each-cell ( ... cells quot: ( ... row col cell -- ... ) -- ... )
|
:: each-cell ( ... cells quot: ( ... row col cell -- ... ) -- ... )
|
||||||
cells [| row |
|
cells [| row |
|
||||||
|
|
Loading…
Reference in New Issue