78 lines
2.4 KiB
Text
78 lines
2.4 KiB
Text
( 24-game
|
|
= m-w m-z 4numbers answer expr numbers
|
|
, seed get-random convertBinaryMinusToUnary
|
|
, convertDivisionToMultiplication isExpresssion reciprocal
|
|
. (seed=.!arg:(~0:~/#?m-w.~0:~/#?m-z))
|
|
& seed$!arg
|
|
& ( get-random
|
|
=
|
|
. 36969*mod$(!m-z.65536)+div$(!m-z.65536):?m-z
|
|
& 18000*mod$(!m-w.65536)+div$(!m-w.65536):?m-w
|
|
& mod$(!m-z*65536+!m-w.9)+1
|
|
)
|
|
& ( convertBinaryMinusToUnary
|
|
= a z
|
|
. @(!arg:%?a "-" ?z)
|
|
& str$(!a "+-1*" convertBinaryMinusToUnary$!z)
|
|
| !arg
|
|
)
|
|
& (reciprocal=.!arg^-1)
|
|
& ( convertDivisionToMultiplication
|
|
= a z
|
|
. @(!arg:?a "/" ?z)
|
|
& str$(!a "*reciprocal$" convertDivisionToMultiplication$!z)
|
|
| !arg
|
|
)
|
|
& ( isExpresssion
|
|
= A Z expr
|
|
. @( !arg
|
|
: ?A
|
|
("+"|"-"|"*"|"/")
|
|
( ?Z
|
|
& isExpresssion$!A
|
|
& isExpresssion$!Z
|
|
)
|
|
)
|
|
| !numbers:?A !arg ?Z
|
|
& !A !Z:?numbers
|
|
| ( @(!arg:"(" ?expr ")")
|
|
| @(!arg:(" "|\t) ?expr)
|
|
| @(!arg:?expr (" "|\t))
|
|
)
|
|
& isExpresssion$!expr
|
|
)
|
|
& out
|
|
$ "Enter an expression that evaluates to 24 by combining the following numbers."
|
|
& out$"You may only use the operators + - * /"
|
|
& out$"Parentheses and spaces are allowed."
|
|
& whl
|
|
' ( get-random$() get-random$() get-random$() get-random$
|
|
: ?4numbers
|
|
& out$!4numbers
|
|
& whl
|
|
' ( get'(,STR):?expr:~
|
|
& !4numbers:?numbers
|
|
& ~(isExpresssion$!expr&!numbers:)
|
|
& out
|
|
$ ( str
|
|
$ ( "["
|
|
!expr
|
|
"] is not a valid expression. Try another expression."
|
|
)
|
|
)
|
|
)
|
|
& !expr:~
|
|
& convertBinaryMinusToUnary$!expr:?expr
|
|
& convertDivisionToMultiplication$!expr:?expr
|
|
& get$(!expr,MEM):?answer
|
|
& out$(str$(!expr " = " !answer))
|
|
& !answer
|
|
: ( 24&out$Right!
|
|
| #&out$Wrong!
|
|
)
|
|
& out$"Try another one:"
|
|
)
|
|
& out$bye
|
|
)
|
|
& 24-game$(13.14)
|
|
& ;
|