Всем привет, прошу помощи. Есть данный код, который определяет комбинации в покере. Но он рассчитан на работу с 5 картами. как переписать его для обработки 7 или 9 карт?
<?php
class Poker
{
protected $_cards = array();
protected $_ranks = array();
protected $_suits = array();
public function __construct(array $cards)
{
$this->_cards = $cards;
$rank = null;
foreach($cards as $card) {
switch (strtolower($card['rank'])) {
case 't':
$rank = 10;
break;
case 'j':
$rank = '11';
break;
case 'q':
$rank = '12';
break;
case 'k':
$rank = '13';
break;
case 'a':
$rank = '14';
break;
;
default:
$rank = $card['rank'];
break;
}
$this->_ranks[] = $rank;
$this->_suits[] = $card['suit'];
}
}
public function checkCombination()
{
echo $this->_isRoyalFlush() ? 'royal flush' : 'not royal flush';
echo '<br>';
echo $this->_isQuad() ? 'quad' : 'not quad';
echo '<br>';
echo $this->_isStraightFlush() ? 'straight flush' : 'not straight flush';
echo '<br>';
echo $this->_isFullHouse() ? 'full house' : 'not full house';
echo '<br>';
echo $this->_isFlush() ? 'flush' : 'not flush';
echo '<br>';
echo $this->_isStraight() ? 'straight' : 'not straigt';
echo '<br>';
echo $this->_isThreeOfKind() ? 'three of kind' : 'not three of kind';
echo '<br>';
echo $this->_isTwoPairs() ? 'two pairs' : 'not two pairs';
echo '<br>';
echo $this->_isPair() ? 'pair' : 'not pair';
echo '<br>';
echo $this->_isHighCard() ? 'high card' : 'not high card';
echo '<br>';
}
protected function _isRoyalFlush()
{
return ($this->_isFlush() && $this->_isStraight() && array_search(14, $this->_ranks) && array_search('13', $this->_ranks));
}
protected function _isQuad()
{
$test = $this->_ranks;
$uniqueElementsCount = array();
foreach($this->_ranks as $key => $card) {
$test = $this->_ranks;
unset($test[$key]);
$uniqueElementsCount[] = count(array_unique($test));
}
return 1 === min($uniqueElementsCount);
}
protected function _isStraightFlush()
{
return ($this->_isFlush() && $this->_isStraight());
}
protected function _isFullHouse()
{
$ranks = $this->_ranks;
sort($ranks);
if ((($ranks[0] == $ranks[1] && $ranks[1] == $ranks[2]) && ($ranks[3] == $ranks[4]))
|| ($ranks[0] == $ranks[1]) && ($ranks[2] == $ranks[3] and $ranks[3] == $ranks[4]))
return true;
return false;
}
protected function _isFlush()
{
$suits = $this->_suits;
sort($suits);
if ($suits[0] === $suits[4])
return true;
return false;
}
protected function _isStraight()
{
$ranks = $this->_ranks;
sort($ranks);
if ($key = array_search(14, $ranks)) {
$tempRanks = $ranks;
unset($tempRanks[$key]);
if (array(2, 3, 4, 5) == $tempRanks) {
return true;
}
unset($tempRanks);
}
$min = $ranks[0];
foreach($ranks as $key => $value) {
$ranks[$key] -= $min;
if ($key != $ranks[$key])
return false;
}
return true;
}
protected function _isThreeOfKind()
{
$ranks = $this->_ranks;
sort($ranks);
if (($ranks[0] == $ranks[1] and $ranks[1] == $ranks[2])
|| ($ranks[1] == $ranks[2] and $ranks[2] == $ranks[3])
|| ($ranks[2] == $ranks[3] and $ranks[3] == $ranks[4]))
return true;
return false;
}
protected function _isTwoPairs()
{
$ranks = $this->_ranks;
foreach($ranks as $key => $rank) {
$testRanks = $ranks;
unset($testRanks[$key]);
sort($testRanks);
if (($testRanks[0] == $testRanks[1])
and ($testRanks[2] == $testRanks[3])
and ($testRanks[0] != $testRanks[2])
and ($testRanks[0] != $ranks[$key])
and ($testRanks[2] != $ranks[$key])
)
return true;
}
return false;
}
protected function _isPair()
{
return 4 === count(array_unique($this->_ranks));
}
protected function _isHighCard()
{
return true;
}
}
$cards = array(
array('suit' => 's', 'rank' => '3'),
array('suit' => 's', 'rank' => '7'),
array('suit' => 's', 'rank' => 'a'),
array('suit' => 's', 'rank' => 't'),
array('suit' => 's', 'rank' => 't')
);
$poker = new Poker($cards);
$poker->checkCombination();