This is a discussion on Bingo Card Generator within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hi your code is very prone to error Not sure what you are trying to do. I think you are ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hi
your code is very prone to error Not sure what you are trying to do. I think you are trying to get 3 arrays each with 30 integers between 1 and 90 each number existing in only one array. Here's how i'd do this <? $i=0; while (count ($numbers)<90) { $myrand = rand()%90+1; // get randon number between 1 and 90 if (!@in_array($myrand,$numbers)) // add to array if it is not already there $numbers[$i] = $myrand; $i++; } sort($numbers); // sort in ascending order // get 3 arrays of 30 elements each $numbers1 = array_slice($numbers,0,30); $numbers2 = array_slice($numbers,30,30); $numbers3 = array_slice($numbers,60,30); // Randomize the arrays shuffle($numbers1); shuffle($numbers2); shuffle($numbers3); // uncomment the 6 lines below to see values /* while (list($k,$v)=each($numbers1)) print "$v<br>"; while (list($k,$v)=each($numbers2)) print "$v<br>"; while (list($k,$v)=each($numbers3)) print "$v<br>"; */ ?> Ha .. 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 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> > > |
|
|||
|
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> |
|
|||
|
Ha .. 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 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. %< 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. |
|
|||
|
Anthony Plunkett wrote:
> %< Code Snipped >% > Should be self explaintory, if you have any problems just ask > > Hope that helps, > > Anthony. 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,$NumberArray)) { $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,$rowsPerCard); #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. |
|
|||
|
Many 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: > >> %< Code Snipped >% > >> Should be self explaintory, if you have any problems just ask >> >> Hope that helps, >> >> Anthony. > > 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,$NumberArray)) > { > $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,$rowsPerCard); > #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. |
|
|||
|
Unfortunately, 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: > >> %< Code Snipped >% > >> Should be self explaintory, if you have any problems just ask >> >> Hope that helps, >> >> Anthony. > > 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,$NumberArray)) > { > $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,$rowsPerCard); > #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. |