php 实现扑克牌比大小
时间: 2024-02-04 19:03:20 浏览: 72
大强学易之扑克比大小.zip易语言项目例子源码下载
好的,我可以为您提供一个更完整的 PHP 扑克牌比大小的代码示例:
```php
/**
* 将扑克牌字符串转换为数组
* @param string $cardsStr 扑克牌字符串,如 "10H 9D AS KC"
* @return array 扑克牌数组,如 array("10H", "9D", "AS", "KC")
*/
function convertCardsToArray($cardsStr) {
$cards = explode(" ", $cardsStr);
return $cards;
}
/**
* 获取扑克牌的点数
* @param string $card 扑克牌,如 "10H"
* @return int 扑克牌点数,如 10
*/
function getCardValue($card) {
$cardValues = array(
'A' => 14,
'K' => 13,
'Q' => 12,
'J' => 11,
'10' => 10,
'9' => 9,
'8' => 8,
'7' => 7,
'6' => 6,
'5' => 5,
'4' => 4,
'3' => 3,
'2' => 2
);
return $cardValues[$card[0]];
}
/**
* 判断扑克牌是否为同花
* @param array $cards 扑克牌数组
* @return bool 是否为同花
*/
function isFlush($cards) {
$suits = array();
foreach ($cards as $card) {
$suits[] = $card[1];
}
return count(array_unique($suits)) == 1;
}
/**
* 判断扑克牌是否为顺子
* @param array $cards 扑克牌数组
* @return bool 是否为顺子
*/
function isStraight($cards) {
$cardValues = array();
foreach ($cards as $card) {
$cardValues[] = getCardValue($card);
}
sort($cardValues);
$minValue = $cardValues[0];
for ($i = 1; $i < count($cardValues); $i++) {
if ($cardValues[$i] != $minValue + $i) {
return false;
}
}
return true;
}
/**
* 获取扑克牌的组合类型
* @param array $cards 扑克牌数组
* @return string 组合类型,如 "同花顺"、"四条"、"葫芦"、"同花"、"顺子"、"三条"、"两对"、"一对"、"高牌"
*/
function getHandType($cards) {
$cardCounts = array();
foreach ($cards as $card) {
$value = getCardValue($card);
if (!isset($cardCounts[$value])) {
$cardCounts[$value] = 0;
}
$cardCounts[$value]++;
}
arsort($cardCounts);
$cardValues = array_keys($cardCounts);
$cardCounts = array_values($cardCounts);
$maxCount = $cardCounts[0];
$secondCount = isset($cardCounts[1]) ? $cardCounts[1] : 0;
$thirdCount = isset($cardCounts[2]) ? $cardCounts[2] : 0;
$fourthCount = isset($cardCounts[3]) ? $cardCounts[3] : 0;
$fifthCount = isset($cardCounts[4]) ? $cardCounts[4] : 0;
$isFlush = isFlush($cards);
$isStraight = isStraight($cards);
if ($isFlush && $isStraight) {
return "同花顺";
} elseif ($maxCount == 4) {
return "四条";
} elseif ($maxCount == 3 && $secondCount == 2) {
return "葫芦";
} elseif ($isFlush) {
return "同花";
} elseif ($isStraight) {
return "顺子";
} elseif ($maxCount == 3) {
return "三条";
} elseif ($maxCount == 2 && $secondCount == 2) {
return "两对";
} elseif ($maxCount == 2) {
return "一对";
} else {
return "高牌";
}
}
/**
* 比较两手扑克牌的大小
* @param array $hand1 手牌1,如 array("10H", "9D", "AS", "KC")
* @param array $hand2 手牌2,如 array("10H", "9D", "AS", "KC")
* @return int 比较结果,1 表示手牌1大,-1 表示手牌2大,0 表示平局
*/
function compareHands($hand1, $hand2) {
$hand1Type = getHandType($hand1);
$hand2Type = getHandType($hand2);
if ($hand1Type != $hand2Type) {
$handTypes = array(
"高牌" => 1,
"一对" => 2,
"两对" => 3,
"三条" => 4,
"顺子" => 5,
"同花" => 6,
"葫芦" => 7,
"四条" => 8,
"同花顺" => 9
);
return $handTypes[$hand1Type] > $handTypes[$hand2Type] ? 1 : -1;
} else {
$cardValues1 = array();
$cardValues2 = array();
foreach ($hand1 as $card) {
$cardValues1[] = getCardValue($card);
}
foreach ($hand2 as $card) {
$cardValues2[] = getCardValue($card);
}
rsort($cardValues1);
rsort($cardValues2);
for ($i = 0; $i < count($cardValues1); $i++) {
if ($cardValues1[$i] > $cardValues2[$i]) {
return 1;
} elseif ($cardValues1[$i] < $cardValues2[$i]) {
return -1;
}
}
return 0;
}
}
// 示例用法
$hand1 = convertCardsToArray("10H 9D AS KC");
$hand2 = convertCardsToArray("JD QS KH AC");
$result = compareHands($hand1, $hand2);
if ($result == 1) {
echo "手牌1大";
} elseif ($result == -1) {
echo "手牌2大";
} else {
echo "平局";
}
```
这个示例代码中,我们实现了扑克牌字符串到数组的转换、获取扑克牌点数、判断扑克牌是否为同花或顺子、获取扑克牌的组合类型、比较两手扑克牌大小的功能。您可以通过调用这些函数来完成扑克牌比大小的操作。需要注意的是,这个示例代码只考虑了两手牌的比较,如果您需要比较多手牌,需要进行相应的扩展。
阅读全文