我有一个闪存卡阵列,并使用 shuffle 我输出15张独特的卡片,每张卡片有3张不同的卡片。
我想要做的是在同一个网页上为十几个人创建这些卡片组,但我无法弄清楚的部分是如何制作它,因此每个完整的设置都是独一无二的,不会重复给任何其他用户的一套。
简短说明的简短代码示例对我最有帮助。
这是我根据需要修改的代码。真的没什么变化。
<?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>";
}
?>
答案 0 :(得分: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值应该是唯一的,我认为集合的数量是不变的。如果它是动态的,那么尝试使用二维数组的相同逻辑。
答案 1 :(得分:0)
只需将准备好的集合存储在一个数组中,然后使用has(in_array函数)检查每个shuffle是否存在于数组中,如果它存在则再次进行洗牌。