2013-06-28 87 views
0

我有以下PHP代碼洗牌1套牌;然而 - 我正在尋找像賭場這樣的6副牌在黑傑克遊戲中洗牌。有關如何實現這一目標的任何想法?PHP Shuffle Multiple Decks

private $suites = array('c', 'd', 'h', 's'); 
    private $ranks = array('2', '3', '4', '5', '6', '7', '8', '9', 't', 'j', 'q', 'k', 'a'); 
    private $deck = array(); 


    for ($i = 0; $i < count($this->deck); $i++) { 
      $r = $i + rand(0, 51 - $i); 
      $temp = $this->deck[$i]; 
      $this->deck[$i] = $this->deck[$r]; 
      $this->deck[$r] = $temp; 
     } 
+0

有什麼期望的輸出? – 2013-06-28 00:51:26

+0

有你聽說過PHP的[ 'shuffle()'](http://php.net/manual/en/function.shuffle.php)? –

+0

Shuffle函數?http://php.net/manual/en/function.shuffle.php – iWantSimpleLife

回答

0

是這樣的?

$decks = array_merge($deck1, $deck2, ...); 
shuffle($decks); 
+0

我認爲這與我正在尋找的接近 - 但我似乎無法應用概念到我的代碼。你能用我的例子來闡述一下嗎? – timw07

0

就像這樣。

CONST COUNT_DECKS = 6; 

$suites = array('c', 'd', 'h', 's'); 
$ranks = array('2', '3', '4', '5', '6', '7', '8', '9', 't', 'j', 'q', 'k', 'a'); 

$deck = array(); 

for ($i = 0; $i < COUNT_DECKS; $i++) 
    foreach ($ranks as $rank) 
     foreach ($suites as $suit) 
      $deck[] = $suit . $rank; 

shuffle($deck); 
var_dump($deck); 
0

所以我基本上解決了這個使用下面的方法(先我裝了$ deck1所有的52張牌,爲了。

for($i =0;$i<6;$i++){ 
      $decks = array_merge($decks,$deck1); 
     } 

     shuffle($decks); 
     for ($i = 0; $i < count($this->deck); $i++) { 
      $this->deck[$i] = $decks[$i]; 
     }