2014-02-07 91 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. –