Php 永无止境的while()循环

Php 永无止境的while()循环,php,session,while-loop,Php,Session,While Loop,我需要为我的脚本添加一个while循环。一段时间$points11,它将被算作1而不是11分。也许这一点对你有帮助 名单(经销商)(手) 并提取(经销商)卡() } 我的分数案例系统如下所示: $total_dealer = 0; $text_dealer = ''; foreach($_SESSION["dealer_hand"] as $dealer_card=>$dealer_points) { switch($dealer_card) { case "King":

我需要为我的脚本添加一个while循环。一段时间$points<17(抽卡)

也许你能猜到,这是一场纸牌游戏。我希望它能这么简单,因为它不起作用。它陷入了一个无休止的while循环

if(FORM_stand("Stand")){
   while ($total_dealer < 17){
     list_dealer_hand();
     draw_dealer_card();
   }
}
我需要手动按几次
站立
,因为这是一个
如果
,但这样它会一直抽牌,直到他的分数达到17分或更高,这意味着
如果
有效,但
永远不会结束

我不知道你是否需要更多的信息,如果你需要的话,请马上问。因为我已经被困在这个while循环上两天了。似乎没有人能帮我

提前谢谢

PS:如果我运行while循环并在显示所有错误后按control+f5,它会显示以下内容:
3、10、7、9、6、King、8、Queen、Jack、4、2、Ace、5、
,在point部分:
85
完蛋了

我知道所有的分数加起来都是95分,但是因为我用了一个我的王牌的例子,如果分数>11,它将被算作1而不是11分。也许这一点对你有帮助

名单(经销商)(手)

并提取(经销商)卡()

}

我的分数案例系统如下所示:

$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;
}
}
编辑:会话分发器\u堆

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 );
试试这个

$total_dealer=0;
    if(FORM_stand("Stand")){
       while ($total_dealer < 17){
         list_dealer_hand();
         draw_dealer_card();
         $total_dealer =$total_dealer+1;
       }
    }
$total\u经销商=0;
if(表格展位(“展位”)){
而($total_<17){
列出经销商的手();
抽取经销商卡();
$total_dealer=$total_dealer+1;
}
}
试试这个

$total_dealer=0;
    if(FORM_stand("Stand")){
       while ($total_dealer < 17){
         list_dealer_hand();
         draw_dealer_card();
         $total_dealer =$total_dealer+1;
       }
    }
$total\u经销商=0;
if(表格展位(“展位”)){
而($total_<17){
列出经销商的手();
抽取经销商卡();
$total_dealer=$total_dealer+1;
}
}

您当前的代码获取静态值
$total\u dealer
,并在不递增的情况下检入while循环,这将导致无限循环。因此,请尝试将foreach{}循环放入while循环中,这将允许
$total\u 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;
}
}
   }
}
if(表格展位(“展位”)){
$total_经销商=0;
而($total_<17){
列出经销商的手();
抽取经销商卡();
$text_经销商=“”;
foreach($\会话[“经销商\手牌”]作为$DELER\卡=>$DELER\积分){
交换机($经销商卡)
{
“国王”一案:
“女王”案:
案例“杰克”:
案例“10”:
$total_经销商+=10;
打破
案例“Ace”:
如果($total_>=11)
{
$total_经销商+=1;
}否则{
$total_经销商+=11;
}
打破
案例“9”:
$total_经销商+=9;
打破
案例“8”:
$total_经销商+=8;
打破
案例“7”:
$total_经销商+=7;
打破
案例“6”:
$total_经销商+=6;
打破
案例“5”:
$total_经销商+=5;
打破
案例“4”:
$total_经销商+=4;
打破
案例“3”:
$total_经销商+=3;
打破
案例“2”:
$total_经销商+=2;
打破
}
}
}
}

您当前的代码获取静态值
$total\u dealer
,并在不递增的情况下检入while循环,这将导致无限循环。因此,请尝试将foreach{}循环放入while循环中,这将允许
$total\u 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;
}
}
   }
}
if(表格展位(“展位”)){
$total_经销商=0;
而($total_<17){
列出经销商的手();
抽取经销商卡();
$text_经销商=“”;
foreach($\会话[“经销商\手牌”]作为$DELER\卡=>$DELER\积分){
交换机($经销商卡)
{
“国王”一案:
“女王”案:
案例“杰克”:
案例“10”:
$total_经销商+=10;
打破
案例“Ace”:
如果($total_>=11)
{
$total_经销商+=1;
}否则{
$total_经销商+=11;
}
打破
案例“9”:
$total_经销商+=9;
打破
案例“8”:
$total_经销商+=8;
打破
案例“7”:
$total_经销商+=7;
打破
案例“6”:
$total_经销商+=6;
打破
案例“5”:
$total_经销商+=5;
打破
案例“4”:
$total_经销商+=4;
打破
案例“3”:
$total_经销商+=3;
打破
案例“2”:
$total_经销商+=2;
打破
}
}
}
}
提取经销商卡()
需要增加
$total\u经销商否则循环将永远停止

更详细的回答 在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
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;
}
<?php
while ($total_dealer < 17){
    list_dealer_hand();
    draw_dealer_card();
    // recalculate the dealers total
    $total_dealer = calc_total($_SESSION['dealer_hand']);
}
同时更改draw_dealer_卡功能

<?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]);
}
提取经销商卡()
需要增加
$total\u经销商否则循环将永远停止

更详细的回答 在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
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;
}
<?php
while ($total_dealer < 17){
    list_dealer_hand();
    draw_dealer_card();
    // recalculate the dealers total
    $total_dealer = calc_total($_SESSION['dealer_hand']);
}
同时更改draw_dealer_卡功能

<?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]);
}

我不认为total_dealer应该增加1,我认为应该将上一张抽到的牌添加到其总数中。你有道理,但在Farewyth详细说明问题之前,我们只能根据猜测给出答案。对不起,是的,但是@WBOCo。是的,它需要加上卡的积分值。而不是1。@Farewyth从哪里获得的
积分值的卡
?尝试将整个
foreach($\u SESSION[“dealer\u hand”]作为$dealer\u card=>$dealer\u积分){
inside while Loop我不认为total_dealer应该增加1,我认为应该将之前的抽牌添加到其总数中。你说得对,但直到Farewyth详细说明我们遇到的问题