How to resolve the algorithm 24 game step by step in the Picat programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm 24 game step by step in the Picat programming language
Table of Contents
Problem Statement
The 24 Game tests one's mental arithmetic.
Write a program that randomly chooses and displays four digits, each from 1 ──► 9 (inclusive) with repetitions allowed. The program should prompt for the player to enter an arithmetic expression using just those, and all of those four digits, used exactly once each. The program should check then evaluate the expression. The goal is for the player to enter an expression that (numerically) evaluates to 24.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm 24 game step by step in the Picat programming language
Source code in the picat programming language
import util.
main =>
play.
play =>
nl,
Digits = [random() mod 9 + 1 : _ in 1..4].sort(),
println("Enter \"q\" to quit"),
println("Enter \"!\" to request a new set of four digits."),
printf("Or enter expression using %w => ", Digits),
Exp = read_line().strip(),
evaluate(Digits,Exp).
evaluate(_Digits,"q") => halt.
evaluate(_Digits,"!") => play.
evaluate(Digits,Exp) =>
Operands = [to_int(D) : D in Exp, digit(D)].sort(),
(Digits !== Operands ->
println("You must use the given digits:" ++ to_string(Digits))
;
catch(Term = parse_term(Exp), Exception, println(Exception)),
Res is Term,
(Res =:= 24 ->
println("Good work!")
;
println("Wong expression")
)
),
play.
You may also check:How to resolve the algorithm Van Eck sequence step by step in the Comal programming language
You may also check:How to resolve the algorithm Random number generator (included) step by step in the Scala programming language
You may also check:How to resolve the algorithm Percolation/Site percolation step by step in the Sidef programming language
You may also check:How to resolve the algorithm Even or odd step by step in the LOLCODE programming language
You may also check:How to resolve the algorithm Reverse a string step by step in the Jsish programming language