4

我有一个用于闪存卡的数组,并使用随机播放输出 15 张独特的卡片,每张 3 张用于 5 个不同的类别。

我想做的是在同一个网页上为大约十几个人创建这些卡片组,但我不知道的部分是如何制作它,所以每个完整的集合都是独一无二的,并且不会从给定的集合中重复给任何其他用户。

带有简短说明的简短代码示例对我最有帮助。

这是我根据需要修改的代码。真的没有太大变化。

<?php
/* original source:
 * 3d10-engine.php
 * by Duane Brien
 */

if (empty($_POST)) {
for ($i = 1; $i < 16; $i++) {
    $numbers['ALL'][] = $i;
}

$picks = array();


$letters = array ('ALL');
foreach ($letters as $letter) {
    for ($i = 0;$i < 10;$i++) {
        shuffle($numbers[$letter]);
        $chunks = array_chunk($numbers[$letter], 5);
        $cards[$i][$letter] = $chunks[0];
        if ($letter == 'N') {
            $cards[$i][$letter][2] = '  '; // Free Space
        }

    }
    foreach ($numbers[$letter] as $number) {
        $balls[] = $letter.$number;
    }
    shuffle($balls);
}

$cardsstr = serialize($cards);
$ballsstr = serialize($balls);
$picksstr = serialize($picks);

} else {
$cards = unserialize($_POST['cardsstr']);
$balls = unserialize($_POST['ballsstr']);
$picks = unserialize($_POST['picksstr']);
array_unshift($picks, array_shift($balls));

echo "<h1>Just Picked: " . $picks[0] . "</h1>";

$cardsstr = serialize($cards);
$ballsstr = serialize($balls);
$picksstr = serialize($picks);
}

?>
Picks : <?php echo implode(',', $picks) ?>
<form method='post'>
<input type='hidden' name='cardsstr' value='<?php echo $cardsstr ?>' />
<input type='hidden' name='ballsstr' value='<?php echo $ballsstr ?>' />
<input type='hidden' name='picksstr' value='<?php echo $picksstr ?>' />
<input type='submit' name='cards' value='next number' />
</form>
<a href="3d10-engine.php">Start Over</a>
<?php

foreach ($cards as $card) {
echo "<table border='1'>";
echo "<tr><td>A</td><td>B</td><td>C</td><td>D</td><td>E</td></tr>";
for ($i = 0; $i < 5; $i++) {
    echo "<tr><td>" . $card['B'][$i] . "</td><td>" .$card['I'][$i]  . "</td><td>" . $card['N'][$i] . "</td>";
    echo "<td>" . $card['G'][$i] . "</td><td>" . $card['O'][$i] . "</td></tr>";
}
echo "</table>";
}

?>
4

2 回答 2

3

由于您在每组中都有更多选项,因此随机选择足以获得独特的最终结果。我的意思是不要让这件事变得更复杂。

试试这个样本

<?php
//Initialize your 5 sets here
$numbers['B'] = range(1,15);
$numbers['I'] = range(16,30);
$numbers['N'] = range(31,45);
$numbers['G'] = range(45,60);
$numbers['O'] = range(61,75);

//My Assumption is you to pick 3 from each 

while(TRUE){
 $rand = rand(0,5);
 if(count($numbers_B) < 3 && !in_array($numbers['B'][$rand]){
   $numbers_B[] = $numbers['B'][$rand];
 }

 $rand = rand(0,5);
 if(count($numbers_I) < 3 && !in_array($numbers['I'][$rand]){
   $numbers_I[] = $numbers['I'][$rand];
 }
 $rand = rand(0,5);
 if(count($numbers_N) < 3 && !in_array($numbers['N'][$rand]){
   $numbers_N[] = $numbers['N'][$rand];
 }
 $rand = rand(0,5);
 if(count($numbers_G) < 3 && !in_array($numbers['G'][$rand]){
   $numbers_G[] = $numbers['G'][$rand];
 }
 $rand = rand(0,5);
 if(count($numbers_O) < 3 && !in_array($numbers['O'][$rand]){
   $numbers_O[] = $numbers['O'][$rand];
 }

if( count($numbers_B) == 3 && count($numbers_I) == 3 && count($numbers_N) == 3 && 

count($numbers_G) == 3 && count($numbers_O) == 3 ){
  break;
 }
    }
    $result = $numbers_B + $numbers_I + $numbers_N + $numbers_G + $numbers_O; ?>

这里 $result 值应该是唯一的,并且我认为集合的数量是恒定的。如果它是动态的,那么用二维数组尝试相同的逻辑。

于 2013-01-02T14:26:06.397 回答
0

只需将准备好的集合存储在一个数组中,然后使用已经(in_array 函数)检查每个随机播放是否存在于数组中,如果存在则再次随机播放。

于 2013-01-02T13:29:47.673 回答