PHP中数组赋值数值为字符串 [英] PHP assigning numeric values to strings in an array

查看:101
本文介绍了PHP中数组赋值数值为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我是新来的PHP,并将AP preciate一些对你有帮助。

Hi everyone I am new to PHP, and would appreciate some of your help

我目前正在建设一个黑杰克游戏,并且需要一些帮助与如何数组的值显示在浏览器中,我公司目前已为卡片阵列设置是这样的:

I am currently building a black Jack Game, and need some help with how the Values of an array are displayed in a browser, I currently have the array for the Cards set like this:

  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而不是被以字符串显示,所以看起来他们没有被考虑。我想知道是否有办法有显示,而不是没有松动的数值字符串。我试图反转像这样

It does what it is suppose to do and does the math how it should. It gives the Jack, Queen, and king the values it should so if a King is paired with a seven it will add 17. My problem with this is that in the browser the Jack, Queen, and King, are displayed as "10" instead of being displayed with the string, so it looks like they are not being accounted for. I would like to know if there is a way to have the string displayed instead without loosing the numeric value. I tried inverting like so

10 => 'King',

和做这个国王被显示,但该数值不占,所以有7人会打败了国王的人。

And by doing this King gets displayed but the numeric value is not accounted, so a person with a 7 will beat a person with a King.

任何帮助将大大AP preciated ...谢谢

Any help will be greatly appreciated... Thanks

的编辑的 *

EDIT *

我加入了全code,有四种不同的PHP文件。

I am adding in the full code, there are four different php files.

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

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

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

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

这将显示在像这样一个浏览器:

This will display in a browser like so:

玩家一个人的心10和俱乐部的2
球员2有红心10和红心3
玩家2胜

Player one has 10 of Hearts and a 2 of Clubs Player two has 10 of Hearts and a 3 of Hearts Player 2 wins

问题是10级的应该是一个国王/女王/插孔之一

The problem is one of the 10's should have been a king/queen/jack

推荐答案

您已经做了多种选择。我建议建立一个二维数组,如:

You have multiple options for doing that. I suggest to build a 2D array like:

$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 */
);

然后就可以用得到的名称和值轻松:

Then you can get name and value easily with:

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

您存储卡上的密钥,如果你想显示你使用的名称 $这个 - &GT;卡['{the_key}'] ['名'] 如果你想显示的值或计算它使用 $这个 - &GT;卡['{the_key}'] ['值']

You store the keys of the cards and if you like to display the name you use $this->cards['{the_key}']['name'] and if you want to display the value or calculate with it use $this->cards['{the_key}']['value'].

这篇关于PHP中数组赋值数值为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