Written by: NetworkError, on 24-10-2008 09:36
Last update: 01-05-2009 08:47
Published in: Public,
Technical Wootness
Views: 1446
Some friends of mine recently came up with some great General Conference Bingo cards. Hilarity ensued. The only problem was there were only 4 cards. This got me thinking, "how hard would it be to write a random Bingo card generator?" The answer - kinda' hard but not really. So I whipped a little something up over my lunch hour. It's nothing too fancy, but it was a fun problem-solving challenge.
Features:
I wanted some extra whistles and bells with my Bingo card generator.
- Variable width
- Variable height
- Variable "FREE" square in the middle (Calculated from the width and height settings)
- Variable word list
- Intelligent "no repeat word" logic (Only re-use words when the word list has been used up.)
- Generate CSV or HTML
Try out the DIY Bing Card Generator.
The Bingo Cards Class:
The class that does all the work is the Bingo class. The rest of the site is a page for setting up inputs and a page for passing the inputs to an instance of the class. Simple.
/**
* @file
* This class and file handle the actual building of your bingo cards.
**/
class Bingo
{
/**
* Width,Height
* @var int
**/
protected $width;
protected $height;
/**
* Free square in the middle?
* @var int
**/
protected $free = true;
/**
* Word list array.
* Exploded on \n
* @var array
**/
protected $list = array();
public function setList($list)
{
$this->list = explode("\n", $list);
if (is_array($this->list)) {
foreach ($this->list as &$item) {
$item = trim($item);
}
} else {
$this->list = array('Error: No list specified.');
}
}
/**
* Set width/height.
* @param int $width
* @param int $height
**/
public function setSize($width, $height)
{
$this->width = (int)$width;
$this->height = (int)$height;
}
/**
* Set free square to bool true/false.
* @param bool $free
**/
public function setFreeSquare($free)
{
$this->free = (bool)$free;
}
/**
* Return HTML table for bingo card.
* @return HTML for bingo card.
**/
public function generateHTML()
{
$array = $this->generateArray();
if (!$array)
return 'No list specified.';
$html = '
';
$count = count($array);
for ($i = 0; $i < $count; $i++) {
$html .= ''; $html .= implode(' | ', $array[$i]); $html .= ' |
';
}
$html .= '
';
return $html;
}
/**
* Return CSV data for bingo card.
* @return CSV for bingo card.
**/
public function generateCSV()
{
$array = $this->generateArray();
if (!$array)
return 'No list specified.';
$html = '# Begin card
';
$count = count($array);
for ($i = 0; $i < $count; $i++) {
$html .= implode(',', $array[$i]);
$html .= '
';
}
return $html;
}
/**
* Return a bingo array.
* @return array(
* row_number => array(
* col_number => cell 1x1,
* col_number => cell 1x2,
* ...
* ),
* ...
* );
**/
public function generateArray()
{
if (count($this->list) == 0) {
return false;
}
// Figure out where the middle square is if it's "Free".
if ($this->free) {
$mid_height = round(($this->height / 2) - .1);
$mid_width = round(($this->width / 2) - .1);
}
$array = array();
$used_keys = array();
for ($row = 0; $row < $this->height; $row++) {
for ($col = 0; $col < $this->width; $col++) {
// Free square?
if ($this->free && $mid_height == $row && $mid_width == $col) {
$array[$row][$col] = 'FREE';
} else {
// Don't lock up because the used_keys array is full.
if (count($this->list) == count($used_keys)) {
$used_keys = array();
}
do {
$key = array_rand($this->list);
} while (in_array($key, $used_keys));
$used_keys[] = $key;
$array[$row][$col] = $this->list[$key];
}
}
}
return $array;
}
}