2014-02-07 236 views
0

我需要爲我的腳本添加while循環。這是一段$積分< 17(draw_cards)永不結束while()循環

也許你可以猜到,這是一款紙牌遊戲。我希望它是那麼簡單,因爲它不會工作。它陷入了無盡的循環。

if(FORM_stand("Stand")){ 
    while ($total_dealer < 17){ 
    list_dealer_hand(); 
    draw_dealer_card(); 
    } 
} 

如果我運行我的腳本,它會一直持續下去。顯示我的例子7, 7, 3, 7, 3, 9, 7, 3, 9, 2, 7, 3, 9, 2, 6, 7, 3, 9, 2, 6, 10, 7, 3, 9, 2, 6, 10, Ace, 7, 3, 9, 2, 6, 10, Ace, 5, 7, 3, 9, 2, 6, 10, Ace, 5, Queen, 7, 3, 9, 2, 6, 10, Ace, 5, Queen, Jack, 7, 3, 9, 2, 6, 10, Ace, 5, Queen, Jack, King, 7, 3, 9, 2, 6, 10, Ace, 5, Queen, Jack, King, 4, 7, 3, 9, 2, 6, 10, Ace, 5, Queen, Jack, King, 4, 8,

最終重複 Notice: Undefined index

但我如果我是使用前:

if(FORM_stand("Stand")){ 
    list_dealer_hand(); 
     if ($total_dealer < 17){ 
     draw_dealer_card(); 
     } 
} 

我需要手動按下Stand幾次,因爲它是一個if但這種方式它將繼續繪製卡直到他的積分是17或更高,這意味着If工程,但While永不止息。

我不知道你是否需要更多的信息,如果你這樣做,請詢問。因爲我一直堅持這個while循環2天了。似乎沒有人能夠幫助我。

在此先感謝!

PS:如果我運行while循環,並按下控制+ F5顯示所有的錯誤後,它讓我看到這一點:3, 10, 7, 9, 6, King, 8, Queen, Jack, 4, 2, Ace, 5, ,和尖端部:85 Busted!

我知道所有的點一起是95,但因爲我爲我的Ace使用了一個案例,如果積分大於11,它將被計爲1而不是11. Mabye這一點可以幫助你!

list_dealer_hand()

function list_dealer_hand() { 
foreach($_SESSION["dealer_hand"] as $dealer_card=>$points) { 
    echo $dealer_card; 
    echo ', '; 
} 
} 

和draw_dealer_card()

function draw_dealer_card() { 
    $dealer_card = array_rand($_SESSION["dealer_pile"]); 
    $_SESSION["dealer_hand"][$dealer_card] = $_SESSION["dealer_pile"][$dealer_card]; 
    unset($_SESSION["dealer_pile"][$dealer_card]); 

}

我對分案制度如下所示:

$total_dealer = 0; 
$text_dealer = ''; 
foreach($_SESSION["dealer_hand"] as $dealer_card=>$dealer_points) { 
switch($dealer_card) 
{ 
    case "King": 
    case "Queen": 
    case "Jack": 
    case "10": 
    $total_dealer += 10; 
    break; 
    case "Ace": 
    if($total_dealer >= 11) 
     { 
     $total_dealer += 1; 
     }else{ 
      $total_dealer += 11; 
     } 
     break; 
    case "9": 
     $total_dealer += 9; 
     break; 
    case "8": 
     $total_dealer += 8; 
     break; 
    case "7": 
     $total_dealer += 7; 
     break; 
    case "6": 
     $total_dealer += 6; 
     break; 
    case "5": 
     $total_dealer += 5; 
     break; 
    case "4": 
     $total_dealer += 4; 
     break; 
    case "3": 
     $total_dealer += 3; 
     break; 
    case "2": 
     $total_dealer += 2; 
     break; 
} 
} 

編輯:會話dealer_pile

if(!isset($_SESSION["dealer_pile"])) $_SESSION["dealer_pile"] = array(
2   => 2, 
3   => 3, 
4   => 4, 
5   => 5, 
6   => 6, 
7   => 7, 
8   => 8, 
9   => 9, 
10   => 10, 
'Jack'  => 10, 
'Queen'  => 10, 
'King'  => 10, 
'Ace'  => 11); 
+1

你的價值永遠不會大於17這就是爲什麼檢查你的價值是遞增或不 –

+1

增量'$在while循環 –

+0

是'$ total_dealer' total_dealer'在list_dealer_hand()中改變值;' – krishna

回答

3

draw_dealer_card()需要增加$total_dealer;否則循環會永遠的。

一個更詳盡的答案

你只在while循環計算總的一次,再也沒有,這就是爲什麼經銷商總數將永遠不會增加,因此絕不會超過17

把代碼在其自身的功能卡轉換爲它的價值,所以你可以在任何地方使用它

<?php 
/** 
* return the value of the card for the current total 
* @param string $card the card to convert to count 
* @param int $current_total the current total of the player/dealer 
* @return int the value of $card 
*/ 
function get_card_value($card, $current_total) { 
    switch($card) { 
     case "King": 
     case "Queen": 
     case "Jack": 
      return 10; 

     case "Ace": 
      return ($current_total > 10) ? 1 : 11; 

     case "10": 
     case "9": 
     case "8": 
     case "7": 
     case "6": 
     case "5": 
     case "4": 
     case "3": 
     case "2": 
      return (int) $card; 
    } 
    return 0; // this should not happen probably abort here 
} 

從這裏很容易,編輯您的while循環是這樣的:

