拆分一个数组,并在每个数组的末尾添加一个字母


Split an array and add a letter to the end of every piece

标题可能会混淆,我将尝试用一个例子来解释:

我有一个数组,包含了德克萨斯州所有可能的1326个先发手,按强度排序

$array = array("AA","AA",..."KK","KK",..."AKs",.."AKo",..);

我想创建一个新的数组,将每只手分成2张牌,并在上面添加一个字母(套装)。套装是4:c,s,d,h所以最后的数组应该变成类似于:

$array1 = array(array("Ac","As"), array("As","Ad"),..., array("Ac","Kc"), array("Ac","Ks"));

附言:当在$array中我使用sintax AKS时,它意味着相同的套装,o代表offsuited。

提前感谢

编辑

$array1应该包含两张牌的所有1326个组合,一副52张牌是可能的。http://pastebin.com/kde4qjLD

$array的排序应按强度的顺序(每只手的预期值)。排序顺序正确的数组可以在这里找到:http://pastebin.com/ydfd08Cw

$array1 = array(
    array( "As", "Ad" ),
    array( "Ad", "Ac" ),
    array( "Ac", "Ah" ),
    array( "Ah", "As" ),
    array( "As", "Ac" ),
    array( "Ac", "Ad" ),
    array( "As", "Ks" ),
    array( "Ad", "Kd" ),
//etc
);
// our Card like Ah or 2c
class Card {
  /**
   * @var string 'A','K','Q','J','T','9','8','7','6','5','4','3','2'
   */
  public $sign;
  /**
   * @var string 'h','c','d','s'
   */
  public $suit;
  public function __construct($sign, $suit) {
    $this->sign = $sign;
    $this->suit = $suit;
  }
  public function __toString() {
    return $this->sign.$this->suit;
  }
  /**
   * Computes a unique-hash of two play cards
   * 
   * returns something like AhAs (ordered by Sign of the Cards)
   * if the sign of the cards is the same, it is ordered by suite
   *
   * every hash from a tupel of cards should return the same hash!
   * @returns string
   */
  public function hash(Card $cardB) {
    $cardA = $this;
    if ($cardA->sign === $cardB->sign) {
      $cmp = strcmp($cardA->suit, $cardB->suit);
    } else {
      $cmp = strcmp($cardA->sign, $cardB->sign);
    }
    return $cmp > 0
        ? $cardA.$cardB
        : $cardB.$cardA
    ;
  }
}
// create all possible cards
$cards = array();
foreach (array('A','K','Q','J','T','9','8','7','6','5','4','3','2') as $sign) {
  foreach (array('h','c','d','s') as $suit) {
    $cards[] = new Card($sign,$suit);
  }
}
/*
  create combinations from all cards ($card) with all cards ($secondcard)
  we don't want to combine:
    - cards which are the same
    - cards which have alreay been combined (saved in index)
*/
$index = array();
$combinations = array();
foreach ($cards as $card) {
  foreach ($cards as $secondCard) {
    if ($card !== $secondCard && !array_key_exists($hash = $card->hash($secondCard), $index)) {
      $combinations[] = array($card, $secondCard);
      $index[$hash] = TRUE;
    }
  }
}
// print results
var_dump(count($combinations));
foreach ($combinations as $cards) {
  print $cards[0].$cards[1]."'n";
}

我希望这些评论能解释它的工作方式。当然,还有一种更好(更复杂)的方法可以做到这一点。这件事的"关键"是从两张卡中创建唯一的哈希,这样同一张卡中的每个元组都会返回相同的哈希。

希望这有助于理解

附言:这浪费了时间和空间,但

是可以理解的

开始:

$suits = array('c','s','d','h');
$faces = array('A','K','Q','J','10','9','8','7','6','5','4','3','2');
function generate_permutations($elems){
    $result = array();
    for ($i=0;$i<count($elems);$i++){
        for ($j=$i+1;$j<count($elems);$j++){
            $result[] = array($elems[$i],$elems[$j]);
        }
    }
    return $result;
}
$cards = array();
foreach ($suits as $suit){
    foreach ($faces as $face){
        $cards[] = $face.$suit;
    }
}
$new_array = generate_permutations($cards);
$newarray=array();
while (true) {
  $a=array_shift($array);
  if (!$a) break;
  $b=substr($a,1);
  $a=substr($a,0,1);
  $newarray[]=array (
    $a.'c', $a.'s',$a.'d',$a.'h',
    $b.'c', $a.'s',$b.'d',$b.'h'
  );
}