PHP Card Game,比较高牌以获得胜利者

时间:2013-11-14 23:04:29

标签: php class oop

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

我将发布以下四个文件:

Card.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 />";
    }


}

?>

Deck.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 />';
    }
}
}
 ?>

EnglishDeck.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";
    }
} 
?>

1 个答案:

答案 0 :(得分:2)

让我们来看看吧。

$oBaraja = new EnglishDeck();

您创建了EnglishDeck的新实例

$oBaraja->shuffleCards();

你洗牌

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

但是在这一点上&#34;保护$ arrCards;&#34;仍然是空的。 你首先需要用卡填充它。


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

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

执行相同的操作

另外 - &gt;比较不是一个功能 如果你替换

它确实有效
$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-&gt; compare($ oPlayer1,$ oPlayer2)

调用它