我正在尝试使用关联数组中的值计算用户是否在扑克手牌中有直牌。
每个玩家的手牌都在相同的数组中,其结构类似于:
<?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
。
发生这种情况的原因是因为玩家1手中也有13
,并且由于13
与14
连续,因此返回14
而不是10
(即使14
不在玩家1的直线内)。
同样的问题也出现在玩家3和4的对比中。
我需要在函数内做什么才能使它返回5个序列号中的最大值(而不是找到的第一组序列号的最大值)?
直牌有5张连续的牌,其和总是遵循这个规则:(n*5)+10。其中n是起始牌。金额越高,起跑牌就越高。所以这可能是一个解决方案:
foreach($player_id as $id){
//GET THE CARD VALUES FROM THIS PLAYER
$cards=array();
foreach($player_cards[$id] as $tmp){
$cards[] = $tmp['value_num'];
}
//ONLY THE UNIQUE CARDS
$cards=array_unique($cards);
//SORT THEM LOW TO HIGH
sort($cards);
//Get the maximum amount of possabilities for a straight from
//the maximum 7 cards in a hand
$max=count($cards)-4;
//IF TOO LITTLE CARDS, CONTINUE WITH NEXT PLAYER
if($max<=0)continue;
//SET SUM and HIGHEST SUM
$sum=0;
$hsum=0;
//LOOP THE POSSIBLE STRAIGHTS
for($i=1;$i<$max+1;++$i){
//GET 5 CONSECUTIVE CARDS
$straight=array_slice($cards,$i-1,5);
$sum_should_be=($straight[0]*5)+10;
$sum = array_sum($straight);
//IF SUM NOT CORRECT, CONTINUE WITH NEXT POSSIBLE STRAIGHT
if($sum!=$sum_should_be)continue;
//GET THE HIGHEST STRAIT FOR USER
if($sum>$hsum)$hsum=$sum;
//ADD STRAIGHT SCORE TO ARRAY FOR COMPARISON
$straight_score[$id]=$straight[4];
}
}
它会给你:
Player 0. Straight score: 10
Player 1. Straight score: 10
Player 2. Straight score: 9
Player 3. Straight score: 9
看一下php的Max函数
http://php.net/manual/en/function.max.php这个怎么样?我不确定我是否理解了这些问题,我也没有测试它,但也许它会有所帮助。
$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) {
if($increment_value[$player_id] == 1) {
$top_card_key = $player_cards[$player_id][key]
$top_card[$top_card_key] = 1;
}
$increment_value[$player_id]++;
if(isset($top_card[$top_card_key])) {
$top_card[$top_card_key]++;
}
}
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]);
// Foreach on top_card, check which one == 5 and display it
// return the top_card;
}
这是你的答案,我尽我所能评论它。注意到这里有一部分不需要同花顺,这是真的,但我也擅自把牌分组了。您将在输出中看到这一点。顶部数组的键是牌的值,该项的数组计数是多张牌的数量,我认为你可以弄清楚如何处理它们。
foreach( $hands as $cards ){
rsort($cards);
//holding array for sequential cards
$seq = array();
$_cards = array();
foreach( $cards as $key => $card){
$c_value = $card['value_num'];
// < start --- not needed for flush - need these without the nested array
if( !isset( $_cards[$c_value] ) ){
$_cards[$c_value] = array($c_value);
}else{
$_cards[$c_value][] = $c_value;
}
//end --- not needed for flush >
//expect last item from seq - 1 ( initially -1 )
$expected = end($seq) - 1;
if(count($seq) < 5 && !in_array( $c_value, $seq )){
if( $c_value == $expected){
//match add to sequence
$seq[] = $c_value;
}else{
//fail, reset sequence to start at this card
$seq = array($c_value);
}
}
}
print_r( $seq );
echo '<br>';
print_r( max($seq));
echo '<br>';
print_r($_cards);
echo '<br>------------<br>';
}
输出Array
(
[0] => 10
[1] => 9
[2] => 8
[3] => 7
[4] => 6
)
10
Array
(
[14] => Array
(
[0] => 14
)
[10] => Array
(
[0] => 10
)
[9] => Array
(
[0] => 9
)
[8] => Array
(
[0] => 8
[1] => 8
)
[7] => Array
(
[0] => 7
)
[6] => Array
(
[0] => 6
)
)
------------
Array
(
[0] => 10
[1] => 9
[2] => 8
[3] => 7
[4] => 6
)
10
Array
(
[14] => Array
(
[0] => 14
)
[13] => Array
(
[0] => 13
)
[10] => Array
(
[0] => 10
)
[9] => Array
(
[0] => 9
)
[8] => Array
(
[0] => 8
)
[7] => Array
(
[0] => 7
)
[6] => Array
(
[0] => 6
)
)
------------
Array
(
[0] => 9
[1] => 8
[2] => 7
[3] => 6
[4] => 5
)
9
Array
(
[14] => Array
(
[0] => 14
)
[12] => Array
(
[0] => 12
)
[9] => Array
(
[0] => 9
)
[8] => Array
(
[0] => 8
)
[7] => Array
(
[0] => 7
)
[6] => Array
(
[0] => 6
)
[5] => Array
(
[0] => 5
)
)
------------
Array
(
[0] => 9
[1] => 8
[2] => 7
[3] => 6
[4] => 5
)
9
Array
(
[14] => Array
(
[0] => 14
)
[13] => Array
(
[0] => 13
)
[9] => Array
(
[0] => 9
)
[8] => Array
(
[0] => 8
)
[7] => Array
(
[0] => 7
)
[6] => Array
(
[0] => 6
)
[5] => Array
(
[0] => 5
)
)
作为旁注,比较前后的卡数可以预测您是否需要检查配对等。,因为没有配对,没有项目被删除。
$cards = array_unique( $cards );