24-game: fixing invalid operator input handling in get-operator
parent
eeaffb14b8
commit
1df34203e6
|
@ -1,6 +1,12 @@
|
||||||
|
USING: 24-game io.streams.string kernel math sequences tools.test ;
|
||||||
USING: 24-game sequences tools.test ;
|
|
||||||
|
|
||||||
IN: 24-game.tests
|
IN: 24-game.tests
|
||||||
|
|
||||||
{ t } [ make-24 first4 makes-24? ] unit-test
|
{ 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
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
! Copyright © 2008 Reginald Keith Ford II
|
! Copyright © 2008 Reginald Keith Ford II
|
||||||
! 24, the Factor game!
|
! 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 ;
|
kernel math prettyprint quotations random sequences shuffle ;
|
||||||
IN: 24-game
|
IN: 24-game
|
||||||
|
|
||||||
|
@ -38,13 +38,14 @@ CONSTANT: (operators) { + - * / rot swap q }
|
||||||
: operators ( array -- operators )
|
: operators ( array -- operators )
|
||||||
length 3 < [ \ rot (operators) remove ] [ (operators) ] if ;
|
length 3 < [ \ rot (operators) remove ] [ (operators) ] if ;
|
||||||
|
|
||||||
: find-operator ( string operators -- word/f )
|
: find-operator ( operators string -- word/f )
|
||||||
[ name>> = ] with find nip ;
|
'[ name>> _ = ] find nip ;
|
||||||
|
|
||||||
: get-operator ( operators -- word )
|
: get-operator ( operators -- word )
|
||||||
dup "Operators: %u\n" printf flush
|
dup "Operators: %u\n" printf flush
|
||||||
readln over find-operator dup
|
dup readln find-operator [ ] [
|
||||||
[ "Operator not found..." print get-operator ] unless nip ;
|
"Operator not found..." print get-operator
|
||||||
|
] ?if ;
|
||||||
|
|
||||||
: try-operator ( array -- array )
|
: try-operator ( array -- array )
|
||||||
[ pprint nl ]
|
[ pprint nl ]
|
||||||
|
|
Loading…
Reference in New Issue