宾果游戏:生成的宾果卡上绘制数字的颜色表格单元格

时间:2015-03-05 15:45:30

标签: php arrays loops for-loop multidimensional-array

我有一个简单的宾果游戏,从池中抽取随机数,然后检查它们是否在随机生成的6x6宾果卡上。

在宾果卡的第7列和第7行中,脚本计算在该行或列中找到的绘制数字的数量。当绘制行或列中的所有数字时,第7行或第7列中的单元格将包含6.当它达到6时,会有宾果游戏。

到目前为止,这似乎有效。我必须添加的最后一件事是导致宾果游戏的行或列中的单元格的绿色背景。见例:

Bingo card with green background on the bingo column

现状:

Current situation with 7th row and column and no green background

此外,我必须隐藏第7列和第7行。它们不应该在屏幕上显示。

您是否有任何建议可以为已经到达宾果游戏的行/列提供背景颜色? 我怎么能隐藏屏幕上的第7列和第7行呢?

我目前的代码:

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

如果要查看代码如何运行,代码应该在粘贴时工作。提前感谢您的任何帮助或建议!

1 个答案:

答案 0 :(得分:1)

此代码可以使用:

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 }

我不得不更改这部分代码,因为它没有正确检查宾果游戏:

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

    }
}

检查宾果游戏的最佳位置是增加行和列的位置。在那一刻,有一个行或列达到6的可能性。