24 game: Difference between revisions

m (GOTO reference changed at end of game, to ensure control returns to correct beginning.)
Line 8,022:
 
=={{header|PicoLisp}}==
<lang PicoLisp>(de checkExpression (Lst Exe)
(de random4digits ()
(make
(when (diff Lst (fish num? Exe))
(link "Not all numbers used" ) )
(when (diff (fish num? Exe) Lst)
(link "Using wrong number(s)") )
(when (diff (fish sym? Exe) '(+ - * /))
(link "Using illegal operator(s)") ) ) )
 
(loop
(setq Numbers (make (do 4 (link (rand 1 9)))))
(prinlNumbers )
"Please enter a Lisp expression using (, ), +, -, *, / and "
(glue ", " Numbers) )
(prin "Or a single dot '.' to stop: ")
(T (= "." (setq Reply (catch '(NIL) (in NIL (read)))))
(bye) )
(cond
((str? Reply)
(prinl "-- Input error: " Reply) )
((checkExpression Numbers Reply)
(prinl "-- Illegal Expression")
(for S @
(space 3)
(prinl S) ) )
((str? (setq Result (catch '(NIL) (eval Reply))))
(prinl "-- Evaluation error: " @) )
((= 24 Result)
(prinl "++ Congratulations! Correct result :-)") )
(T (prinl "Sorry, this gives " Result)) )
(prinl) )</lang>
{{out}}
<pre>Please enter a Lisp expression using (, ), +, -, *, / and 1, 3, 3, 5
Or a single dot '.' to stop: (* (+ 3 1) (+ 5 1))
++ Congratulations! Correct result :-)
 
(de prompt ()
Please enter a Lisp expression using (, ), +, -, *, / and 8, 4, 7, 1
(prinl "Please enter a 'Lisp' expression that equals 24 using (, ), +, -, *, / and " Numbers) )
Or a single dot '.' to stop: (* 8 (% 7 3) 9)
-- Illegal Expression
Not all numbers used
Using wrong number(s)
Using illegal operator(s)
 
(de getinput ()
Please enter a Lisp expression using (, ), +, -, *, / and 4, 2, 2, 3
(T (= "." (setq ReplyExpression (catch '(NIL)’NIL (in NIL (read)))))
Or a single dot '.' to stop: (/ (+ 4 3) (- 2 2))
-- Illegal Expression )
-- Evaluation error: Div/0
 
(de checkexpression (Numbers Expression)
Please enter a Lisp expression using (, ), +, -, *, / and 8, 4, 5, 9
(make
Or a single dot '.' to stop: .</pre>
(when (diff LstNumbers (fish num? ExeExpression))
(link "Not all numbers used." ) )
(when (diff (fish num? ExeExpression) LstNumbers)
(link "Using wrong number(s).") )
(when (diff (fish sym? ExeExpression) '(+ - * /))
(link "Using illegal operator(s).") ) ) )
 
(de loopuntilquit ()
(prinl "Example 'Lisp' expression: (+ (* 4 5) (- 1 2))")
(loop
(set 'Numbers (random4digits))
(bye) prompt)
(set 'Expression (getinput))
(if (= Expression ".") (prog (prinl "bye!") (bye)))
(set 'Check (checkexpression Numbers Expression))
(if (str?car ReplyCheck)
(mapcar (prinl S) ) Check)
(for S @prog
(prinlset "--'Evaluated Illegal(eval Expression"))
(spaceif 3(= Evaluated 24)
(prinl "++ Congratulations! Correct result :-)") )
(prinl "That evaluated to " Evaluated " Try again!") ) ) ) ) )
 
(loopuntilquit)
</lang>
{{out}}
<pre>
pil 24game.l +
Example 'Lisp' expression: (+ (* 4 5) (- 1 2))
"Please enter a 'Lisp' expression that equals 24 using (, ), +, -, *, / and "1329
(+ 1 3)
Not all numbers used.
<pre>Please enter a 'Lisp' expression that equals 24 using (, ), +, -, *, / and 1, 3, 3, 53758
(+ 3 9)
Not all numbers used.
Using wrong number(s).
Please enter a 'Lisp' expression that equals 24 using (, ), +, -, *, / and 8, 4, 7, 12713
(^ 2 7 1 3)
Using illegal operator(s).
Please enter a 'Lisp' expression that equals 24 using (, ), +, -, *, / and 4, 2, 2, 38367
(* (* 3 8) (- 7 6))
++ Congratulations! Correct result :-)
Please enter a 'Lisp' expression that equals 24 using (, ), +, -, *, / and 8, 4, 5, 91635
.
bye!
</pre>
 
=={{header|PL/I}}==
Anonymous user