Code Comments
Programming Forum and web based access to our favorite programming groups.Hi all. I thought I had fixed this, but I havent .. Using the following script .. at this address http://yidpows.freeserverhost.net/bingo2.php I have got the script to make the required six cards that have all the numbers (1-90 inclusive) randomly placed but in theri respective columns. However, if you click on the link, you will see that sometimes not all the numbers are used. As I am still new to php, I am wondering if their are any of these functions that could help me out. Thanks for any help, Scruffy. <?php srand ((float) microtime() * 10000000); for( $i=0; $i<=90; $i++ ) { global $numbers1; global $numbers2; global $numbers3; $numbers1[] = $i; $numbers2[] = $i; $numbers3[] = $i; } function drawtable($passed) { global $numbers1; global $numbers2; global $numbers3; print "<tr>"; unset( $passed[0] ); for( $i=0; $i<5; $i++ ) { $name = array_rand( $passed ); $name2[] = $passed[$name]; unset( $numbers1[$name] ); unset( $numbers2[$name] ); unset( $numbers3[$name] ); if ($name < 10) { for( $j=0; $j<10; $j++ ) { unset( $passed[$j] ); } } else if ($name>=10 && $name <=79) { for( $loop=10; $loop<=70; $loop+=10 ) { for( $loop2=0; $loop2<=9; $loop2++ ) { if(($loop+$loop2)==$name) { for( $j=$loop; $j<=($loop+9); $j++ ) { unset( $passed[$j] ); } } } } } else { for( $j=80; $j<=90; $j++ ) { unset( $passed[$j] ); } } } sort( $name2 ); for( $loop=10; $loop<=90; $loop+=10 ) { print "<td width=\"20\" align=\"center\" bgcolor=\"white\">"; for( $i=0; $i<5; $i++ ) { $check = $name2[$i]; if ($check==90 && $loop==90) { print $check; } else if ($check<($loop) && $check>=($loop-10)) { print $check; } else { print ""; } } print " </td>"; } print "</tr>"; return($name2); } ?> <html> <head> <title>Bingo !!</title> </head> <body> <?php for ($z=1;$z<=6;$z++){ print"<table align=\"center\" border=\"1\" bgcolor=\"black\">"; drawtable($numbers1); // foreach( $numbers1 as $key => $value ){print $key." -key, $value -value - numbers1<br>\n";} drawtable($numbers2); // foreach( $numbers2 as $key => $value ){print $key." -key, $value -value - numbers2<br>\n";} drawtable($numbers3); // foreach( $numbers3 as $key => $value ){print $key." -key, $value -value - numbers3<br>\n";} print"</table>"; } // if ((count($numbers1)!=1) || (count($numbers2)!=1) || (count($numbers3)!=1)){ if ((count($numbers1)>1) || (count($numbers2)>1) || (count($numbers3)>1)){ print "arrgh .. bugger .. its broke , again<br>"; // print "<script>\n"; // print " document.location=\"http://yidpows.freeserverhost.net/bingo2.php\";"; // print "</script>\n"; } foreach( $numbers1 as $key => $value ){print $key." -key, $value -value - numbers1<br>\n";} foreach( $numbers2 as $key => $value ){print $key." -key, $value -value - numbers2<br>\n";} foreach( $numbers3 as $key => $value ){print $key." -key, $value -value - numbers3<br>\n";} ?> </body> </html>
Post Follow-up to this messageMany thanks Anthony. I am just looking at my mail in passing as ot were, but
I shall look at your code later tonight and implement it to see. Just for
clarification though, from an expert in bingo-ology (I made that word up,
and the expert is my wife (been playing bingo many years she has) !!! ) each
bingo card should each have 15 random numbers between 1 and 90 inclusive, in
their assigned columns(1-9,10-19,20-29 .... 709-79,80-90) and in a 'book' of
6 cards, all 90 numbers should appear once only ( in other words all 90
numbers somewhere on the 6 cards) but never a repetition of any numbers ....
( and I thought it was a simple game till I tried this !!)
Thanks again, will speak .. type later to let you know how it went .
All the best, Scruffy.
"Anthony Plunkett" <doobeh@thefort.org> wrote in message
news:41c5780a$0$25864$afc38c87@news.easynet.co.uk...
> Anthony Plunkett wrote:
>
>
>
> I noticed that you probably don't want totally random numbers per cell
> like in my last example (I've never played bingo!) if you want 90 unique
> numbers per card the following code is what would be better to look at:
>
> <?php
>
> #Number of cards wanted:
> $cards = 5;
> $columnsPerCard = 9;
> $rowsPerCard = 3;
>
> function uniqueRandomNumbers($columns,$rows) {
> #How many numbers do we need:
> $totalNumbersNeeded = $columns * $rows;
> #Create an array to hold them:
> $NumberArray = array();
> #Do following until we have the numbers we need:
> for ($a = 1; $a <= ($totalNumbersNeeded); $a++)
> {
> $randomNumber = rand(1,90);
> #If number is already in the array, try again (and
> #again)
> while(in_array($randomNumber,$NumberArra
y))
> {
> $randomNumber = rand(1,90);
> }
> #Put number in array:
> $NumberArray[] = $randomNumber;
> }
> #Return the array:
> return $NumberArray;
> }
>
>
> for ($i = 1; $i <= $cards; $i++) {
> #Get Random numbers for card:
> $randomNumbers = uniqueRandomNumbers($columnsPerCard,$row
sPerCard);
> #Initiate table:
> echo '<table border="1">';
> #Create Row:
> for ($k = 1; $k <= $rowsPerCard; $k++) {
> echo '<tr>';
>
> #Create column:
> for ($j = 1; $j <= $columnsPerCard; $j++) {
> #pop the last number off the array, and print
> #it:
> $randomNumber = array_pop($randomNumbers);
> echo '<td>'.$randomNumber.'</td>';
> }
> #Close Row
> echo '</tr>';
> }
> #Close Table and add a line spacer:
> echo '</table><br/>';
> }
> ?>
>
> Again, hope that helps :]
>
> Anthony.
Post Follow-up to this messageUnfortunately, its not what I meant, but I am looking at the code to learn more, so it wasn't a wasted effort, I do appreciate your help. If you click on this link ( http://yidpows.freeserverhost.net/bingo2.php ) you will see my version, which does not quite work properly, but you will get the jist of the card layout. Many thanks again, Scruffy. "Anthony Plunkett" <doobeh@thefort.org> wrote in message news:41c5780a$0$25864$afc38c87@news.easynet.co.uk... > Anthony Plunkett wrote: > > > > I noticed that you probably don't want totally random numbers per cell > like in my last example (I've never played bingo!) if you want 90 unique > numbers per card the following code is what would be better to look at: > > <?php > > #Number of cards wanted: > $cards = 5; > $columnsPerCard = 9; > $rowsPerCard = 3; > > function uniqueRandomNumbers($columns,$rows) { > #How many numbers do we need: > $totalNumbersNeeded = $columns * $rows; > #Create an array to hold them: > $NumberArray = array(); > #Do following until we have the numbers we need: > for ($a = 1; $a <= ($totalNumbersNeeded); $a++) > { > $randomNumber = rand(1,90); > #If number is already in the array, try again (and > #again) > while(in_array($randomNumber,$NumberArra y)) > { > $randomNumber = rand(1,90); > } > #Put number in array: > $NumberArray[] = $randomNumber; > } > #Return the array: > return $NumberArray; > } > > > for ($i = 1; $i <= $cards; $i++) { > #Get Random numbers for card: > $randomNumbers = uniqueRandomNumbers($columnsPerCard,$row sPerCard); > #Initiate table: > echo '<table border="1">'; > #Create Row: > for ($k = 1; $k <= $rowsPerCard; $k++) { > echo '<tr>'; > > #Create column: > for ($j = 1; $j <= $columnsPerCard; $j++) { > #pop the last number off the array, and print > #it: > $randomNumber = array_pop($randomNumbers); > echo '<td>'.$randomNumber.'</td>'; > } > #Close Row > echo '</tr>'; > } > #Close Table and add a line spacer: > echo '</table><br/>'; > } > ?> > > Again, hope that helps :] > > Anthony.
Post Follow-up to this messageHa .. What ??!!?? wrote: > Hi all. I thought I had fixed this, but I havent .. Using the following > script .. at this address http://yidpows.freeserverhost.net/bingo2.php I > have got the script to make the required six cards that have all the numbe rs > (1-90 inclusive) randomly placed but in theri respective columns. However, > if you click on the link, you will see that sometimes not all the numbers > are used. As I am still new to php, I am wondering if their are any of the se > functions that could help me out. > Thanks for any help, Scruffy. %< Code Snipped >% Hi Scruffy, Heres a little example that does what you're looking for, just three for loops and the use of the rand() function: <?php #User Settings: $cards = 5; $columnsPerCard = 9; $rowsPerCard = 3; #Begin creation: #For each of the cards do the following: for ($i = 1; $i <= $cards; $i++) { #Initiate table: echo '<table border="1">'; #For each row do: for ($k = 1; $k <= $rowsPerCard; $k++) { echo '<tr>'; #For each column do: for ($j = 1; $j <= $columnsPerCard; $j++) { #create random number: $randomNumber = rand(1,90); echo '<td>'.$randomNumber.'</td>'; } #Close Row echo '</tr>'; } #Close Table and add a line spacer: echo '</table><br/>'; } ?> Should be self explaintory, if you have any problems just ask Hope that helps, Anthony.
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.