<?php 
while ($total_dealer < 17){ 
    list_dealer_hand(); 
    draw_dealer_card(); 
    /* this is bad code using end(), 
    * which might not always get the last drawn card. 
    * Also calculation of total is wrong this way: 
    * What happens if dealer draws Ace, Ace, Ace, King? 
    * Should be 1+1+1+10 = 13 but will result in 11+1+1+10=23 
    */ 
    $total_dealer += get_card_value(end($_SESSION['dealer_hand']), $total_dealer); 
} 

的正確計算爲了使您的代碼更健壯增加一個功能calc_total(array $cards)它計算出總的卡陣列,並用它來代替while循環重新計算的總經銷商。像這樣的功能可能看起來像這樣

<?php 
function calc_total(array $cards) { 
    //this is a little tricky since aces must be counted last 
    $total = 0; 
    $aces = array(); 
    foreach($cards as $card) { 
     if($card === 'Ace') { 
      $aces[] = $card; 
      continue; // next $card 
     } 
     $total += get_card_value($card, $total); 
    } 
    // add aces values 
    if (($total + 10 + count($aces)) > 21) { 
     //all aces must count 1 or 21 will be exceeded 
     return $total + count($aces); 
    } 
    foreach($aces as $card) { 
     $total += get_card_value($card, $total); 
    } 
    return $total; 
} 

現在while循環可能LOOL這樣

<?php 
while ($total_dealer < 17){ 
    list_dealer_hand(); 
    draw_dealer_card(); 
    // recalculate the dealers total 
    $total_dealer = calc_total($_SESSION['dealer_hand']); 
} 

設置樁

混合數字和字符串鍵是完全有效的PHP,但也是大部分時間誤導。在你的籌碼中,你只需要牌,價值並不重要,你可以通過致電get_card_value($card, 0)隨時獲得牌值。於是成立了一堆這樣的:

<?php 
if(!isset($_SESSION["dealer_pile"])) $_SESSION["dealer_pile"] = array(
    'Jack', 'Queen', 'King', 'Ace', '10', '9', '8', '7', '6', '5', '4', '3', '2' 
); 

也改變了draw_dealer_card功能

<?php 
function draw_dealer_card() { 
    //get a key 
    $key = array_rand($_SESSION["dealer_pile"]); 
    // add the card to the hand 
    $_SESSION["dealer_hand"][] = $_SESSION["dealer_pile"][$key]; 
    /* 
    * why are you removing it from pile, the pile might 
    * contain multiple cards of each type 
    */ 
    // unset($_SESSION["dealer_pile"][$dealer_card]); 
} 

注意的$_SESSION['dealer_hand']如何不再關聯。考慮到這一點,只要你添加卡,只需要使用,$_SESSION["dealer_hand"][] = $the_new_card

+0

我很欣賞你爲幫助我而付出的努力,但它似乎不工作= /,如果我只添加前2件。先進的案例系統和時間。如果我按開始遊戲,它不會添加一個值。這給了經銷商1張牌。其次,如果我按下立場,說他在開始時得到了3,在我按下立場後,它會向我顯示這些卡片:'3,3,5,3,5,2,'總分數爲:'17' –

+0

如果我添加你的cal_total函數它說有一個意想不到的{在行'if(($總+ 10 +計數($ aces)> 21){',編輯:nevermind你剛剛使用2 1需要 –

+0

謝謝@Farewyth代碼檢查暗示我的語法不正確,但我沒有看到它,應該使用IDE而不是直接寫在這裏:) - 我也只重複絕對需要重複的代碼。它並不是無處不在,例如'while'仍然會進入'if(FORM_stand(「Stand」)){'block。只需詢問是否需要進一步幫助。 – Armin

0

試試這個

$total_dealer=0; 
    if(FORM_stand("Stand")){ 
     while ($total_dealer < 17){ 
     list_dealer_hand(); 
     draw_dealer_card(); 
     $total_dealer =$total_dealer+1; 
     } 
    } 
+0

我不認爲total_dealer應該增加1我認爲它應該添加前面的繪製卡到它的總數。 –

+1

@WBOCo。是對的,它應該隨點數增加。 –

2

您當前的代碼獲取的​​3210靜態值,並檢查在while循環不增加,這將導致無限loop.So嘗試把的foreach {}循環內while循環,它將允許​​3210在每次選擇後增加值。

if(FORM_stand("Stand")){ 
$total_dealer = 0; 
    while ($total_dealer < 17){ 
    list_dealer_hand(); 
    draw_dealer_card(); 

$text_dealer = ''; 
foreach($_SESSION["dealer_hand"] as $dealer_card=>$dealer_points) { 
switch($dealer_card) 
{ 
    case "King": 
    case "Queen": 
    case "Jack": 
    case "10": 
    $total_dealer += 10; 
    break; 
    case "Ace": 
    if($total_dealer >= 11) 
     { 
     $total_dealer += 1; 
     }else{ 
      $total_dealer += 11; 
     } 
     break; 
    case "9": 
     $total_dealer += 9; 
     break; 
    case "8": 
     $total_dealer += 8; 
     break; 
    case "7": 
     $total_dealer += 7; 
     break; 
    case "6": 
     $total_dealer += 6; 
     break; 
    case "5": 
     $total_dealer += 5; 
     break; 
    case "4": 
     $total_dealer += 4; 
     break; 
    case "3": 
     $total_dealer += 3; 
     break; 
    case "2": 
     $total_dealer += 2; 
     break; 
} 
} 
    } 
} 
+0

我不認爲total_dealer應該增加1我認爲它應該添加先前繪製的卡到它的總數。 –

+0

你有一點,但直到Farewyth指定更多關於我們可以給出基於野生猜測的答案的問題 – krishna

+0

對不起,是的,但@WBOCo。是正確的,它需要增加與卡的價值點。而不是與1. –