I have a 6x6 bingo card which is generated with random numbers between 10 and 70. The 7th row and 7th column are used to count the drawn numbers that are on the card. When a row or column reaches 6, there is bingo.
The eventual result I get is right, but in the proces of getting there a few things are going wrong.
In my function generateCard I create the rows and columns of numbers for the card. I think the problem lies in this function.
function generateCard()
{
$card = array();
for ($row = 1; $row < 8; ++$row)
{
$card[$row] = array();
$deck = array(0,1,2,3,4,5,6,7,8,9);
for ($kolom = 1; $kolom < 8; ++$kolom) {
$index = mt_rand(0,count($deck) - 1);
$number = $deck[$index];
$card[$row][] = $row . $number;
unset($deck[$index]);
$deck = array_values($deck);
}
//Test
printCard($card);
// 7th column
$card[$row][7] = 0;
}
//Test
printCard($card);
// 7th row
for ($kolom = 1; $kolom < 7; ++$kolom){
$card[7][$kolom] = 0;
}
//Test
printCard($card);
return $card;
}
I've put in a few printCard functions to test the outcome.
The first test above the creation of the 7th column gives me a ton of undefined offset 7 notices. I figured this happens because the column does not exist, but when I try to create this earlier, I still get the notices.
The second test above the 7th row shows that the 7th row (which should be 0) gets filled with numbers. This gets overwritten after with 0 values. I figured I could fix this by putting $row < 7 and $kolom < 7, but when I do this the card won't get printed right at all.
I'm wondering why i'm getting all these undefined offset notices about the last column (even when I create it earlier) and why I can't use $row < 7 and $column < 7 in both the for loops of generate card to avoid filling up the last row and column (7x7) with values. These should be 0 before the bingo game starts.
Do you have any suggestions?
I think i'm overlooking a few things here..
I will put the complete code here:
mt_srand((double)microtime()*1000000);
function generateCard()
{
$card = array();
for ($row = 1; $row < 8; ++$row)
{
$card[$row] = array();
$deck = array(0,1,2,3,4,5,6,7,8,9);
for ($kolom = 1; $kolom < 8; ++$kolom) {
$index = mt_rand(0,count($deck) - 1);
$number = $deck[$index];
$card[$row][] = $row . $number;
unset($deck[$index]);
$deck = array_values($deck);
}
//Test
printCard($card);
// 7th column
$card[$row][7] = 0;
}
//Test
printCard($card);
// 7th row
for ($kolom = 1; $kolom < 7; ++$kolom){
$card[7][$kolom] = 0;
}
//Test
printCard($card);
return $card;
}
function printCard($card){ ?>
<table border="1" cellspacing="0" cellpadding="5">
<?php for($rij = 1; $rij < 8; $rij++) { ?>
<tr>
<?php for ($kolom = 1; $kolom < 8; $kolom++) { ?>
<td<?php if (($card[$rij][7] == 6) || ($card[7][$kolom] == 6)) { echo ' style="background-color:green"'; } ?>><?php echo $card[$rij][$kolom]; ?></td>
<?php }
} ?>
</tr>
</table>
<?php }
$card = generateCard();
$getrokkenGetallen = array();
$deck = range(10,69);
$bingo = false;
while (!$bingo){
$index = mt_rand(0,count($deck) - 1);
$number = $deck[$index];
if(!in_array($number, $getrokkenGetallen)){
unset($deck[$index]);
$deck = array_values($deck);
$getrokkenGetallen[] = $number;
for ($row = 1; $row < 7; $row++) {
for ($kolom = 1; $kolom < 7; $kolom++) {
if ($card[$row][$kolom] == $number) {
$card[$row][7] += 1;
$card[7][$kolom] += 1;
if(($card[$row][7] == 6) || ($card[7][$kolom] == 6)){
$bingo = true;
}
break;
}
}
}
}
}
echo '<h2>Bingokaart waarop BINGO is gevallen</h2>';
printCard($card);
echo '<p><strong>Getrokken getallen:</strong><br>';
foreach($getrokkenGetallen as $value)
{
echo $value . ' ';
}
echo '</p>';
echo '<p><strong>Aantal getallen dat is getrokken:</strong> ';
echo count($getrokkenGetallen);
echo '</p>';
Example of the output:
Thank you in advance for any help or suggestions.
I'm not exactly sure where your error comes from, and I'm not a regular PHP user, but if i change the generateCard function to this:
function generateCard(){
$card = array();
for ($row = 1; $row < 8; ++$row){
$card[$row] = array();
$deck = range($row*10, $row*10+9);
shuffle($deck);
for($col = 1; $col < 8; ++$col){
if($row == 7 OR $col == 7){
$card[$row][$col] = 0;
}else{
$card[$row][$col] = $deck[$col];
}
}
}
//Test
printCard($card);
return $card;
}
It all works as expected. It's a little simpler than the route you were going i think.
Related
Find the largest three elements in an array, Given an array with all distinct elements, find the largest three elements. Expected time complexity is O(n) and extra space is O(1)
<?php
$number = array(1,2,3,4,5,6,7,8,9,10);
print_r($number);
echo "<br>";
$biggest_number_1 = 0;
$biggest_number_2 = 0;
$biggest_number_3 = 0;
for ($i=0; $i < count($number); $i++){
if($number[$i] > $biggest_number_1){
$biggest_number_1 = $number[$i];
}
if($number[$i] > $biggest_number_2 && $number[$i] != 10){
$biggest_number_2 = $number[$i];
}
if($number[$i] > $biggest_number_3 && $number[$i] != 10 && $number[$i] != 9){
$biggest_number_3 = $number[$i];
}
}
echo $biggest_number_1."<br>";
echo $biggest_number_2."<br>";
echo $biggest_number_3;
?>
Simpliest way should be something like that :
$number = array(1,2,3,4,5,6,7,8,9,10);
rsort($number); // order array desc
// Just echo first 3 result in your array
echo $number[0]."<br>";
echo $number[1]."<br>";
echo $number[2];
Now if you want to loop through to your array to get the result, you can try this :
$number = array(1,2,3,4,5,6,7,8,9,10);
$biggest_number_1 = $biggest_number_2= $biggest_number_3 = 0;
foreach ($number as $nb) {
if ($nb > $biggest_number_1) {
$biggest_number_3 = $biggest_number_2;
$biggest_number_2 = $biggest_number_1;
$biggest_number_1 = $nb;
} else if ($nb > $biggest_number_2) {
$biggest_number_3 = $biggest_number_2;
$biggest_number_2 = $nb;
} else if ($nb > $biggest_number_3) {
$biggest_number_3 = $nb;
}
}
echo $biggest_number_1."<br>";
echo $biggest_number_2."<br>";
echo $biggest_number_3;
Just sort the array by ascending and take last 3 element of array.
Try this:- This is perfectly work
$number=array(1,2,3,4,5,6,7,8,9,10);
$biggest_number_1 = $biggest_number_2= $biggest_number_3 = 0;
for ($i = 0; $i < sizeof($number) ; $i ++)
{
if ($number[$i] > $biggest_number_3 )
{
$biggest_number_1 = $biggest_number_2;
$biggest_number_2 = $biggest_number_3 ;
$biggest_number_3 = $number[$i];
}
else if ($number[$i] > $biggest_number_2)
{
$biggest_number_1 = $biggest_number_2;
$biggest_number_2 = $number[$i];
}
else if ($number[$i] > $biggest_number_1){
$biggest_number_1 = $number[$i];
}
}
echo $biggest_number_1."<br>";
echo $biggest_number_2."<br>";
echo $biggest_number_3;
I have a simple Bingo game where random numbers are drawn from a pool and then checked if they are on the randomly generated 6x6 bingo card.
In the 7th column and the 7th row of the bingo card, the script counts how many drawn numbers are found in that row or column. When all the numbers in a row or column are drawn, the cell in the 7th row or column will contain 6. When it reaches 6, there is bingo.
So far this seems to be working. The last things I have to add is a green background to the cells in the row or column that caused the bingo. See example:
Current situation:
Also I have to hide the 7th column and the 7th row. They are not supposed to show on the screen.
Do you have any suggestions to give a background color the row/column that has reached bingo?
And how could I hide the 7th column and 7th row from the screen?
My current code:
<html>
<body>
<?php
// Seeding
mt_srand((double)microtime()*1000000);
// generate numbers for the bingo card
function generateCard()
{
$card = array();
for ($row = 1; $row < 7; ++$row)
{
$card[$row] = array();
$deck = array(0,1,2,3,4,5,6,7,8,9);
// add 6 numbers to the row
for ($rownumber = 0; $rownumber < 6; ++$rownumber) {
// Random index
$index = mt_rand(0,count($deck) - 1);
// Random number from $deck
$number = $deck[$index];
// add row to random number (e.g. row 1 and number 8 = 18)
$card[$row][] = $row . $number;
// take out current random number so it wont be drawn again
unset($deck[$index]);
// Reset the index of $deck, so no unset is chosen
$deck = array_values($deck);
}
// Last column
$card[$row][] = 0;
}
// Last row
for ($col = 0; $col < 6; ++$col){
$card[7][$col] = 0;
}
return $card;
}
$card = generateCard();
// Print card
function printCard($card){ ?>
<table border="1" cellspacing="0" cellpadding="5">
<?php foreach ($card as $index => $rij) { ?>
<tr>
<?php foreach ($rij as $columnIndex => $number) { ?>
<td><?php echo $number ?></td>
<?php } ?>
</tr>
<?php } ?>
</table>
<?php }
$getrokkenGetallen = array();
$deck = range(10,69);
$bingo = false;
// Keep drawing numbers till bingo is true
while (!$bingo){
$index = mt_rand(0,count($deck) - 1);
$number = $deck[$index];
if(!in_array($number, $getrokkenGetallen)){
unset($deck[$index]);
$deck = array_values($deck);
$getrokkenGetallen[] = $number;
// Check if number is on the card
for ($row = 0; $row < 7; $row++) {
for ($rownumber = 0; $rownumber < 7; $rownumber++) {
if(isset($card[$row][$rownumber])){
if ($card[$row][$rownumber] == $number) {
// set color?
$card[$row][6] += 1; // Increment col
$card[7][$rownumber] += 1; // Increment row
}
}
}
}
}
// check if the 7th column or row contains 6 positive draws (5 for testing)
if(in_array(6, $card[$row]) || in_array(6, $card[$rownumber])){
$bingo = true;
}
}
if($bingo){
printCard($card);
echo '<p>Drawn numbers are:<br>';
foreach($getrokkenGetallen as $value)
{
echo $value . ' ';
}
echo '</p>';
echo '<p>Times drawn: ';
echo count($getrokkenGetallen);
echo '</p>';
}
?>
</body>
</html>
Code should work as it is pasted if you want to see how it runs. Thanks in advance for any help or suggestions!
This code will work:
function printCard($card){ ?>
<table border="1" cellspacing="0" cellpadding="5">
<?php
$row = 0;
foreach ($card as $index => $rij)
{
$row++;
if ($row < 7)
{?>
<tr>
<?php
$column = 0;
foreach ($rij as $columnIndex => $number)
{
$column++;
if ($column < 7)
{ ?>
<td<?php if (($rij[6]==6) || ($card[7][$column-1] ==6)) { echo ' style="background-color:green"'; } ?>><?php echo $number ?></td>
<?php }
}
} ?>
</tr>
<?php
} ?>
</table>
<?php }
I had to change this part of the code, because it didn't do a right check for bingo:
// Keep drawing numbers till bingo is true
while (!$bingo){
$index = mt_rand(0,count($deck) - 1);
$number = $deck[$index];
if(!in_array($number, $getrokkenGetallen)){
unset($deck[$index]);
$deck = array_values($deck);
$getrokkenGetallen[] = $number;
// Check if number is on the card
for ($row = 0; $row < 7; $row++) {
for ($rownumber = 0; $rownumber < 7; $rownumber++) {
if(isset($card[$row][$rownumber])){
if ($card[$row][$rownumber] == $number) {
// set color?
$card[$row][6] += 1; // Increment col
$card[7][$rownumber] += 1; // Increment row
// check if the 7th column or row contains 6 positive draws (5 for testing)
if(($card[$row][6] == 6) || ($card[7][$rownumber] == 6)){
$bingo = true;
}
break;
}
}
}
}
}
}
The best place to check for bingo is where you increment the row and column. At that moment there is a posibility that a row or column reaches 6.
I'm making a simplified bingo game with PHP filled with 6x6 random numbers between 10 and 70. Each row is in it's own range (e.g. 10-19, 20-29 etc.)
The bingo game is supposed to play like this:
The card gets filled with numbers
Numbers get drawn randomly
If the card contains a drawn number, the row and column of that number get marked +1
If a row, a column or both reach 6, the drawing stops and there is Bingo (row or column with 6 is green).
To 'mark' the rows and columns i'm supposed to use the 7th column and 7th row. Each cel in the 7th row and column starts at 0. For each number that is found in the row or column, the cel in the 7th row and 7th column gets +1. See example below:
When for example number 18 is drawn:
The current code that I use for generating the card is:
function generateCard(){
$card = array();
for ($row = 1; $row < 7; ++$row)
{
$card[$row] = array();
$deck = array(0,1,2,3,4,5,6,7,8,9);
for ($rownumber = 0; $rownumber < 6; ++$rownumber) {
$index = mt_rand(0,count($deck) - 1);
$number = $deck[$index];
$card[$row][] = $row . $number;
unset($deck[$index]);
$deck = array_values($deck);
}
}
return $card;}
What I can't figure out is how I can add this 7th column and 7th row to the array and then when a number on the card is drawn, add +1 to that specific row/column?
Also, how do I know which numbers to color green when there is a vertical bingo? When it's horizontal I could use the array key number, but i'm not sure about the columns.
Thank you in advance for any help and suggestions.
EDIT:
<?php
mt_srand((double)microtime()*1000000);
function generateCard()
{
$card = array();
for ($row = 1; $row < 7; ++$row)
{
$card[$row] = array();
$deck = array(0,1,2,3,4,5,6,7,8,9);
for ($rownumber = 0; $rownumber < 6; ++$rownumber) {
$index = mt_rand(0,count($deck) - 1);
$number = $deck[$index];
$card[$row][] = $row . $number;
unset($deck[$index]);
$deck = array_values($deck);
}
// Last kolom
$card[$row][] = 0;
}
// Last row
for ($col = 0; $col < 6; ++$col){
$card[7][$col] = 0;
}
return $card;
}
// Kaart vullen
$card = generateCard();
// Print the card
function printCard($card){ ?>
<table border="1" cellspacing="0" cellpadding="5">
<!-- row -->
<?php foreach ($card as $index => $rij) { ?>
<tr>
<!-- add 6 numbers to row -->
<?php foreach ($rij as $columnIndex => $number) { ?>
<td><?php echo $number ?></td>
<?php } ?>
</tr>
<?php } ?>
</table>
<?php }
$getrokkenGetallen = array();
$deck = range(10,69);
$bingo = false;
// Draw numbers while there is no bingo
while (!$bingo){
//for($i = 0; $i < 60; $i++ ){
$index = mt_rand(0,count($deck) - 1);
$number = $deck[$index];
// Check if random number is in drawn numbers
if(!in_array($number, $getrokkenGetallen)){
unset($deck[$index]);
$deck = array_values($deck);
$getrokkenGetallen[] = $number;
// Check if number is on the card
for ($row = 0; $row < 6; $row++) {
for ($rownumber = 1; $rownumber < 7; $rownumber++) {
if(isset($card[$row][$rownumber])){
if ($card[$row][$rownumber] == $number) {
// set color
$card[$row][6] += 1; // Increment col
$card[7][$rownumber] += 1; // Increment row
}
}
}
}
}
// check if the 7th column or row contains 6 positive draws (5 for testing)
if(in_array(5, $card[$row]) || in_array(5, $card[$rownumber])){
$bingo = true;
echo 'bingo';
}
}
// While developing
echo printCard($card);
echo '<p>Drawn numbers are:<br>';
foreach($getrokkenGetallen as $value)
{
echo $value . ' ';
}
echo '</p>';
?>
You can set your last row and last column in your current function pretty easily that way :
for ($row = 1; $row < 7; ++$row)
{
$card[$row] = array();
$deck = array(0,1,2,3,4,5,6,7,8,9);
for ($rownumber = 0; $rownumber < 6; ++$rownumber) {
// Bla bla
}
$card[$row][] = 0; // Last column
}
// Last line
for ($col = 0; $col < 6; ++$col) {
$card[7][$col] = 0;
}
To check if the number exists, you will have to parse your card again, and if you find the number, you will basically be at the col and row you want to increment :
$tab = generateCard();
$number = 18;
function checkNumber($number, &$tab) {
for ($col = 0; $col < 6; $col++) {
for ($row = 1; $row < 7; $row++) {
if ($tab[$col][$row] == $number) {
// Set color to green however your want
$tab[$col][8] += 1; // Increment col
$tab[7][$row] += 1; // Increment row
return true;
}
}
}
return false;
}
$numberWasFound = checkNumber($number, $tab);
echo $numberWasFound ? 'Yes !' : 'No :(';
I have written the following code to count the number of string occurrences in a given file.
PHP
<?php
$p = fopen("g.txt", "r");
$q = fread($p, filesize("g.txt"));
$t = explode(" ", $q);
$m = explode(" ", $q);
$i = 0;
$j = 0;
$r = 0;
$count = 0;
$c = count($t);
$d = array();
echo "count of".
"<br/>";
for ($i = 0; $i < $c; $i++) {
for ($j = $i; $j < $c; $j++) {
if ($t[$i] == $t[$j]) {
$count = $count + 1;
}
}
for ($r = $i + 1; $r < $c; $r++) {
if ($t[$i] == $t[$r])
unset($t[$r]);
}
echo $t[$i].
"=".$count.
"<br/>";
$count = 0;
}
?>
I am getting a notice of undefined offset on line numbers 17 and 24, though my output is coming out to be correct. Can you please help me in rectifying the above problem?
The problem is that you are deleting items from the array $t. You saved the count in $c, but the actual count will change by your last inner loop.
Even if you replace $c by count($t) everywhere, it will go wrong, because the last loop should be in reverse order, otherwise you skip items. For instance if you have the list 'a', 'b', 'c'. then when you delete 'b' and increment $r, you will not check 'c' at all.
So, if I fix those things, your code becomes as below. Although I didn't really check it for other issues. Frankly, I don't really get what is should do. ;-)
<?php
$p=fopen("g.txt","r");
$q=fread($p,filesize("g.txt"));
$t=explode(" ",$q);
$m=explode(" ",$q);
$i=0;
$j=0;
$r=0;
$count=0;
$d=array();
echo "count of"."<br/>";
for($i=0; $i<count($t); $i++)
{
for($j=$i; $j<count($t); $j++)
{
if($t[$i]==$t[$j])
{
$count=$count+1;
}
}
for($r=count($t) - 1; $r > $i; $r--)
{
if($t[$i]==$t[$r])
unset($t[$r]);
}
echo $t[$i]."=".$count."<br/>";
$count=0;
}
?>
In conclusion, you should do more tests. If the outcome of this script was okay, then it was by accident.
I want to generate 10 numbers with each ranging from (1 to 5) but can only duplicate after 2 elements
for example 5 3 1 4 2 5(can be duplicated here) 2 (cannot be duplicate here since it occur before 1 element) ...etc.
I have this code in php working but its performance is awful since it sometimes exceeds the maximum 30 seconds execution time.
<?php
function contain($prevItems, $number) {
if (count($prevItems) == 3)
{
array_shift($prevItems);
}
for ($k=0; $k<count($prevItems); $k++) {
if ($prevItems[$k] == $number)
return true;
}
return false;
}
$num[0] = rand(1,5);
$prevItems[0] = $num[0];
for ($i=1; $i<=10; $i++) {
$num[$i] = rand(1,5);
while (contain($prevItems, $num[$i])) {
$num[$i] = rand (1,5);
}
$prevItems[$i] = $num[$i]; //append into the array
}
print_r($num);
?>
Edit:
I have also tried this method, its performance is good but it duplicates elements
<?php
$evalAccurance = array();
$count = 0;
while ( $count < 11)
{
$random = rand(1, 5);
if (in_array($random, $evalAccurance))
{
$p = $random;
for ($k = $p ; $k <5; $k++)
{
$random = $random++;
if (in_array($random, $evalAccurance))
continue 1;
else break 1;
}
if (in_array($random, $evalAccurance))
{
for ($k = $p ; $k >0; $k--)
{
$random = $random--;
if (in_array($random, $evalAccurance))
continue 1;
else break 1;
}
}
}
$evalAccurance[] = $random;
if (count ($evalAccurance) == 4)
array_shift($evalAccurance);
print_r ($evalAccurance);
$count++;
}
?>
One way you could do this:
// pass to function current array of numbers
function randomNumber($ar){
// create a random number from 1 to 5
$num = rand(1,5);
// check backwards 3 elements for same number, if none found return it
if(!in_array($num, array_slice($ar, -3, 3, true))){
return $num;
} else {
// else recall function with same array of numbers
return randomNumber($ar);
}
}
$array = array();
// loop 10 numbers and call randomNumber with current set of results.
for($i=1; $i<=10; $i++){
$array[] = randomNumber($array);
}
print_r($array);
Using PHP SPLQueue:
$queue = new SplQueue();
$values = array(1, 2, 3, 4, 5);
$container = array();
for ($i = 0; $i < 10; $i++) {
$value = give_random($values, $queue);
$container[] = $value;
if ($queue->offsetExists(1) AND $queue->offsetExists(0)) {
$queue->dequeue();
}
$queue->enqueue($value);
}
function give_random(&$values, &$queue) {
$picked_value = $values[array_rand($values)];
if ($queue->offsetExists(1)) {
if ($picked_value == $queue->offsetGet(1)) {
$picked_value = give_random($values, $queue);
}
}
if ($queue->offsetExists(0)) {
if ($picked_value == $queue->offsetGet(0)) {
$picked_value = give_random($values, $queue);
}
}
return $picked_value;
}
print_r($container);
It could be neater, but you can figure what's going on.
Cheers.