24 Point Poker Game Calculator
24 Poker Game Introduction
24 Poker Game is an easy and fun math games. 4 cards (exclude the kings) are randomly picked. And the player who solves the equation to 24 first wins. For example, if the 4 cards are 10, 10 and 5 5. The formula to 24 is: (5 * 5) - (10 / 10).You can use the Depth First Search Algorithm to check if 4 cards can make to 24. The following PHP uses the bruteforce algorithm to search for all possible solutions.
API (Application Programming Interface)
https://helloacm.com/api/24/?a=10&b=10&c=5&d=5It will return JSON-encoded data:
{"errorCode":0,"cnt":1,"result":["(5 * 5) - (10 \/ 10)"]}
Complete API Source Code (PHP)
/*
Author: https://helloacm.com/
Brute-force Algorithm
*/
$a = 0;
$b = 0;
$c = 0;
$d = 0;
if (isset($_GET['a'])) {
$a = (integer)($_GET['a']);
} else {
if (isset($_POST['a'])) {
$a = (integer)($_POST['a']);
}
}
if (isset($_GET['b'])) {
$b = (integer)($_GET['b']);
} else {
if (isset($_POST['b'])) {
$b = (integer)($_POST['b']);
}
}
if (isset($_GET['c'])) {
$c = (integer)($_GET['c']);
} else {
if (isset($_POST['c'])) {
$c = (integer)($_POST['c']);
}
}
if (isset($_GET['d'])) {
$d = (integer)($_GET['d']);
} else {
if (isset($_POST['d'])) {
$d = (integer)($_POST['d']);
}
}
function tryA($a, $b, $aa, $bb) {
$a = str_replace("=", "", $a);
$b = str_replace("=", "", $b);
$r = array();
$r["=" . pow($a, $b)] = "pow($aa, $bb)";
$r["=" . pow($b, $a)] = "pow($bb, $aa)";
if ($a != 0) $r["=" . ($b / $a)] = "$bb / $aa";
if ($b != 0) $r["=" . ($a / $b)] = "$aa / $bb";
$r["=" . ($a * $b)] = "$aa * $bb";
$r["=" . ($a - $b)] = "$aa - $bb";
$r["=" . ($b - $a)] = "$bb - $aa";
$r["=" . ($a + $b)] = "$aa + $bb";
return $r;
}
function tryB($a, $b, $c) {
$r = tryA($a, $b, $a, $b);
$rr = array();
foreach ($r as $x => $y) {
$try1 = tryA($x, $c, "($y)", $c);
$rr = array_merge($rr, $try1);
}
return $rr;
}
// ((($a, $b), $c), $d)
function tryC($a, $b, $c, $d) {
$r = tryB($a, $b, $c);
$rr = array();
foreach ($r as $x => $y) {
$try1 = tryA($x, $d, "($y)", $d);
if (array_key_exists("=24", $try1)) {
$rr[] = $try1["=24"];
}
}
return $rr;
}
// ($a, $b), ($c, $d)
function tryD($a, $b, $c, $d) {
$r = tryA($a, $b, $a, $b);
$q = tryA($c, $d, $c, $d);
$rr = array();
foreach ($r as $x => $y) {
foreach ($q as $u => $v) {
$try1 = tryA($x, $u, "($y)", "($v)");
if (array_key_exists("=24", $try1)) {
$rr[] = $try1["=24"];
}
}
}
return $rr;
}
function try24($a, $b, $c, $d) {
$rr = array();
// ($a, $b) and ($c, $d)
$rr = array_merge($rr, tryD($a, $b, $c, $d));
$rr = array_merge($rr, tryD($a, $c, $b, $d));
$rr = array_merge($rr, tryD($a, $d, $b, $c));
// ($c, $d) and ($a, $b)
$rr = array_merge($rr, tryD($b, $c, $a, $d));
$rr = array_merge($rr, tryD($b, $d, $a, $c));
$rr = array_merge($rr, tryD($c, $d, $a, $b));
// ((($a, $b), $c), $d)
$rr = array_merge($rr, tryC($a, $b, $c, $d));
$rr = array_merge($rr, tryC($a, $c, $b, $d));
$rr = array_merge($rr, tryC($a, $d, $b, $c));
$rr = array_merge($rr, tryC($b, $c, $a, $d));
$rr = array_merge($rr, tryC($b, $d, $a, $c));
$rr = array_merge($rr, tryC($c, $d, $b, $a));
// ((($a, $b), $d), $c)
$rr = array_merge($rr, tryC($a, $b, $d, $c));
$rr = array_merge($rr, tryC($a, $c, $d, $b));
$rr = array_merge($rr, tryC($a, $d, $c, $b));
$rr = array_merge($rr, tryC($b, $c, $d, $a));
$rr = array_merge($rr, tryC($b, $d, $c, $a));
$rr = array_merge($rr, tryC($c, $d, $a, $b));
return array_values(array_unique($rr));
}
if (($a > 0) && ($b > 0) && ($c > 0) && ($d > 0)) {
$data['errorCode'] = 0;
$result = try24($a, $b, $c, $d);
$data['cnt'] = count($result);
$data['result'] = $result;
} else {
$data['cnt'] = 0;
$data['errorCode'] = 1;
$data['result'] = "Invalid Inputs: $a $b $c $d";
}
header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json');
die(json_encode($data));
If $_GET parameter s is not specified, this API will use the $_POST variable s instead.
curl -X POST https://helloacm.com/api/24/ -d "a=1" -d "b=1" -d "c=2" -d "d=3"
API Servers to compute 24 Game
You can use the following API servers. All API calls are monitored and subjects to fair use policy.- Load Balancer: https://api.justyy.workers.dev/api/
