1

我是 PHP 新手,出于学习目的,我正在制作一个简单的纸牌游戏(我最终想变成二十一点),但现在我只想比较两个玩家的牌,看看谁的牌最多以这种方式获得胜利者。我遇到了一个问题,当我显示 php 时,我得到一个没有任何信息的空白屏幕。*如果我删除“cardgame.php”中的代码,那么我会得到两个玩家拥有的卡片。在错误日志中,这也是显示错误的地方,但我无法弄清楚那段代码有什么问题。在那段代码中一定有一些东西阻止了它的工作......任何帮助将不胜感激。

我将发布以下四个文件:

卡片.php

class Card
{
    private $suit;
    private $figure;


    public function __construct($Suit, $Fig) {
            $this->suit   = $Suit;
            $this->figure = $Fig;
    }

    public function __toString() {
            return $this->figure . ' of ' . $this->suit;
    }

    public function getFigure() {
            return $this->figure;
    }

    public function getCard() {
            echo $this->figure . " of " . $this->suit . ".<br />";
    }


}

?>

甲板.php

<?php //can't make objects out of abstract classes, but it's children can
abstract class Deck
{
protected $arrCards; 
protected $listOfCards;

/* abstract methods force classes that extends this class to implement them */
abstract public function dealCard(); //all classes that will inherit will inherit this method


/* already implemented methods */
public function __construct($Cards) {
    $this->arrCards = $Cards;
}

public function shuffleCards() {
    shuffle($this->arrCards);
}

public function listCards() {
    foreach($this->arrCards as $Card) {
        echo $Card . '<br />';
    }
}
}
 ?>

英文甲板.php

<?php
include_once "Deck.php"; 
include_once "Card.php"; 

class EnglishDeck extends Deck
{      
        //perhaps the keys?
    private $suits = array('Clubs', 'Diamonds', 'Hearts', 'Spades');
    private $cards = array(
    'Ace'=> 1, 
    2 => 2, 
    3 => 3, 
    4 => 4, 
    5 => 5, 
    6 => 6, 
    7 => 7, 
    8 => 8, 
    9 => 9, 
    10 => 10, 
    'Jack' => 10, 
    'Queen'=>10, 
    'King'=>10);

public function dealCard() {
    return array_pop($this->arrCards);
}

public function __construct() {
    $Cards = $this->initEnglishDeck();
    parent::__construct($Cards);
}

function initEnglishDeck() {
    $arrCards = array();

    foreach($this->suits as $Suit) {
        foreach ($this->cards as $Card) {
            $arrCards[] = new Card($Suit, $Card);
        }

    }
    return $arrCards;
}

}

$oBaraja = new EnglishDeck();
$oBaraja->shuffleCards();
?>

和cardgame.php

   <?php

    include_once "Card.php";
   include_once "EnglishDeck.php";


$oBaraja = new EnglishDeck();
$oBaraja->shuffleCards();


//PLAYER 1
$oP1card1  = $oBaraja->dealCard();
echo("Player one has " . $oP1card1);

 $oP1card2  = $oBaraja->dealCard();
echo(" and a " . $oP1card2 );

$oPlayer1 = $oP1card1 + $oP1card2;

echo "<br>";

//PLAYER 2
$oP2card1  = $oBaraja->dealCard();
echo("Player two has " . $oP2card1);

$oP2card2  = $oBaraja->dealCard();
 echo(" and a " . $oP2card2);

 $oPlayer2 = $oP2card1 + $oP2card2;

 // THIS IS WHERE I AM GETTING ERRORS
 $oBaraja->compare($oPlayer1, $oPlayer2) {
    if($oPlayer1 > $oPlayer2){
        echo "Player 1 wins";
    } else if ($oPlayer1 < $oPlayer2) {
        echo "Player 2 wins";
    } else {
        echo "it's a tie";
    }
} 
?>
4

1 回答 1

3

让我们来看看它。

$oBaraja = new EnglishDeck();

你创建了一个 EnglishDeck 的新实例

$oBaraja->shuffleCards();

你洗牌

public function shuffleCards() {
shuffle($this->arrCards);}

但此时“受保护的 $arrCards;” 仍然是空的。您首先需要用卡片填充它。


编辑我运行了你的脚本,这是输出

Notice: Object of class Card could not be converted to int online 18

Player two has 10 of Hearts and a 3 of Diamonds
it's a tie

要修复此替换

 $oPlayer1 = $oP2card1 + $oP2card2;

 $oPlayer1 = (string)$oP1card1 + (string)$oP1card2;

对 $oPlayer2 执行相同的操作

此外 ->compare 不是函数。
如果你更换它确实有效

$oBaraja->compare($oPlayer1, $oPlayer2) {
if($oPlayer1 > $oPlayer2){
    echo "Player 1 wins";
} else if ($oPlayer1 < $oPlayer2) {
    echo "Player 2 wins";
} else {
    echo "it's a tie";
}
}

if($oPlayer1 > $oPlayer2){
    echo "Player 1 wins";
} else if ($oPlayer1 < $oPlayer2) {
    echo "Player 2 wins";
} else {
    echo "it's a tie";
}

或者将它放在一个名为 compare($oPlayer1, $oPlayer2) 的函数中,然后使用 $oBaraja->compare($oPlayer1, $oPlayer2) 调用它

于 2013-11-14T23:19:49.040 回答