从数组中筛选密钥?php扑克
现在我知道了寻找直线背后的基本逻辑,我假设它包括一个伪从数组中筛选密钥?php扑克,php,Php,现在我知道了寻找直线背后的基本逻辑,我假设它包括一个伪 function is_straight(array $cards) { sort($cards); if(($cards[4] - $cards[0]) == 5) { //Code to make sure the cards in between are increment //is straight.
function is_straight(array $cards) {
sort($cards);
if(($cards[4] - $cards[0]) == 5) {
//Code to make sure the cards in between are increment
//is straight.
}
}
理论上,5张卡的支票是有效的
但是,如何从7张牌的阵列中消除牌,从而找到一个直牌呢
我是否需要单独检查7张牌阵列中的所有5手牌组合
那么,从$cards数组中删除两张卡,并检查该组合是否为直行
因此,我有点停留在逻辑方面,而不是代码方面。在伪代码中
#filter doubles
cards = array_unique(cards)
sort(cards)
foreach cards as key, value:
if not key_exists(cards, key+4):
return false
if cards[key+4] == value + 4:
return true
更长可能更明确的版本
#filter doubles
cards = array_unique(cards)
sort(cards)
straight_counter = 1
foreach cards as key, value:
if not key_exists(cards, key+1):
return false
# is the following card an increment to the current one
if cards[key+1] == value + 1:
straight_counter++
else:
straight_counter = 1
if straight_counter == 5:
return true
假设
$cards
是一个包含从1到13的cards值的数组,我认为您需要使用4而不是5的差值进行计算:
5-1=46-2=4
7-3=4
等等 您还需要为10,J,Q,K,a添加一个特定的逻辑 但对于您的具体问题,您认为:
function is_straight(array $cards) {
sort($cards);
if((($cards[4] - $cards[0]) == 4) ||
(($cards[5] - $cards[1]) == 4) ||
(($cards[6] - $cards[2]) == 4)) {
//Code to make sure the cards in between are increment
//is straight.
}
}
可能是重复的。他的问题是基于5张牌的评估,我的是基于7张牌的评估。啊!我错过了,但看起来很相似。是的,我在计算5张牌的评估时没有太大困难,我要求的是攻击7张牌的逻辑方法,而不是对每个可能的组合使用5张牌的方法。非常出色,非常符合逻辑。ThanksI最终设法找出了4是合适的数字,并且编码方式与您介绍的方式基本相同。谢谢你花时间回答我。
function is_straight(array $array) {
$alpha = array_keys(array_count_values($array));
sort($alpha);
if (count($alpha) > 4) {
if (($alpha[4] - $alpha[0]) == 4) {
$result = $alpha[4];
return $result;
}
if (count($alpha) > 5) {
if (($alpha[5] - $alpha[1]) == 4) {
$result = $alpha[5];
return $result;
}
}
if (count($alpha) > 6) {
if (($alpha[6] - $alpha[2]) == 4) {
$result = $alpha[6];
return $result;
}
}
}
}