永远不会结束while()循环

时间:2014-02-07 10:36:40

标签: php session while-loop

我需要为我的脚本添加一个while循环。这是$ points< 17(draw_cards)

也许你可以猜到,这是一款纸牌游戏。我希望它就这么简单,因为它不起作用。它陷入无休止的循环中。

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永远不会结束。

我不知道您是否需要更多信息,如果您这样做,请随时提出。因为我已经坚持这个循环2天了。似乎没有人能够帮助我。

提前致谢!

PS:如果我在显示所有错误后运行while循环并按下control + f5,它会显示:3, 10, 7, 9, 6, King, 8, Queen, Jack, 4, 2, Ace, 5, ,并在点部分显示:85 Busted!

我知道所有的积分都是95,但是因为我使用了一个案例给我的Ace,如果积分是&gt; 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;
}
}

编辑:会话经销商_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 );

3 个答案:

答案 0 :(得分:3)

draw_dealer_card()需要增加$total_dealer;,否则循环将永远存在。

更详细的答案

你只计算一次总计,而不是再次计算,这就是为什么经销商总数永远不会增加,因此永远不会超过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循环可以像这样轻松了

<?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

即可

答案 1 :(得分:2)

您当前的代码获取$total_dealer的静态值并在没有递增的情况下检入while循环,这会导致无限循环。所以尝试将foreach {}循环放入while循环中,这将允许$total_dealer递增每次选择后的值。

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;
}
}
   }
}

答案 2 :(得分:0)

试试这个

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