]> gitweb.factorcode.org Git - factor.git/commitdiff
minesweeper: reduce duplicated code in count-neighbors.
authorJohn Benediktsson <mrjbq7@gmail.com>
Wed, 7 Mar 2018 00:01:33 +0000 (16:01 -0800)
committerJohn Benediktsson <mrjbq7@gmail.com>
Wed, 7 Mar 2018 00:01:33 +0000 (16:01 -0800)
extra/minesweeper/minesweeper.factor

index 1a9c7062e2e218c7e326f255635ad616bb9c9db9..960a6baf35e570f5e9ab13389f0bbbfadfcc7a29 100644 (file)
@@ -45,17 +45,16 @@ TUPLE: cell #adjacent mined? state ;
 : place-mines ( cells n -- cells )
     [ 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 )
-    neighbors [
-        first2 [ + ] bi-curry@ bi* cell-at
-        [ mined?>> ] [ f ] if*
-    ] with with with count ;
+    [ mined?>> ] count-neighbors ;
 
 : adjacent-flags ( cells row col -- #mines )
-    neighbors [
-        first2 [ + ] bi-curry@ bi* cell-at
-        [ state>> +flagged+ = ] [ f ] if*
-    ] with with with count ;
+    [ state>> +flagged+ = ] count-neighbors ;
 
 :: each-cell ( ... cells quot: ( ... row col cell -- ... ) -- ... )
     cells [| row |