6x6 Bingo 游戏:当卡片上有开奖号码时,开奖号码的第 7 列和第 7 行加 +1

6x6 Bingo game: When a drawn number is on the card, add +1 to the 7th column and row of the drawn number

我正在制作一个简化的宾果游戏,其中 PHP 填充了 10 到 70 之间的 6x6 个随机数。每一行都在它自己的范围内(例如 10-19、20-29 等)

宾果游戏应该是这样玩的:

至'mark' 我应该使用第 7 列和第 7 行的行和列。第 7 行和第 7 列中的每个 cel 从 0 开始。对于在行或列中找到的每个数字,第 7 行和第 7 列中的 cel 获得 +1。请参见下面的示例:

例如绘制数字 18 时:

我目前用于生成卡片的代码是:

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;}

我想不通的是如何将第 7 列和第 7 行添加到数组中,然后在抽取卡片上的数字时,将 +1 添加到特定的 row/column?

此外,当有垂直宾果游戏时,我如何知道哪些数字要涂成绿色?当它水平时,我可以使用数组键号,但我不确定列。

提前感谢您的帮助和建议。

编辑:

<?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>';
?>

您可以通过这种方式非常轻松地设置当前函数中的最后一行和最后一列:

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;
}

要检查数字是否存在,你将不得不再次解析你的卡片,如果你找到了数字,你基本上就会在你想要递增的列和行上:

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