0

大家好,我是 PHP 新手,希望能得到您的帮助

我目前正在构建一个黑杰克游戏,并且需要一些关于如何在浏览器中显示数组值的帮助,我目前有这样的卡片数组:

  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);

它做它应该做的事情,并做它应该做的数学运算。它为 Jack、Queen 和 King 提供了应有的值,因此如果 King 与 7 配对,它将添加 17。我的问题是在浏览器中 Jack、Queen 和 King 显示为“10”而不是与字符串一起显示,因此看起来它们没有被考虑在内。我想知道是否有办法在不丢失数值的情况下显示字符串。我试着像这样反转

10 => 'King',

通过这样做,King 会显示出来,但数值不会被计算在内,因此拥有 7 的人将击败拥有 King 的人。

任何帮助将不胜感激......谢谢

编辑*

我正在添加完整的代码,有四个不同的 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

     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

    include_once "Deck.php"; 
    include_once "Card.php"; 

    class EnglishDeck extends Deck
    {   

private $suits = array('Clubs', 'Diamonds', 'Hearts', 'Spades');
private $cards = array( 
    1=> 'Ace', 
    2=> '2' , 
    3 => '3',
    4 => '4', 
    5=> '5' , 
    6 => '6',
    7 => '7',
    8=> '8' , 
    9 => '9',
    10 => '10',
    10 =>'Jack', 
    10=>'Queen', 
    10=>'King'
    );


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;
}

    }

纸牌游戏.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 );

  echo "<br>";

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

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

 //Player Variables when cards are added together
$oPlayer1 = (string)$oP1card1 + (string)$oP1card2;
$oPlayer2 = (string)$oP2card1 + (string)$oP2card2;


echo "<br />";

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

这将显示在浏览器中,如下所示:

玩家一有红心 10 和红心 2 玩家二有红心 10 和红心 3 玩家 2 获胜

问题是10个中的一个应该是国王/王后/杰克

4

1 回答 1

1

您有多种选择。我建议构建一个二维数组,如:

$this->cards = array(
    'k' => array('name' => 'King', 'value' => 10),
    'q' => array('name' => 'Queen', 'value' => 10),
    'a' => array('name' => 'Ace', 'value' => 1),
    5 => array('name' => '5', 'value' => 5) 
    /* and so on */
);

然后,您可以通过以下方式轻松获取名称和值:

$this->cards['k']['name']; // Label of the card "King" (King)
$this->cards['a']['value']; // Value of the card "Ace" (1)

您存储卡的键,如果您想显示您使用的名称$this->cards['{the_key}']['name'],如果您想显示值或使用它计算$this->cards['{the_key}']['value']

于 2013-11-15T06:48:04.247 回答