How to resolve the algorithm 24 game step by step in the Red programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm 24 game step by step in the Red 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 Red programming language
Source code in the red programming language
Red []
print "Evaluation from left to right with no precedence, unless you use parenthesis." print ""
a: "123456789"
guess: ""
valid: ""
sucess: false
random/seed now/time
loop 4 [append valid last random a]
print ["The numbers are: " valid/1 ", " valid/2 ", " valid/3 " and " valid/4]
sort valid
insert valid " "
expr: [term ["+" | "-"] expr | term]
term: [primary ["*" | "/"] term | primary]
primary: [some digit | "(" expr ")"]
digit: charset valid
while [not sucess] [
guess: ask "Enter your expression: "
if guess = "q" [halt]
numbers: copy guess
sort numbers
numbers: take/last/part numbers 4
insert numbers " "
either (parse guess expr) and (valid = numbers) [
repeat i length? guess [insert at guess (i * 2) " "]
result: do guess
print ["The result of your expression is: " result]
if (result = 24) [sucess: true]
][
print "Something is wrong with the expression, try again."
]
]
print "You got it right!"
Red [
Title: "24 Game"
Author: "gltewalt"
]
op: charset "*/+-"
term: [opt "(" num opt ")"]
valid-expression: [term op term op term op term]
explode: func [val][
extract/into val 1 c: copy []
]
check-expression: does [
if "q" = e: ask "Enter expression: " [halt]
either parse trim/all e valid-expression [
either 24 = m: math to-block form explode e [
print ["You got it!" m]
][
print ["Not quite correct. That's" m]]
][
print "Invalid expression."
]
]
main: does [
numbers: collect [loop 4 [keep random 9]]
num: charset form numbers
print [newline "Using the following numbers, enter an expression that equals 24: (pmdas)" numbers]
if none? attempt [check-expression][print "Invalid expression."]
]
forever [main]
You may also check:How to resolve the algorithm Percentage difference between images step by step in the Rust programming language
You may also check:How to resolve the algorithm Increment a numerical string step by step in the Io programming language
You may also check:How to resolve the algorithm Runtime evaluation/In an environment step by step in the Pike programming language
You may also check:How to resolve the algorithm Variadic function step by step in the BCPL programming language
You may also check:How to resolve the algorithm Execute Computer/Zero step by step in the Wren programming language