PHP,如何在 class 中调用函数后对数值求和?

PHP, How can I sum-up the numeric values after calling a function from within a class?

在用户选择玩家数量后,该程序会向每位玩家发 5 张牌,显示牌的图像以及牌的数值。

一切都如上所述,但我不知道如何在调用函数后对值进行总计。谁能给我个主意?

<?php

class classHand
{
    var $totals;
    var $cards;

    function drawCard($c, $theDeck)
    {
        if (is_numeric($c)) {
            $c = floor($c);
            for ($i = 0; $i < $c; $i++) {
                $this->cards[] = $theDeck->dealCard();
            }
        }
    }

    function showHand()
    {
        print("<table border=0>\n");
        print("<tr><td>&nbsp;</td>\n");
        for ($i = 0; $i < count($this->cards); $i++) {
            print("<td>" . $this->cards[$i]->getImage() . "</td><td>&nbsp;</td>\n");
        }
        print("</tr></table>\n");
    }

    function showValue()
    {
        for ($i = 0; $i < count($this->cards); $i++) {
            print("&nbsp;" . $this->cards[$i]->getValue() . "&nbsp;");
        }
    } // end of showValue
} // end of classHand

class classPlayer
{
    var $name;
    var $hand;

    function classPlayer($n = "player")
    {
        $this->name = $n;
        $this->hand = new classHand();
    }
}

然后这是实现名为 cards.php

的 类 的页面
<?php

include("classCard.php");

$dealersDeck = new classDeck();
$dealersDeck->shuffleDeck();

$player[] = new classPlayer("You");

$selected_players = $_POST['players'];

for ($i = 0; $i < 5; $i++) {
    for ($j = 0; $j < count($player); $j++) {
        $player[$j]->hand->drawCard(1, $dealersDeck);
    }
}

for ($i = 0; $i < count($player); $i++) {
    print("Player: " . $player[$i]->name . "<br />");
    $player[$i]->hand->showHand();
    $player[$i]->hand->showValue();
    print("<P>&nbsp;</p>");
}
function drawCard($c,$theDeck){
    if(is_numeric($c)){
        $c=floor($c);
        for($i=0;$i<$c;$i++){
            $this->cards[] = $theDeck->dealCard();
            $this->total += $theDeck->dealCard()->getValue();
        }
     }
}

function getTotal(){
   return $this->total;
}

你的$totals属性没用过,所以改名为$totalValue。在此变量中,您跟踪手中的牌。

class Hand
{
    protected $totalValue;
    protected $cards;

    public function __construct()
    {
        $this->reset();
    }

    /**
     * $deck is the only required parameter
     */
    public function drawCard($deck, $count = 1, $reset = false)
    {
        // reset the counter
        $this->reset();

        if (!is_numeric($count)) return;

        for ($i = 0; $i < $ccount; $i++) {
            $this->addCard($deck->dealCard());
        }
    }

    public function addCard(Card $card)
    {
        $this->totalValue += $card->getValue();
        $this->cards[] = $card;
    }

    function getTotalValue()
    {
        return $this->totalValue;
    }

    public function reset()
    {
        $this->totalValue = 0;
        $this->cards = array();
    }
}

现在可以得到Hand的值:

$deck = new Deck();
$players[] = new Player("You");

// start with 5 cards
foreach ($players as $player) {
    $player->getHand()->drawCard($deck, 5);
}

// each player draws a card
foreach ($players as $player) {
    $player->getHand()->drawCard($deck);
}

// get totals
foreach ($players as $player) {
    print("Player: " . $player->getName() . "<br />");
    $player->getHand()->getTotalValue();
    print("<P>&nbsp;</p>");
}

// start again with new 5 cards
foreach ($players as $player) {
    $player->getHand()->drawCard($deck, 5, true); // reseting with the 3rd param
}

您不需要在 class 名称前加上 class 前缀,并且 public 属性被视为 "not done"。通常的做法是将所有属性至少设置为受保护,并添加访问函数(get..()set...()

在这个特定的示例中,我什至会考虑合并 PlayerHand class,因为它们有点类似。除非 Player 对象是通用用户对象,否则您当然会在很多地方重用。