PHP为数组中的字符串指定数值
大家好,我是PHP新手,非常感谢你们的帮助 我目前正在制作一个black Jack游戏,需要一些关于如何在浏览器中显示数组值的帮助,我目前的卡片数组设置如下:PHP为数组中的字符串指定数值,php,arrays,key,Php,Arrays,Key,大家好,我是PHP新手,非常感谢你们的帮助 我目前正在制作一个black Jack游戏,需要一些关于如何在浏览器中显示数组值的帮助,我目前的卡片数组设置如下: private $suits = array('Clubs', 'Diamonds', 'Hearts', 'Spades'); private $cards = array( 'Ace'=> 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 =&
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。我的问题是,在浏览器中,Jack、Queen和King显示为“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 />";
}
}
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";
}
包含一次“Card.php”;
包括_once“EnglishDeck.php”;
$oBaraja=新英语贝克();
$oBaraja->shuffleCards();
//玩家1
$oP1card1=$oBaraja->dealCard();
echo(“玩家1拥有”$oP1card1);
$oP1card2=$oBaraja->dealCard();
echo(“和a”。$oP1card2);
回声“
”;
//玩家2
$oP2card1=$oBaraja->dealCard();
echo(“玩家2拥有”$oP2card1);
$oP2card2=$oBaraja->dealCard();
echo(“和a”。$oP2card2);
//将卡添加到一起时的玩家变量
$oPlayer1=(字符串)$oP1card1+(字符串)$oP1card2;
$oPlayer2=(字符串)$oP2card1+(字符串)$oP2card2;
回声“
”;
如果($oPlayer1>$oPlayer2){
回声“玩家1获胜”;
}否则如果($oPlayer1<$oPlayer2){
回声“玩家2赢”;
}否则{
回声“这是一条领带”;
}
这将在浏览器中显示,如下所示:
一号球员有10颗红心和2颗梅花
玩家2有10颗红心和3颗红心
玩家2获胜
问题是,10个选项中的一个应该是国王/王后/杰克,你有多种选择。我建议构建一个二维阵列,如:
$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']
如何显示它们?您好,我已经更新了我的问题以显示我的完整代码,这显示在card.phpHi中,我不知道在哪里插入$this->cards['k']['name'];//“国王”(国王)牌的标签$this->cards['a']['value'];//卡片“Ace”的值(1)要使我的代码正常工作,有什么提示吗?这是如何访问卡片的两个内容(标签和值)。因此,在需要值的地方,可以使用$this->cards['{the_key}']['value']
,如果需要打印出卡名,可以使用$this->cards['{the_key}']['name']
。
$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)