<?php
function generatePokerCombinations() {
// 定义扑克牌的花色
$suits = ['H', 'D', 'C', 'S']; // 红桃、方块、梅花、黑桃
// 定义扑克牌的点数范围
$ranks = range(3, 15); // 3 - A(14),小王(15)
// 定义随心配
$wildcard = 0;
// 生成所有可能的牌
$allCards = [];
foreach ($suits as $suit) {
foreach ($ranks as $rank) {
$allCards[] = $suit . $rank;
}
}
// 确保数组中的每个元素都是数组
function ensureArrayElements($arr) {
$newArr = [];
foreach ($arr as $key => $element) {
if (!is_array($element)) {
echo "Warning: Element at key $key is not an array. Converting to array.\n";
$element = [$element];
}
$newArr[] = $element;
}
return $newArr;
}
// 生成所有可能的牌型组合
$combinations = [];
// 没有随心配的情况
// 顺子(长度为 5)
for ($start = 3; $start <= 11; $start++) {
foreach ($suits as $suit) {
$combination = [];
for ($i = 0; $i < 5; $i++) {
$combination[] = $suit . ($start + $i);
}
$combinations[] = ['type' => '顺子', 'cards' => $combination];
}
}
// 同花顺(长度为 5)
for ($start = 3; $start <= 11; $start++) {
foreach ($suits as $suit) {
$combination = [];
for ($i = 0; $i < 5; $i++) {
$combination[] = $suit . ($start + $i);
}
$combinations[] = ['type' => '同花顺', 'cards' => $combination];
}
}
// 钢板(三顺,长度为 6)
for ($start = 3; $start <= 10; $start++) {
foreach ($suits as $suit1) {
foreach ($suits as $suit2) {
foreach ($suits as $suit3) {
$combination = [
$suit1 . $start,
$suit2 . $start,
$suit3 . $start,
$suit1 . ($start + 1),
$suit2 . ($start + 1),
$suit3 . ($start + 1)
];
$combinations[] = ['type' => '钢板', 'cards' => $combination];
}
}
}
}
// 3 连对
for ($start = 3; $start <= 11; $start++) {
foreach ($suits as $suit1) {
foreach ($suits as $suit2) {
$combination = [
$suit1 . $start,
$suit2 . $start,
$suit1 . ($start + 1),
$suit2 . ($start + 1),
$suit1 . ($start + 2),
$suit2 . ($start + 2)
];
$combinations[] = ['type' => '3连对', 'cards' => $combination];
}
}
}
// 包含 1 张随心配的情况
$withOneWildcard = [];
if (is_array($combinations)) {
foreach ($combinations as $key => $combinationInfo) {
$combination = $combinationInfo['cards'];
if (!is_array($combination)) {
echo "Error: Element at key $key in \$combinations is not an array when adding one wildcard.\n";
var_dump($combination);
echo "\n";
} else {
for ($i = 0; $i <= count($combination); $i++) {
$newCombination = $combination;
array_splice($newCombination, $i, 0, $wildcard);
$withOneWildcard[] = ['type' => $combinationInfo['type'] . '(含1张随心配)', 'cards' => $newCombination];
}
}
}
} else {
echo "Error: \$combinations is not an array when adding one wildcard.\n";
}
$combinations = ensureArrayElements(array_merge($combinations, $withOneWildcard));
// 包含 2 张随心配的情况
$withTwoWildcards = [];
if (is_array($combinations)) {
foreach ($combinations as $key => $combinationInfo) {
$combination = $combinationInfo['cards'];
if (!is_array($combination)) {
echo "Error: Element at key $key in \$combinations is not an array when adding two wildcards.\n";
var_dump($combination);
echo "\n";
} else {
for ($i = 0; $i <= count($combination); $i++) {
for ($j = $i; $j <= count($combination); $j++) {
$newCombination = $combination;
array_splice($newCombination, $i, 0, $wildcard);
array_splice($newCombination, $j + 1, 0, $wildcard);
$withTwoWildcards[] = ['type' => $combinationInfo['type'] . '(含2张随心配)', 'cards' => $newCombination];
}
}
}
}
} else {
echo "Error: \$combinations is not an array when adding two wildcards.\n";
}
$combinations = ensureArrayElements(array_merge($combinations, $withTwoWildcards));
// 打印所有组合
foreach ($combinations as $index => $combinationInfo) {
if (!is_array($combinationInfo['cards'])) {
echo "Error: Element at index $index in \$allCombinations is not an array.\n";
var_dump($combinationInfo['cards']);
echo "\n";
} else {
echo "组合 ". ($index + 1). " - 牌型: ". $combinationInfo['type']. ",牌: ". implode(', ', $combinationInfo['cards']). "\n";
}
}
}
// 调用函数生成并输出牌型组合
generatePokerCombinations();
?>