my shitty variable names probably don't help
if ($_POST['pucks'] && ctype_digit($_POST['pucks']) && $_POST['pucks'] > 0)
{
$p = $_POST['pucks']; // $p = input from form
$valid = array(6, 9, 20); // simple array of what's a valid purchase count
array_walk($valid, 'walk', $p); // array_walk (for each member of array, do this function) so for each value in $valid, call function 'walk' with variable $p
echo "lose";exit; // we didn't achieve win, so we must have achieved lose
}
function win()
{ echo "win"; exit; }
function walk($v, $k, $p)
{
echo $p . "<br />";
if ($p - $v === 0) // if p - current value from valid equals 0, we won
{
win();
}
else
{
$p -= $v; // p equals p minus current value from valid array
$valid = array(6, 9, 20); // redclared valid array for this scope // lazy
foreach($valid as $a => $b) // for each member in array $valid, use $a as array key, $b as array value
if ($p < $b) // attempt to save unncessary calls, not entirely sure if this works... but if $p is less than any one value in the valid array, unset that value so we don't call walk on it again
unset($valid[$a]); // cont from above
if (count($valid)) // // cont from above -- if we still have valid values, array walk them again
array_walk($valid, 'walk', $p);
}
}
so basically, it takes the inputed value, x, and subtracts 6, 9, and 20 from it
if x is still above 6, 9, 20, subtract appropiate numbers again
repeat as necessary (the recursive part of it)
eventually, we get to a point where if 6, 9, or 20 doesn't fit into x, we lost
if we get a 0, we won
None.