Created
May 31, 2018 10:44
-
-
Save blongden/815268b474ab44684ab2a6b1869f1934 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
function shuffleBalls(): array | |
{ | |
$balls = range(1, 90); | |
shuffle($balls); | |
return $balls; | |
} | |
function drawBall(array $balls): array | |
{ | |
if (empty($balls)) { | |
return []; | |
} | |
return [array_shift($balls), $balls]; | |
} | |
function displayBall($ball, $balls): void | |
{ | |
$display = []; | |
if (count($balls) < 89) { | |
$display[] = ','; | |
} | |
$display[] = "$ball"; | |
if (count($balls) && count($balls) % 5 == 0) { | |
$display[] = "(".count($balls)." left)"; | |
} | |
fwrite(STDERR, join(" ", $display)); | |
} | |
function chance($percent) | |
{ | |
return rand(0,99) < $percent; | |
} | |
function createAnnouncement($ball) | |
{ | |
$statements = [ | |
1 => "kelly's eye", | |
11 => "legs eleven", | |
22 => "two little ducks", | |
33 => "all the threes", | |
44 => "droopy drawers", | |
50 => "it's a bulls eye!", | |
60 => "grandma's getting frisky", | |
69 => "meal for two", | |
83 => "stop farting!", | |
88 => "two fat ladies", | |
90 => "top of the shop" | |
]; | |
$say = []; | |
switch($ball) { | |
case isset($statements[$ball]) && chance(50): | |
$say[] = $statements[$ball]; | |
break; | |
case strlen((string) $ball) == 1: | |
$say[] = "on it's own"; | |
break; | |
default: | |
[$num1, $num2] = str_split((string) $ball); | |
$say[] = "$num1 and $num2"; | |
break; | |
} | |
$say[] = chance(25) ? "number $ball" : $ball; | |
return join(", ", $say); | |
} | |
function announce($ball, $balls) | |
{ | |
$announce = createAnnouncement($ball); | |
if(getenv("BINGO_DISPLAY")) { | |
printf("%s\n", $announce); | |
} else { | |
displayBall($ball, $balls); | |
shell_exec(join(" ", ["say", escapeshellarg($announce)])); | |
sleep(1); | |
} | |
} | |
function play() | |
{ | |
$balls = shuffleBalls(); | |
while(@[$ball, $balls] = drawBall($balls)) { | |
announce($ball, $balls); | |
} | |
} | |
play(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment