2013-10-05 63 views
0

這讓我很難過。基本上我正在試圖發牌。我得到正確數量的卡片,但它複製了它隨機生成的卡片。相反,如果用戶輸入52,我需要52個不同的卡片。使用in_array/php卡片經銷商程序

我一直在努力做到這一點,但無法弄清楚。我嘗試使用in_array功能,但無濟於事

別人的幫助,請

<?php 
$input = $_POST["txtNumberOfCards"]; 

$suit = array("Hearts", "Diamonds", "Clubs", "Spades"); 
$card = array("Ace","2", "3","4","5","6","7","8","9","10","Jack","Queen","King"); 

$randsuit = rand(0,3); 
$randcard = rand(0,12); 

for($x = 0; $x < $input; $x++){ 
echo ('<img src="Images/'.$card[$randcard].'of'.$suit[$randsuit].'.gif">'); 
} 

?> 
+0

這是因爲您沒有檢查卡是否已經生成或沒有生成。 –

回答

1

使用數組來跟蹤哪些卡片具有已被使用並據此採取行動。另外,在您的代碼有蘭特功能外for循環,這意味着它會只產生一個隨機數一旦

$input = $_POST["txtNumberOfCards"];; 

$suit = array("Hearts", "Diamonds", "Clubs", "Spades"); 
$card = array("Ace","2", "3","4","5","6","7","8","9","10","Jack","Queen","King"); 

$setCards = array(); 
for($x = 0; $x < $input; $x++){ 
    $randsuit = rand(0,3); 
    $randcard = rand(0,12); 
    if(isset($setCards[$suit[$randsuit].$card[$randcard]])) { 
     $x--; 
     continue; 
    } 
    echo ('<img src="Images/'.$card[$randcard].'of'.$suit[$randsuit].'.gif">'); 
    $setCards[$suit[$randsuit].$card[$randcard]] = true; 
} 

phpFiddle:注意小提琴回聲出來的HTML,所以它可以被看作替代渲染。

+0

我喜歡你如何在一個for循環中將卡的生成檢查和顯示結合起來,但是我總是使用一個帶有負邏輯的while循環來檢查,所以如果有的話,我不必「撤銷」計數器一個副本。 –

0

您將需要使用功能intval()

另外,$input爲int變量轉換,隨機分配varilabel每次循環生成不同的卡片:

<?php 
$input = intval($_POST["txtNumberOfCards"]); 

$suit = array("Hearts", "Diamonds", "Clubs", "Spades"); 
$card = array("Ace","2", "3","4","5","6","7","8","9","10","Jack","Queen","King"); 


for($x = 0; $x < $input; $x++){ 
$randsuit = rand(0,3); 
$randcard = rand(0,12); 
echo ('<img src="Images/'.$card[$randcard].'of'.$suit[$randsuit].'.gif">'); 
} 

?> 
+0

爲什麼選擇倒票?請在投票前進行辯解。 – neeagl

+0

我沒有投票,但這不會做重複檢測。 –

+0

@neeagl - 問題是爲什麼他的代碼返回重複。 –

0

你得到重複卡的原因是因爲你沒有檢查卡組合是否已經處理過。

最好的解決方案是建立一個卡片陣列並用它來檢查是否已經創建了卡片組合。然後回顯構建的數組。

<?php 
//The user input 
$input = $_POST["txtNumberOfCards"]; 

$suit = array("Hearts" , "Diamonds" , "Clubs" , "Spades"); 
$card = array("Ace" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "10" , "Jack" , "Queen" , "King"); 
//$dealArray holds the cards as they are dealt and used to compare what cards have all ready been dealt 
$dealArray= array(); 
//$count the while loop counter variable 
$count = 0; 
//while the $count variable is less then the $input 
while($count < $input) 
{ 
    //generate a card combination array 
    $cardCombo = array('suit' => $suit[ rand(0 , 3) ],//Generate Random suit 
         'card' => $card[ rand(0 , 12) ]);//Generate Random card 

    //if the $cardCombo array that was generated is not in the $dealArray 
    if (!in_array($cardCombo , $dealArray)) 
    { 
    //Add the $cardCombo array to the end of the $dealArray 
    array_push($dealArray , $cardCombo); 
    //Output an HTML image for the $cardCombo array 
    echo ('<img src="Images/' . $cardCombo['card'] . 'of' . $cardCombo['suit'] . '.gif">'); 
    $count++;//Add 1 to the counter 
    } 
} 
?> 
+0

嘿,謝謝一堆只有問題是當我用我的用戶輸入它不返回正確數量的卡...我輸入5,我有3張牌。 –

+0

我將它改爲while循環,以便只在沒有匹配而不是函數的結果的情況下計數for循環時才計數。 –

+0

謝謝你介意爲我評論代碼,因爲其中一些對我來說仍然沒有意義。卡組合例如不知道這意味着什麼,並提前感謝。 –