[ Поиск ] - [ Пользователи ] - [ Календарь ]
Полная Версия: Помогите с кодом
djnightart
Всем привет, прошу помощи. Есть данный код, который определяет комбинации в покере. Но он рассчитан на работу с 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()
{
// is Royal Flush
echo $this->_isRoyalFlush() ? 'royal flush' : 'not royal flush';
echo '<br>';

// is Quad (four of kind)
echo $this->_isQuad() ? 'quad' : 'not quad';
echo '<br>';

// is StraightFlush
echo $this->_isStraightFlush() ? 'straight flush' : 'not straight flush';
echo '<br>';

// is Full House
echo $this->_isFullHouse() ? 'full house' : 'not full house';
echo '<br>';

// is Flush
echo $this->_isFlush() ? 'flush' : 'not flush';
echo '<br>';

// is Straight
echo $this->_isStraight() ? 'straight' : 'not straigt';
echo '<br>';

// is Three of Kind
echo $this->_isThreeOfKind() ? 'three of kind' : 'not three of kind';
echo '<br>';

// is Two Pairs
echo $this->_isTwoPairs() ? 'two pairs' : 'not two pairs';
echo '<br>';

// is one Pair
echo $this->_isPair() ? 'pair' : 'not pair';
echo '<br>';

// is High Card
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])) // 1=2=3 and 4=5
|| ($ranks[0] == $ranks[1]) && ($ranks[2] == $ranks[3] and $ranks[3] == $ranks[4])) // 1=2 and 3=4=5
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 Ace is low card in straight
if ($key = array_search(14, $ranks)) {
$tempRanks = $ranks;
unset($tempRanks[$key]);
if (array(2, 3, 4, 5) == $tempRanks) {
return true;
}
unset($tempRanks);
}

// if Ace is high card - default algorithm
$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])
// exclude full house
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();
Быстрый ответ:

 Графические смайлики |  Показывать подпись
Здесь расположена полная версия этой страницы.
Invision Power Board © 2001-2024 Invision Power Services, Inc.