How to resolve the algorithm 24 game step by step in the PHP programming language
How to resolve the algorithm 24 game step by step in the PHP 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 PHP programming language
==> Notes for the code and the AI responses...
- The first response didn't answer my question at all.
- The second response has some good details and gives us a good answer.
Here I will explain the code myself.
The code is written in PHP and it's a game where the user is given four digits and must use the +, -, *, and / operators to make the answer 24. The user can also enter 'q' to quit the game or '!' to generate a new set of four digits.
The code first defines a function called make_numbers()
which generates four random numbers in the range 1 to 9 and returns them as an array.
It then defines a function called play()
which takes the four numbers and the user's expression as input and returns the result of the expression.
The main game loop then begins, where the user is repeatedly prompted to enter an expression until they enter a valid expression that evaluates to 24. If the user enters 'q' the game quits, and if they enter '!' a new set of four numbers is generated. If the user enters an invalid expression, they are told that it is not valid and are prompted to enter another expression. If the user enters a valid expression that does not evaluate to 24, they are told that the result is not 24 and are prompted to enter another expression. If the user enters a valid expression that evaluates to 24, they are told that they have won and the game exits.
Here is a breakdown of the code:
- The
make_numbers()
function generates four random numbers in the range 1 to 9 and returns them as an array. - The
play()
function takes the four numbers and the user's expression as input and returns the result of the expression. - The main game loop begins, where the user is repeatedly prompted to enter an expression until they enter a valid expression that evaluates to 24.
- If the user enters 'q' the game quits.
- If the user enters '!' a new set of four numbers is generated.
- If the user enters an invalid expression, they are told that it is not valid and are prompted to enter another expression.
- If the user enters a valid expression that does not evaluate to 24, they are told that the result is not 24 and are prompted to enter another expression.
- If the user enters a valid expression that evaluates to 24, they are told that they have won and the game exits.
Source code in the php programming language
#!/usr/bin/env php
The 24 Game
Given any four digits in the range 1 to 9, which may have repetitions,
Using just the +, -, *, and / operators; and the possible use of
brackets, (), show how to make an answer of 24.
An answer of "q" will quit the game.
An answer of "!" will generate a new set of four digits.
Otherwise you are repeatedly asked for an expression until it evaluates to 24
Note: you cannot form multiple digit numbers from the supplied digits,
so an answer of 12+12 when given 1, 2, 2, and 1 would not be allowed.
<?php
while (true) {
$numbers = make_numbers();
for ($iteration_num = 1; ; $iteration_num++) {
echo "Expresion $iteration_num: ";
$entry = rtrim(fgets(STDIN));
if ($entry === '!') break;
if ($entry === 'q') exit;
$result = play($numbers, $entry);
if ($result === null) {
echo "That's not valid\n";
continue;
}
elseif ($result != 24) {
echo "Sorry, that's $result\n";
continue;
}
else {
echo "That's right! 24!!\n";
exit;
}
}
}
function make_numbers() {
$numbers = array();
echo "Your four digits: ";
for ($i = 0; $i < 4; $i++) {
$number = rand(1, 9);
// The check is needed to avoid E_NOTICE from PHP
if (!isset($numbers[$number])) {
$numbers[$number] = 0;
}
$numbers[$number]++;
print "$number ";
}
print "\n";
return $numbers;
}
function play($numbers, $expression) {
$operator = true;
for ($i = 0, $length = strlen($expression); $i < $length; $i++) {
$character = $expression[$i];
if (in_array($character, array('(', ')', ' ', "\t"))) continue;
$operator = !$operator;
if (!$operator) {
if (!empty($numbers[$character])) {
$numbers[$character]--;
continue;
}
return;
}
elseif (!in_array($character, array('+', '-', '*', '/'))) {
return;
}
}
foreach ($numbers as $remaining) {
if ($remaining > 0) {
return;
}
}
return eval("return $expression;");
}
?>
You may also check:How to resolve the algorithm Evaluate binomial coefficients step by step in the Lasso programming language
You may also check:How to resolve the algorithm Population count step by step in the AppleScript programming language
You may also check:How to resolve the algorithm GUI enabling/disabling of controls step by step in the V (Vlang) programming language
You may also check:How to resolve the algorithm Lucas-Lehmer test step by step in the Factor programming language
You may also check:How to resolve the algorithm Call an object method step by step in the OCaml programming language