我是PHP的新手,出于学习目的,我正在制作一个简单的纸牌游戏(我最终想把它变成二十一点),但现在我只想比较两个玩家的牌,看看谁的牌集最高,并以这种方式获得获胜者。我遇到了一个问题,当我显示php时,我得到一个空白屏幕,没有信息。*如果我删除"纸牌游戏.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();
?>
和纸牌游戏.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";
}
}
?>
让我们走一遍。
$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;
对 $oPlayer 2 执行相同的操作
另外 ->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($oPlayer 1, $oPlayer 2) 的函数中,然后使用 $oBaraja->compare($oPlayer 1, $oPlayer 2) 调用它