6x6宾果游戏:当卡上有一个绘制的数字时,将+1添加到第7列和所绘制数字的行

时间:2015-03-05 09:40:28

标签: php arrays multidimensional-array nested-loops

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

宾果游戏应该像这样玩:

  • 卡片上装满了数字
  • 数字随机抽取
  • 如果卡中包含绘制的数字,则该数字的行和列标记为+1
  • 如果一行,一列或两者都达到6,则绘图停止并且有Bingo(行或列为6为绿色)。

Finished bingo game

标记'行和列我应该使用第7列和第7行。第7行和第3列中的每个单元格从0开始。对于在行或列中找到的每个数字,第7行和第7列中的单元格获得+1。见下面的例子:

No drawn numbers

例如,当绘制数字18时:

When number 18 is drawn

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

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添加到该特定行/列?

另外,如果有垂直宾果游戏,我怎么知道哪些数字会变成绿色?如果它是水平的,我可以使用数组键编号,但我不确定列。

提前感谢您提供任何帮助和建议。

编辑:

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

1 个答案:

答案 0 :(得分:1)

您可以非常轻松地设置当前函数的最后一行和最后一列:

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

要检查号码是否存在,您将不得不再次解析您的卡片,如果您找到该号码,您将基本上位于要增加的col和行:

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