php5 斗地主发牌叫牌逻辑

	// PHP5+
class PokerGame {
    private $playerCount;  //玩家人数;
    private $landlordNo;   //第几人当地主

    public function __construct($playerCount = 3, $landlordNo = 3) {
        $this->playerCount = $playerCount;
        $this->landlordNo  = $landlordNo;
    }
    private function getPokerConfig() {
        return array(
            'faces'  => array('3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A', '2'),
            'jokers' => array('JOKER大王', 'JOKER小王'),
            'suits'  => array('♦', '♣', '♥', '♠')
        );
    }
    private function generateShuffledCards() {
        $config = $this->getPokerConfig();
        $cards  = array();
        foreach ($config['suits'] as $suit) {
            foreach ($config['faces'] as $face) {
                $cards[] = $suit . $face;
            }
        }
        foreach ($config['jokers'] as $joker) {
            $cards[] = $joker;
        }
        shuffle($cards);
        return $cards;
    }
    private function dealCards() {
        $cardDeck = $this->generateShuffledCards();

        $cardsPerPerson = 17;
        $bottomCardNum  = 3;

        if (count($cardDeck) !== $this->playerCount * $cardsPerPerson + $bottomCardNum) {
            die('牌数量与玩家配置不匹配');
        }
        $playerCards = array();
        for ($i = 0; $i < $this->playerCount; $i++) {
            $offset        = $i * $cardsPerPerson;
            $playerCards[] = array_slice($cardDeck, $offset, $cardsPerPerson);
        }
        $bottomOffset = $this->playerCount * $cardsPerPerson;
        $bottomCards  = array_slice($cardDeck, $bottomOffset, $bottomCardNum);
        return array($playerCards, $bottomCards);
    }
    private function getCardSortWeight($card) {
        $config = $this->getPokerConfig();
        $faceWeight = array_flip($config['faces']);
        foreach ($faceWeight as $face => $weight) {
            $faceWeight[$face] = $weight + 1;
        }

        $littleJokerWeight = count($config['faces']) + 1;
        $bigJokerWeight    = $littleJokerWeight + 1;

        if (strpos($card, 'JOKER大王') !== false) {
            return $bigJokerWeight;
        }
        if (strpos($card, 'JOKER小王') !== false) {
            return $littleJokerWeight;
        }

        $face = str_replace($config['suits'], '', $card);
        return isset($faceWeight[$face]) ? $faceWeight[$face] : 0;
    }
    private function sortPlayerCards(&$cards) {
        usort($cards, function ($cardA, $cardB) {
            $weightA = $this->getCardSortWeight($cardA);
            $weightB = $this->getCardSortWeight($cardB);
            return $weightB - $weightA;
        });
    }
    public function callLandlord() {
        list($playerCards, $bottomCards) = $this->dealCards();
        
        $landlordIndex = $this->landlordNo - 1;
        if (isset($playerCards[$landlordIndex])) {
            $playerCards[$landlordIndex] = array_merge($playerCards[$landlordIndex], $bottomCards);
        }
        foreach ($playerCards as &$hand) {
            $this->sortPlayerCards($hand);
        }
        unset($hand);
        return $playerCards;
    }

}

$pokerGame = new PokerGame(3, 3);

header("Content-type: application/json;charset=UTF-8");
$result = array(
    'code' => 200,
    'msg'  => '叫牌结果(地主为第3位玩家)',
    'data' => $pokerGame->callLandlord()
);

echo json_encode($result, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);

  

posted @ 2026-05-29 16:58  钢锅  阅读(3)  评论(0)    收藏  举报