Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
<?php
/*
Matt's Word Bingo generator script.
Copyright (C) 2017 Matthew D Brown
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
This code was published as part of a tutorial which can be seen here:
http://dev.authorbuzz.co.uk/2017/10/03/writers-word-bingo/
*/
// set up the random number generator
function make_seed() {
list($usec, $sec) = explode(' ', microtime());
return $sec + $usec * 1000000;
}
mt_srand(make_seed()); // fresh randomness
// picker for getting items from the list
function get_some($n,$what,$fallback='writer'){
global $bingo;
if($n>_SLOT_SIZE_){
$n=_SLOT_SIZE_;
} // sanity check
if(isset($bingo[$what])){
$list = $bingo[$what];
$max = count($list);
if($n>=$max){return $list;}
$reply = array();
$i=0;
while((count($reply)<$n)&&$i<_SLOT_SIZE_){
$reply[] = $list[mt_rand(0,count($list)-1)];
$reply=array_keys(array_flip($reply)); // remove duplicates
++$i;
}
return $reply;
}else{
if($what==$fallback){
return array(); // stopping cycle of 3
}
return get_some($n,$fallback);
}
}
function refill($grid,$s=0){
$number = count($grid);
++$s;
if($s<_SLOT_SIZE_ && $number<_SLOT_SIZE_){
$n = _SLOT_SIZE_-$number;
++$n;
$more = get_some($n,'snv','rares');
$grid = array_merge($grid,$more);
#$grid = $grid+$more;
$grid = array_keys(array_flip($grid));
$grid = refill($grid,$s); // stopping cycle of slotsize+1
}
return $grid;
}
function sizeMe($text){
$size = strlen($text);
if($size > 40){
return "12pt";
}
if($size > 35){
return "14pt";
}
if($size > 30){
return "16pt";
}
if($size > 25){
return "18pt";
}
if($size > 15){
return "22pt";
}
if($size > 8){
return "28pt";
}
if($size > 4){
return "32pt";
}
return "38pt";
}
// 5*5 = 25 -1 =24
define('_SLOT_SIZE_',24);
$bingo = array();
// I chose writer as the fallback default in my functions.
// feel free to pick a different category but remember to change
// the functions to match.
$bingo['writer'][] = 'First Person';
$bingo['writer'][] = 'Third Person';
$bingo['writer'][] = 'Past Tense';
$bingo['writer'][] = 'Description';
$bingo['writer'][] = 'Hook';
$bingo['writer'][] = 'Main Character';
$bingo['writer'][] = 'Protagonist';
$bingo['writer'][] = 'Chapter';
$bingo['writer'][] = 'Antagonist';
$bingo['writer'][] = 'Begining';
$bingo['writer'][] = 'Middle';
$bingo['writer'][] = 'End';
$bingo['writer'][] = 'Beta Reader';
// add as many mroe as you want
#$bingo['writer'][] = '';
$bingo['rares'][] = 'Ice Breaker';
$bingo['rares'][] = 'Event';
$bingo['rares'][] = 'Next Week';
$bingo['rares'][] = 'Homework';
#$bingo['rares'][] = '';
$bingo['rude'][] = 'Someone swears';
$bingo['rude'][] = 'Bent';
$bingo['rude'][] = 'Attracted';
$bingo['rude'][] = '"Oh, yes?"';
$bingo['rude'][] = 'Innuendo';
$bingo['rude'][] = 'Pants';
#$bingo['rude'][] = '';
$bingo['snv'][] = 'Simon threatens to shave Matt';
$bingo['snv'][] = 'Childish';
$bingo['snv'][] = '"Simon, Shut Up"';
$bingo['snv'][] = 'Tea';
$bingo['snv'][] = 'Sugar';
$bingo['snv'][] = 'Cheese';
$bingo['snv'][] = 'Food';
$bingo['snv'][] = 'Milk';
$bingo['snv'][] = 'Coffee';
$bingo['snv'][] = 'Cat';
#$bingo['snv'][] = '';
$bingo['authors'][] = 'Terry Pratchett';
$bingo['authors'][] = 'Douglas Adams';
$bingo['authors'][] = 'Neil Gaiman';
$bingo['authors'][] = 'Iain (M) Banks';
$bingo['authors'][] = 'Philip K. Dick';
$bingo['authors'][] = 'Stephen King';
$bingo['authors'][] = 'Matthew (D) Brown';
$bingo['authors'][] = 'J. K. Rowling';
#$bingo['authors'][] = '';
// the categories can be whatever you want
#$bingo['theme'][] = '';
// select your chosen categories
$writer = get_some(12,'writer');
$authors = get_some(1,'authors','writer');
$crude = get_some(rand(2,5),'rude','rares');
$rares = get_some(rand(2,4),'rares','authors');
$snv = get_some(rand(3,5),'snv','rude');
$grid = array();
// merge in all of your selections here
$grid = array_merge($writer,$authors,$crude,$rares,$snv);
$grid = array_keys(array_flip($grid)); // remove duplicates
$grid = refill($grid); // fill any gaps
// randomise the order
shuffle($grid);
echo "<table id='bingotable'>";
for ($x = 0; $x < 5; $x++) {
echo '<tr>';
for ($y = 0; $y < 5; $y++) {
if($x==2&&$y==2){
$free = 'Word';
$size = sizeMe($free);
echo '<td id="bingo"><span style="font-size:'.$size.';">Word Bingo</span></td>';
}else{
$item = array_shift($grid);
$size = sizeMe($item);
echo "<td><span class='item xy__{$x}_{$y}' style='font-size:{$size};'>{$item}</span></td>";
}
}
echo '</tr>';
}
echo "</table>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment