PHP将数值分配给数组中的字符串

时间:2013-11-15 06:42:54

标签: php arrays key

大家好我是PHP的新手,非常感谢你的一些帮助

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

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

它做了它想做的事情,并做数学应该如何做。它为杰克,女王和国王提供了应有的价值,如果国王与七人配对则会增加17.我的问​​题是在浏览器中杰克,女王和国王显示为“10”而不是与字符串一起显示,所以看起来它们没有被考虑。我想知道是否有一种方法可以显示字符串,而不会丢失数值。我试着像这样反转

10 => 'King',

通过这样做,国王被显示但数字值没有被计算,所以一个拥有7的人将击败一个拥有国王的人。

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

编辑 *

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

     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个俱乐部 玩家2有10个心脏和3个心脏 玩家2获胜

问题是其中一个应该是国王/女王/杰克

1 个答案:

答案 0 :(得分:1)

您有多种选择。我建议建立一个2D数组,如:

$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']计算,请使用{{1}}。