三张牌吹牌排名?PHP


Three card brag hand ranking? PHP

我发现很难找到一个卡片排名教程,甚至很难找到一些源代码来为我指明正确的方向。

我基本上是朝着用多个in_array创建多个函数的方向前进,并从头开始编写它们,因为这样做会让三个函数变得容易。示例

    function is_trip($card1, $card2, $card3) 
    {
        if (in_array($card1, $aces) && in_array($card2, $aces) && in_array($card3, $aces)) 
        {
            $score = 9500;
            $rank = 'Three Aces';
        }
        if (in_array($card1, $kings) && in_array($card2, $kings) && in_array($card3, $kings)) 
        {
            $score = 9000;
            $rank = 'Three Kings';
        }
    } And so on ...

所以这很可能适用于trips,但对于直接刷新,我会使用一种按数字组织卡片的方法,因为它们在数组中的顺序是合适的。

因此,直接刷新有望像$highcard + $lowcard / 2 == $midcard一样简单,如果这是真的,那么你就有了直接刷新。

至于一个直数组,我被卡住了,很可能不得不按照我目前的想法使用一个数组,但在最简单的时候,写这个数组看起来像是很多代码。。

对于刷新,使用in_array并不困难,因为我只需要在in_array中确定1-13 14-26 27-39 40-52的范围来确定刷新,但随后我需要$highcard$midcard值来确定与其他值的刷新。

你可能会想到这一点,What's his question??

好吧,我的问题是……我对卡片进行排名的方式正确吗?我应该使用桶计数方法将排名放入位代码中并使用查找表吗?或者,如果我的方法完全愚蠢,你对我应该去哪里有什么建议吗。。

提前感谢您的帮助。

这是非常粗糙的,未经测试,但像这样的东西呢:-

<?php
$hand = new Hand;
$hand->addCard(new Card(RankInterface::ACE, SuitInterface::SPADE));
$hand->addCard(new Card(RankInterface::QUEEN, SuitInterface::HEART));
$hand->addCard(new Card(RankInterface::KING, SuitInterface::CLUB));
$isFlush = isFlush($hand);

使用类似的东西:-

<?php
namespace Card;
interface SuitInterface {
    const
        SPADE   = 'spade',
        HEART   = 'heart',
        DIAMOND = 'diamond',
        CLUB    = 'club';
}
interface RankInterface {
    const
        JOKER   = 0,
        ACE     = 1,
        TWO     = 2,
        THREE   = 3,
        FOUR    = 4,
        FIVE    = 5,
        SIX     = 6,
        SEVEN   = 7,
        EIGHT   = 8,
        NINE    = 9,
        TEN     = 10,
        JACK    = 11,
        QUEEN   = 12,
        KING    = 13;
}
class Card {
    protected
        $rank,
        $suit;
    public function __construct($rank, $suit) {
        $this->rank = $rank;
        $this->suit = $suit;
    }
    public function getRank() {
        return $this->rank;
    }
    public function getSuit() {
        return $this->suit;
    }
    public function isSameRank(Card $card) {
        return $this->getRank() === $card->getRank();
    }
    public function isSameSuit(Card $card) {
        return $this->getSuit() === $card->getSuit();
    }
}
class Hand
{
    protected
        $storage = array();
    public function addCard(Card $card) {
        $this->storage[] = $card;
        return $this;
    }
    public function asArray() {
        return $this->storage;
    }
}
function isFlush(Hand $hand) {
    $cards = $hand->asArray();
    $first = array_shift($cards);
    foreach($cards as $card) {
        if( ! $first->isSameSuit($card)) {
            return false;
        }
    }
    return true;
}

然后,您只需要为各种有效的手/组合添加一些单独的逻辑。