]> gitweb.factorcode.org Git - factor.git/commitdiff
24-game: fixing invalid operator input handling in get-operator
authorBjörn Lindqvist <bjourne@gmail.com>
Thu, 16 Jun 2016 21:41:17 +0000 (23:41 +0200)
committerBjörn Lindqvist <bjourne@gmail.com>
Thu, 16 Jun 2016 21:41:17 +0000 (23:41 +0200)
extra/24-game/24-game-tests.factor
extra/24-game/24-game.factor

index daa93cb2253cd9ff9d1cca94900e579e5524de5c..d68b3896980d34dcc58cab32afd376a3b64a3f22 100644 (file)
@@ -1,6 +1,12 @@
-
-USING: 24-game sequences tools.test ;
-
+USING: 24-game io.streams.string kernel math sequences tools.test ;
 IN: 24-game.tests
 
 { t } [ make-24 first4 makes-24? ] unit-test
+
+{ f } [ (operators) "hello" find-operator ] unit-test
+
+{ + } [ "+" [ (operators) get-operator ] with-string-reader ] unit-test
+
+{ swap } [
+    "bad\ninput\nswap" [ (operators) get-operator ] with-string-reader
+] unit-test
index 1e204630a47ca881021efd5d46babf9758cc7257..fa13438081cd1cd710fb662bcdce64fb9013dca9 100644 (file)
@@ -1,6 +1,6 @@
 ! Copyright © 2008 Reginald Keith Ford II
 ! 24, the Factor game!
-USING: accessors backtrack combinators continuations formatting io
+USING: accessors backtrack combinators continuations formatting fry io
 kernel math prettyprint quotations random sequences shuffle ;
 IN: 24-game
 
@@ -38,13 +38,14 @@ CONSTANT: (operators) { + - * / rot swap q }
 : operators ( array -- operators )
     length 3 < [ \ rot (operators) remove ] [ (operators) ] if ;
 
-: find-operator ( string operators -- word/f )
-    [ name>> = ] with find nip ;
+: find-operator ( operators string -- word/f )
+    '[ name>> _ = ] find nip ;
 
 : get-operator ( operators -- word )
     dup "Operators: %u\n" printf flush
-    readln over find-operator dup
-    [ "Operator not found..." print get-operator ] unless nip ;
+    dup readln find-operator [ ] [
+        "Operator not found..." print get-operator
+    ] ?if ;
 
 : try-operator ( array -- array )
     [ pprint nl ]