Slightlly OT - Bingo problem - Page 3

•  Subject
• Author
• Posted on

Re: Slightlly OT - Bingo problem

I know this thread is old - but I don't get over here much, and this
presented an interesting challange.

It is possible to get random cards with no collision or overflow
detection if you shuffle properly.  Try this code (sorry for any word
wrap):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
<title>Bingo Card</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
table {margin: 10px auto; border: 2px solid black }
caption { margin: 0 auto }
td { width: 30px; text-align: center; border: 1px solid black }
</style>

<body>
<?

srand((float)microtime() * 1000000);

// Create and fill the initial array of numbers
\$numbers = range(1, 90);
for (\$i = 0; \$i < 90; \$i++)
\$numbers[\$i] = \$i+1;
shuffle(\$numbers);

// Create the bingo card
//\$card = array(array(), array(), array());  // 3 rows
\$cards = array( array(array(), array(), array()),  // 3 rows per card
array(array(), array(), array()),
array(array(), array(), array()),
array(array(), array(), array()),
array(array(), array(), array()),
array(array(), array(), array()) ); // 6 cards

for (\$card = 0; \$card < 6; \$card++) {  // For each card
for (\$row = 0; \$row < 3; \$row++) {   // Each row in a card
for (\$cell = 0; \$cell < 5; \$cell++)  // First 5 boxes in each row
\$cards[\$card][\$row][\$cell] = array_pop(\$numbers);

for (\$cell = 5; \$cell < 9; \$cell++)
\$cards[\$card][\$row][\$cell] = '&nbsp;';  // Empty the rest of each
row

shuffle(\$cards[\$card][\$row]);  // Shuffle the row contents

for (\$cell1 = 0; \$cell1 < 9; \$cell1++) // Finally sort the numbers
in the row
for (\$cell2 = \$cell1+1; \$cell2 < 9; \$cell2++)  // Use a simple
swap sort since it's a small array
if (is_numeric(\$cards[\$card][\$row][\$cell1]) &&
is_numeric(\$cards[\$card][\$row][\$cell2])) // Ignore blank boxes
if (\$cards[\$card][\$row][\$cell1] > \$cards[\$card][\$row][\$cell2])
{
\$temp = \$cards[\$card][\$row][\$cell1];
\$cards[\$card][\$row][\$cell1] = \$cards[\$card][\$row][\$cell2];
\$cards[\$card][\$row][\$cell2] = \$temp;
}
}
}
// Now display the cards
for (\$card = 0; \$card < 6; \$card++) {
echo "<table>\n" .
'<caption>Bingo Card ' . (\$card+1) . "</caption>\n";
for (\$row = 0; \$row < 3; \$row++) {
echo "<tr>\n";
for (\$cell = 0; \$cell < 9; \$cell++)
echo '<td>' . \$cards[\$card][\$row][\$cell] . "</td>\n";
echo "</tr>\n";
}
echo "</table>\n";
}
?>

</body>
</html>

--

To reply, delete the 'x' from my email
Jerry Stuckle,
JDS Computer Training Corp.
jstucklex@attglobal.net
Member of Independent Computer Consultants Association - www.icca.org

Re: Slightlly OT - Bingo problem

-snip-

Certainly is

http://www.darrylcatchpole.net/bingo.phps

<pre>
<?php

class cRow {
var    \$aNumbers = array();
var    \$aBag = array();

function cRow(\$min, \$max) {
mt_srand(mktime());
/*
place all numbers into a bag
*/
\$this->aBag = range(\$min, \$max);
/*
check the number of counters in bag as 1 - 9 is
lower the 10 - 19
*/
\$counters = \$max - \$min;
for(\$i=\$counters;\$i>0;\$i--)
{
/*
draw a random number out of the bag
*/
\$n = mt_rand(0 , \$i);
\$this->aNumbers[] = \$this->aBag[\$n];
/*
remove from bag
*/
unset(\$this->aBag[\$n]);
sort(\$this->aBag);
}
// add last number
\$this->aNumbers[] = array_pop(\$this->aBag);
}
}

\$aCard = array(
new cRow(1 , 9),
new cRow(10 , 19),
new cRow(20 , 29),
new cRow(30 , 39),
new cRow(40 , 49),
new cRow(50 , 59),
new cRow(60 , 69),
new cRow(70 , 79),
new cRow(80 , 89)
);

print_r(\$aCard);

?>
</pre>

np :)

Re: Slightlly OT - Bingo problem

Llewellyn contained the following:

Sorry CJ, but unless I'm missing something, you haven't got a handle on
the problem.
See www.ckdog.co.uk/php/test/bingo.php

Each card has 9 columns and 3 rows
Each row contains 5 numbers and 4 spaces
Each column can contain 0-3 numbers
Numbers are ordered on the cards Column 1 contains 1-9 col2 10-19 ...
col8 70-79, col8 80-90
There are six cards
All numbers from 1-90 must be used just once.

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker /

Re: Slightlly OT - Bingo problem

