]> gitweb.factorcode.org Git - factor.git/commitdiff
numbers-game: simplify for readability.
authorJohn Benediktsson <mrjbq7@gmail.com>
Tue, 10 Sep 2019 11:52:59 +0000 (04:52 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Tue, 10 Sep 2019 11:52:59 +0000 (04:52 -0700)
extra/numbers-game/numbers-game.factor

index 6fae3d99c51b291f38eac2adff0842a323fb21ec..83dd42392f5e656b55e02addb546ca157772e103 100644 (file)
@@ -1,39 +1,23 @@
-USING: kernel math math.parser random io ;
+USING: combinators io kernel math.order math.parser random ;
 IN: numbers-game
 
-: printfl ( s -- )
-    print flush ;
-
-: writefl ( s -- )
-    write flush ;
-
-: read-number ( -- n )
-    readln string>number ;
-
 : guess-banner ( -- )
-    "I'm thinking of a number between 0 and 100." printfl ;
-
-: guess-prompt ( -- ) "Enter your guess: " writefl ;
-
-: too-high ( -- ) "Too high" printfl ;
-
-: too-low ( -- ) "Too low" printfl ;
-
-: correct ( -- ) "Correct - you win!" printfl ;
-
-: inexact-guess ( actual guess -- )
-     < [ too-high ] [ too-low ] if ;
+    "I'm thinking of a number between 0 and 100." print flush ;
 
-: judge-guess ( actual guess -- ? )
-    2dup = [ 2drop correct f ] [ inexact-guess t ] if ;
+: guess-number ( -- n )
+    "Enter your guess: " write flush readln string>number ;
 
-: number-to-guess ( -- n ) 100 random ;
+: correct? ( actual guess -- ? )
+    <=> {
+        { +lt+ [ "Too high" print flush f ] }
+        { +eq+ [ "Correct - you win!" print flush t ] }
+        { +gt+ [ "Too low" print flush f ] }
+    } case ;
 
 : numbers-game-loop ( actual -- )
-    dup guess-prompt read-number judge-guess
-    [ numbers-game-loop ] [ drop ] if ;
+    [ dup guess-number correct? not ] loop drop ;
 
 : numbers-game ( -- )
-    guess-banner number-to-guess numbers-game-loop ;
+    guess-banner 100 random numbers-game-loop ;
 
 MAIN: numbers-game