-
-
Save oelna/60e6a1e59fe89fccff1e93791e3a086b to your computer and use it in GitHub Desktop.
<?php | |
/* | |
Generate however many unique bingo cards you require (should be fewer than 10k though). | |
This is not meant to compute ALL possible bingo cards, obviously, but to help set up a | |
nice amount of cards for play. | |
If you require a free space somewhere on the board, you should probably tweak the output, | |
not the generator. | |
I wrote this because existing solutions on stackoverflow were in languages I could not | |
test as easily and many existing generator websites generate crap cards that do not | |
adhere to the bingo number distribution in the 5 rows, so you spend an eternity looking | |
up your numbers. | |
I hope this helps somebody. Let me know if you find it useful or you know how to improve | |
the code. If you can spare a dollar or two, I appreciate gifts! https://paypal.me/oelna | |
Created: April 4, 2017 | |
Updated: June 6, 2019 | |
By: Arno Richter | |
URL: http://bingo.arnorichter.de/ | |
*/ | |
DEFINE('BR', "<br />\n"); | |
$number_of_cards = 60; // the amount of unique cards to generate. don't make too many! | |
$columns = array( | |
range(1,15), | |
range(16,30), | |
range(31,45), | |
range(46,60), | |
range(61,75) | |
); | |
$bingo_cards = array(); | |
$card_hashes = array(); | |
$i = 0; | |
/* GENERATE THE CARDS */ | |
while($i < $number_of_cards) { | |
$bingo_card = array(); | |
for($j=0; $j<5; $j++) { | |
$random_keys = array_rand($columns[$j], 5); | |
$random_values = array_intersect_key($columns[$j], array_flip($random_keys)); // http://stackoverflow.com/a/18047331/3625228 | |
$bingo_card = array_merge($bingo_card, $random_values); | |
} | |
// generate a unique hash for this card and compare it to the ones we already have | |
$card_hash = md5(json_encode($bingo_card)); // or whatever hashing algorithm is preferred | |
if(!in_array($card_hash, $card_hashes)) { | |
$bingo_cards[] = $bingo_card; | |
$card_hashes[] = $card_hash; | |
$i += 1; | |
} | |
if($i > 10000) break; // safety exit | |
} | |
/* OUTPUT, if needed (output with monospace font). could be made into an html table. */ | |
foreach($bingo_cards as $card) { | |
for($k=0; $k<(sizeof($card)/5); $k++) { | |
echo(str_pad($card[$k], 2, ' ', STR_PAD_LEFT).' | '); | |
echo($card[$k+5].' | '); | |
echo($card[$k+10].' | '); | |
echo($card[$k+15].' | '); | |
echo($card[$k+20].BR); | |
if($k < 4) echo(str_repeat('-', 22).BR); | |
} | |
echo(BR.BR); | |
} | |
?> |
Works great, but if you want real random values (currently they are sorted column by column), you should use shuffle()
instead:
$random_values = $columns[$j];
shuffle($random_values);
$bingo_card = array_merge($bingo_card, array_slice($random_values, 0, 5));
Seems like the script doesn't add the $card_hash to $card_hashes, so probably add $card_hashes[] = $card_hash; after line 50.
Seems like the script doesn't add the $card_hash to $card_hashes, so probably add $card_hashes[] = $card_hash; after line 50.
@prispens You are of course correct! I'm such a doofus, making the hashing solution in the first place and never implementing it correctly. Thanks!
@ydelwiche If I remember correctly (I'm sort of out of the game now, but I researched the rules somewhat back in 2017 when I made this), the "sorted by value" way is the "official" way Bingo cards should be done (to facilitate the checking of numbers), even though I can't find proof right now. Thanks for your contribution though! It feels well thought out.
Quick question, I'd like to be able to generate the same set of cards given a seed, is that possible to update this algorithm to allow for a seed?
I'm sorry, I have not built in any support for seeds. You are on your own on this. Good luck! If anything comes of it, you're welcome to share the results, though!
Hi there the code seems to work perfectly however in your example the numbers are formatted in tables with instructions and are sized A4, adding the code above to my page generates beautifully but the formatting is missing - do you have a code example with the formatting, please?
If this helps you, I would love to hear from you. How did you use it, what did you change?
Btw: I just let the script generate 10000 cards and it took about 5-10 seconds, so not the biggest deal.