在牌组上拖了52张牌之后摆脱所有Aces的算法是什么?输出结果中不允许使用Aces。
<?php
function pc_array_shuffle($array) {
$i = count($array);
while(--$i) {
$j = mt_rand(0, $i);
if ($i != $j) {
// swap elements
$tmp = $array[$j];
$array[$j] = $array[$i];
$array[$i] = $tmp;
}
}
return $array;
}
$suits = array('Clubs', 'Diamonds', 'Hearts', 'Spades');
$cards = array('Ace', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King');
$deck = pc_array_shuffle(range(1, 52));
$n=1;
while(($draw = array_pop($deck)) != NULL) {
echo $n.') '.$cards[$draw / 4] . ' of ' . $suits[$draw % 4] . '<br />';
$n++;
}
?>
答案 0 :(得分:1)
尝试一下:
$suits = array('Clubs', 'Diamonds', 'Hearts', 'Spades');
$cards = array('Ace', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King');
$deck = array();
$no_aces = array();
// build array of cards
foreach ($cards as $card){
foreach ($suits as $suit){
$deck[] = $card . " of " . $suit;
}
}
// shuffle deck - see what I did there? ;)
shuffle($deck);
// add shuffled cards to another array while removing the aces
foreach($deck as $card){
$pos = strrpos(strtolower($card), "ace");
if ($pos === false) {
// not an ace, add to array
$no_aces[] = $card;
}
}
// $no_aces array now contains all the shuffled cards without the aces
var_dump($no_aces);
答案 1 :(得分:0)
我只是在那里抛出疯狂的猜测,但是......
$n=1;
while(($draw = array_pop($deck)) != NULL) {
$card = $cards[$draw / 4];
if (strtolower($card) != 'ace') {
echo $n.') '.$card . ' of ' . $suits[$draw % 4] . PHP_EOL;
}
$n++;
}