I've got a perfect handle on the problem. You asked for an algorythm that
would randomise 90 numbers into a matrix without using brute force.

All numbers from 1 to 90 are stored in seperate arrays all in random order.

All I have not done is written code to extract them into six cards which I
shall leave to the reader who should be capable of doing so. ;-)

Re: Slightlly OT - Bingo problem

Geoff Berrow wrote:
<snip>

<snip>

that

Without brute-force collision detection...

<?php
for(\$i=0; \$i<9 ; ++\$i)
{
\$rand_tbl[\$i] = range(\$i*10 + 1, \$i*10 + 9);
shuffle(\$rand_tbl[\$i]);
}
//print_r(\$rand_tbl);
for(\$row=0; \$row<3; ++\$row)
{
\$card_arr[\$row] = array_fill(0, 9, '');
\$handle = range(0, 8);
shuffle(\$handle);
for(\$col=0; \$col<5; ++\$col)
\$card_arr[\$row][\$handle[\$col]] =
array_pop(\$rand_tbl[\$handle[\$col]]);
}
//print_r(\$card_arr);
echo '<pre>'."\n";
for(\$row=0; \$row<3; ++\$row)
{
for(\$col=0; \$col<9; ++\$col)
echo "\n";
}
echo '</pre>'."\n";
?>

Obviously, there must be some better algorithms.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com    Blog: http://rajeshanbiah.blogspot.com /

Re: Slightlly OT - Bingo problem

R. Rajesh Jeba Anbiah wrote:

will

<snip previous inaccurate code that didn't account 10,20,30..80>
Fixed...

<?php
\$rand_tbl[0] = range(1, 9);
shuffle(\$rand_tbl[0]);
for(\$i=1; \$i<9 ; ++\$i)
{
\$rand_tbl[\$i] = range(\$i*10, \$i*10 + 9);
shuffle(\$rand_tbl[\$i]);
}
//print_r(\$rand_tbl);
for(\$row=0; \$row<3; ++\$row)
{
\$card_arr[\$row] = array_fill(0, 9, '');
\$handle = range(0, 8);
shuffle(\$handle);
for(\$col=0; \$col<5; ++\$col)
\$card_arr[\$row][\$handle[\$col]] =
array_pop(\$rand_tbl[\$handle[\$col]]);
}
//print_r(\$card_arr);
echo '<pre>'."\n";
for(\$row=0; \$row<3; ++\$row)
{
for(\$col=0; \$col<9; ++\$col)
echo "\n";
}
echo '</pre>'."\n";
?>

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com    Blog: http://rajeshanbiah.blogspot.com /

Re: Slightlly OT - Bingo problem

Geoff Berrow wrote:
<snip>

<snip>

that

Without brute-force collision detection...

<?php
for(\$i=0; \$i<9 ; ++\$i)
{
\$rand_tbl[\$i] = range(\$i*10 + 1, \$i*10 + 9);
shuffle(\$rand_tbl[\$i]);
}
//print_r(\$rand_tbl);
for(\$row=0; \$row<3; ++\$row)
{
\$card_arr[\$row] = array_fill(0, 9, '');
\$handle = range(0, 8);
shuffle(\$handle);
for(\$col=0; \$col<5; ++\$col)
\$card_arr[\$row][\$handle[\$col]] =
array_pop(\$rand_tbl[\$handle[\$col]]);
}
//print_r(\$card_arr);
echo '<pre>'."\n";
for(\$row=0; \$row<3; ++\$row)
{
for(\$col=0; \$col<9; ++\$col)
echo "\n";
}
echo '</pre>'."\n";
?>

Obviously, there must be some better algorithms.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com    Blog: http://rajeshanbiah.blogspot.com /

Re: Slightlly OT - Bingo problem

R. Rajesh Jeba Anbiah wrote:

<snip triple post>

Apologies for the triple post. Stupid GG beta was keep on timouting
the posts but finally posted them.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com    Blog: http://rajeshanbiah.blogspot.com /

Re: Slightlly OT - Bingo problem

Geoff Berrow wrote:
<snip>

<snip>

that

Without brute-force collision detection...

<?php
for(\$i=0; \$i<9 ; ++\$i)
{
\$rand_tbl[\$i] = range(\$i*10 + 1, \$i*10 + 9);
shuffle(\$rand_tbl[\$i]);
}
//print_r(\$rand_tbl);
for(\$row=0; \$row<3; ++\$row)
{
\$card_arr[\$row] = array_fill(0, 9, '');
\$handle = range(0, 8);
shuffle(\$handle);
for(\$col=0; \$col<5; ++\$col)
\$card_arr[\$row][\$handle[\$col]] =
array_pop(\$rand_tbl[\$handle[\$col]]);
}
//print_r(\$card_arr);
echo '<pre>'."\n";
for(\$row=0; \$row<3; ++\$row)
{
for(\$col=0; \$col<9; ++\$col)
echo "\n";
}
echo '</pre>'."\n";
?>

Obviously, there must be some better algorithms.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com    Blog: http://rajeshanbiah.blogspot.com /