2017-03-01 16 views
0

こんにちは私は、どのようにしてポーカーハンドから2つのペアを見つけるかを理解する必要があります。 私は、2つのペアが同じランクの2枚のカード、別のランクの2枚のカード、および1枚のカードを含むポーカーハンドであるという論理に基づいて2対であるかどうかを教えてくれる別個のカードの数を数える必要があると信じています3位の私はこれについてどうやって行くのか分かりません。mysqlのポーカーハンドから2つのペアを見つける

助けてください。ここ

は私のポーカーカードテーブルが

+----------+------+------+------+-----------+-----------+ 
| cardName | face | type | suit | faceValue | gameValue | 
+----------+------+------+------+-----------+-----------+ 
| AC  | no | A | C |   1 |  14 | 
| 2C  | no | 2 | C |   2 |   2 | 
| 3C  | no | 3 | C |   3 |   3 | 
| 4C  | no | 4 | C |   4 |   4 | 
| 5C  | no | 5 | C |   5 |   5 | 
+----------+------+------+------+-----------+-----------+ 

とポーカーのカードの手

+----------+--------+----+-----+----+----+----+----------+ 
| playerId | gameId | C1 | C2 | C3 | C4 | C5 | handType | 
+----------+--------+----+-----+----+----+----+----------+ 
| 12789 | 17MET | QH | QS | 3D | 3C | 3H |   | 
| 12789 | 82SAT | 7C | 4S | 4D | 4C | 3H |   | 
| 56347 | 03DEC | 6S | 3S | 3H | 3C | 3D |   | 
| 56347 | 23WSA | KH | 10H | 7H | 3H | AH |   | 
| 56347 | 30DEC | AC | KH | KD | 3D | 3S |   | 
+----------+--------+----+-----+----+----+----+----------+ 

私はそれがはるかにだ、私のコメントで言ったように私は最後の行

+----------+--------+----+-----+----+----+----+----------+ 
| playerId | gameId | C1 | C2 | C3 | C4 | C5 | handType | 
+----------+--------+----+-----+----+----+----+----------+ 
| 56347 | 30DEC | AC | KH | KD | 3D | 3S |   | 
+----------+--------+----+-----+----+----+----+----------+ 
+1

最後の行はとても特別ですか?同じplayerIdの他の行とどう違うのですか? –

+0

それはちょうど2組の – eagerzero

+0

を含んでいますか?cardName = c1? – denny

答えて

1

私は関係なく、スーツの共通カードを取得するために、UNIONを経由して、個々のカードの事前集合体としてこれについて行くだろう。これはあなたに1人/ゲーム

playerid gameid onecard 
12789  17MET Q 
12789  17MET Q 
12789  17MET 3 
12789  17MET 3 
12789  17MET 3 

のために、以下のようなものを与えるだろう...

select PlayerID, GameID, left(c1,1) as OneCard 
    from PlayerHand 
union all 
select PlayerID, GameID, left(c2,1) as OneCard 
    from PlayerHand 
union all 
select PlayerID, GameID, left(c3,1) as OneCard 
    from PlayerHand 
union all 
select PlayerID, GameID, left(c4,1) as OneCard 
    from PlayerHand 
union all 
select PlayerID, GameID, left(c5,1) as OneCard 
    from PlayerHand 

でグループを適用すると、あなたは簡単にカードを見ることができ、簡単な集計を行うことができます

あなたのデータ毎の
select 
     preQuery.playerid, 
     preQuery.gameid, 
     preQuery.onecard, 
     count(*) as CntThisCard 
    from 
     (the entire union query above) preQuery 
    group by 
     preQuery.playerid, 
     preQuery.gameid, 
     preQuery.onecard 
    having 
     count(*) > 1 

、これは次の行を返します...

playerid gameid onecard cntThisCard 
12789  17MET Q  2 
12789  17MET 3  3 This is a full-house 
12789  82SAT 4  3 Three-of-a-kind 
56347  03DEC 3  4 Four-of-a-kind 
56347  23WSA (not returned in data set) 
56347  30DEC K  2 
56347  30DEC 3  2 Two-pair 

だから今、どのように明示的に2ペアを探していることから、私は明示的にwhere句を持っているものは何でも「手」この、あまりにもなるだろうロールアップ...この場合

select 
     QryLvl2.PlayerID, 
     QryLvl2.GameID 
    from 
     (the entire query above returning per-card count) QryLvl2 
    where 
     QryLvl2.CntThisCard = 2 
    group by 
     QryLvl2.PlayerID, 
     QryLvl2.GameID 
    having 
     count(*) = 2 

、抽出します手札に2枚しか入っていないカードしか探していない。カウント(*)= 2のグループは、最終的な手を与える2つの異なるカードを意味します。

2番目からわかるように、4つの種類のフルハウス、3つの種類、2つのペア、1つのハイカードをすぐに特定できます。

カードの表情を気にしないので、ジャック/ 3のペアが10代と9代のペアよりも高いかどうかを判断するために、カードの表を番号/面に単純化することができます他の手と比較すると。

+0

ありがとう、それは素晴らしいです - 非常に感謝します。 – eagerzero

+0

@eagerzero、喜んで...明らかに、より複雑な手のためのより多くの評価は、すべてのスーツがハンド分析の修飾子となるストレート、フラッシュ、ストレートフラッシュ、ロイヤルフラッシュと同じです。 – DRapp

1

を取得する必要がありますそのようなことに適した言語でうまくやっています。 SQLは、仕事のための適切なツールではありません。唯一の学術練習として、これはあなたが必要とする文です:

select * 
    from pokerCard 
    where (left(c1,1) = left(c2,1) and left(c3,1) = left(c4,1)) 
    or (left(c1,1) = left(c2,1) and left(c3,1) = left(c5,1)) 
    or (left(c1,1) = left(c2,1) and left(c4,1) = left(c5,1)) 
    or (left(c1,1) = left(c3,1) and left(c2,1) = left(c4,1)) 
    or (left(c1,1) = left(c3,1) and left(c2,1) = left(c5,1)) 
    or (left(c1,1) = left(c3,1) and left(c4,1) = left(c5,1)) 
    or (left(c1,1) = left(c4,1) and left(c2,1) = left(c3,1)) 
    or (left(c1,1) = left(c4,1) and left(c2,1) = left(c5,1)) 
    or (left(c1,1) = left(c4,1) and left(c3,1) = left(c5,1)) 
    or (left(c1,1) = left(c5,1) and left(c2,1) = left(c3,1)) 
    or (left(c1,1) = left(c5,1) and left(c2,1) = left(c4,1)) 
    or (left(c1,1) = left(c5,1) and left(c3,1) = left(c4,1)) 
    or (left(c2,1) = left(c3,1) and left(c1,1) = left(c4,1)) 
    or (left(c2,1) = left(c3,1) and left(c1,1) = left(c5,1)) 
    or (left(c2,1) = left(c3,1) and left(c4,1) = left(c5,1)) 
    or (left(c2,1) = left(c4,1) and left(c3,1) = left(c5,1)) 
    or (left(c2,1) = left(c5,1) and left(c3,1) = left(c4,1)) 
+0

ええ - それは大丈夫を殺す以上..あなたの時間をありがとうそして、努力 – eagerzero

関連する問題