【问题标题】:PHP assigning numeric values to strings in an arrayPHP将数值分配给数组中的字符串
【发布时间】:2013-11-15 06:42:54
【问题描述】:

大家好,我是 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 />";
    }
    }

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

    }

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

    }

cardgame.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 中的一个应该是国王/王后/杰克

【问题讨论】:

  • 你是如何展示它们的?
  • 嗨,我已经更新了我的问题以显示我的完整代码,这显示在 card.php 中

标签: php arrays key


【解决方案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-&gt;cards['{the_key}']['name'],如果您想显示值或使用它进行计算,请使用$this-&gt;cards['{the_key}']['value']。

【讨论】:

  • 嗨,我不知道在哪里插入 $this->cards['k']['name']; // 卡片“King”的标签(King) $this->cards['a']['value']; // 卡片“Ace”(1) 的值让我的代码工作,有什么提示吗?
  • 这是您访问卡的两个东西(标签和值)的方式。因此,如果您需要该值,您可以使用$this-&gt;cards['{the_key}']['value'],如果您需要打印卡片名称,请使用$this-&gt;cards['{the_key}']['name']。
猜你喜欢
  • 2021-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-09
  • 2021-10-13
相关资源
最近更新 更多