我正在尝试使用关联数组中的值来计算用户是否在扑克牌中拥有顺子。
每个玩家的手牌都在同一个数组中,其结构类似于:
<?php
// Note: the first 5 values of each player's hand are identical because those are
// the dealer's cards (the cards on the "board" that everyone can see).
// The last two values represent the player's "hole cards" that only they can see.
// Also note: 11 = Jack, 12 = Queen, 13 = King, 14 = Ace.
$hands = array(
// Player 0
0 => array(
0 => array("value_num" => 6),
1 => array("value_num" => 7),
2 => array("value_num" => 8),
3 => array("value_num" => 9),
4 => array("value_num" => 14),
5 => array("value_num" => 10),
6 => array("value_num" => 8),
),
// Player 1
1 => array(
0 => array("value_num" => 6),
1 => array("value_num" => 7),
2 => array("value_num" => 8),
3 => array("value_num" => 9),
4 => array("value_num" => 14),
5 => array("value_num" => 10),
6 => array("value_num" => 13),
),
// Player 2
2 => array(
0 => array("value_num" => 6),
1 => array("value_num" => 7),
2 => array("value_num" => 8),
3 => array("value_num" => 9),
4 => array("value_num" => 14),
5 => array("value_num" => 5),
6 => array("value_num" => 12),
),
// Player 3
3 => array(
0 => array("value_num" => 6),
1 => array("value_num" => 7),
2 => array("value_num" => 8),
3 => array("value_num" => 9),
4 => array("value_num" => 14),
5 => array("value_num" => 5),
6 => array("value_num" => 13),
),
);
我编写的用于确定玩家是否有顺子的函数如下所示:
<?php
/**
* @return
* The highest card value of the player's straight, if they have a straight.
* Otherwise, 0.
*/
function has_straight($cards, $player_id, $required_consecutive_cards = 5) {
// Extract the "value_num" values to calculate the straight.
for ($i = 0; $i < count($cards[$player_id]); $i++) {
$values[$player_id][$i] = $cards[$player_id][$i]['value_num'];
}
// Check to see if the player has an Ace within their hand.
$has_ace[$player_id] = array_search(14, $values[$player_id]);
// If the player has an Ace, push a 1 into their $value array in case they also
// have 2, 3, 4, and 5 in their hand.
if ($has_ace[$player_id] !== FALSE) {
array_push($values[$player_id], 1);
}
// Only check the player's unique values. Right now we're not concerned with
// pairs or anything else.
$player_cards[$player_id] = array_unique($values[$player_id]);
// Reverse sort the player's unique values to make counting sequences easier.
rsort($player_cards[$player_id]);
// Set a number to increment if one card is sequential to the other.
$increment_value[$player_id] = 1;
foreach ($player_cards[$player_id] as $key => $value) {
$next_key = $key + 1;
$next_value = $value - 1;
if (!array_key_exists($next_key, $player_cards[$player_id])) {
return 0;
}
if ($player_cards[$player_id][$next_key] == $next_value) {
$increment_value[$player_id]++;
}
else {
$increment_value[$player_id] = 1;
unset($player_cards[$player_id][$key]);
}
if ($increment_value[$player_id] == $required_consecutive_cards) {
// Reverse sort what's left of the values so that the first one can be
// the value returned.
rsort($player_cards[$player_id]);
return $player_cards[$player_id][0];
}
}
return 0;
}
因此,要确定玩家 0 是否有顺子,您可以运行以下命令:
<?php
$player_to_check = 0;
print has_straight($hands, $player_to_check);
...这应该导致10
作为输出(10
是玩家 0 顺子中的最高值 -- 6
, 7
, 8
, 9
, 10
)。
我遇到的问题是,例如,当您检查玩家 1 的顺子(应该与玩家 0 的顺子相同)时,输出将14
是10
.
发生这种情况的原因是因为玩家 113
手中也有 a,并且由于13
与 连续14
,14
因此返回而不是10
(即使14
不在玩家 1 的顺子之内)。
比较玩家 3 和 4 时可以看到同样的问题。
我需要在函数内部做什么才能使其返回一组 5 个序列号中的最大值(而不是找到的第一组序列号的最大值)?