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:
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 :(';