2012-01-02 3 views
1

私はPHPで単純なポーカースクリプトを作成していますが、5つのカードのプレイヤーハンドを分析しています。私は結果を見つけることで開始するかどうかはわかりませんphp array poker hand result

Array (
    [0] => Array (
     [face] => k 
     [suit] => d 
    ) 
    [1] => Array (
     [face] => 6 
     [suit] => s 
    ) 
    [2] => Array (
     [face] => 6 
     [suit] => h 
    ) 
    [3] => Array (
     [face] => 4 
     [suit] => d 
    ) 
    [4] => Array (
     [face] => 7 
     [suit] => h 
    ) 
) 

は、私のような配列($手)に保存されている手を持っています。たとえば、プレーヤーが4種類のカードを持っているのか、同じカードを4枚持っているのか、どうすれば分かりますか?

または、プレイヤーが連続した顔(3,4,5,6,7)のRUNを獲得した場合は、

(私は配列では非常に良好ではないよ)

+3

は、[PHPのALGOSを参照してください(https://github.com/Zweer/Poker/blob/master/Source/ poker.php)、または[ruby algos](https://github.com/robolson/ruby-poker/blob/master/lib/ruby-poker/poker_hand.rb)を読むことができれば – clyfe

+0

@clyfe urlが変更されます。 – tanaydin

答えて

2

4のユニークなは十分に簡単です。あなたのカードのあなたの配列をループし、あなたが持っている直面しているどのように多くのそれぞれのまで追加:

$have = array(); 

foreach($hand as $card) { 
    $have[$card['face']]++; 
} 

これはあなたに

$have = array(
    'k' => 1, 
    '6' => 2, 
    '4' => 1, 
    '7' => 1 
); 

を与えるだろうあなたは、値のいずれかどうかを確認するために、この新しい配列を検索あなたは4を持っているなら、あなたは4種類あります。この場合、あなたは一人の二人だけの人と一人の人の人たちがいます。

連続走行では、元の配列をスーツで並べ替える必要があります。その後、顔を合わせると、すべてのダイヤモンドがまとめられ、すべての心臓が一緒になります。各スーツ内には、昇順です。あなたの手の配列がすでにソートされていて、 '面'のカードが数値で表されていると仮定すると( 'j' - > 10、 'q '=> 11、K ''=> 12、 ''=> 13):

$last_suit = null; 
$last_face = null; 
$consecutive = 0; 

foreach($hand as $card) { 
    if ($last_suit != $card['suit']) { // got a new suit, reset the counters 
     $consecutive = 0; 
     $last_face = $card['face']; // remember the current card 
     $last_suit = $card['suit']; // remember the new suit 
     continue; // move on to next card 
    } 
    if (($card['face'] - $last_face) == 1)) { 
     // the new card is 1 higher than the previous face, so it's consecutive 
     $consecutive++; 
     $last_face = $card['face']; // remember the new card 
     continue; // move on to next card 
    } 
    if ($consecutive == 5) { 
     break; // got a 5 card flush 
    } 
} 
+0

答えに感謝します。親切な部分の4つについては、新しい配列を検索するときに、どのPHP関数を使用する必要がありますか? – user1022585

+1

'array_search($ have、4)'。これはあなたが4つ持っている顔である一致するキーを返します。 –