2017-03-01 26 views
0

您好,我需要幫助來解決如何從撲克手中找到兩對。 我相信我需要計算不同牌的數量,然後根據邏輯判斷它是否爲兩對,其中兩對是包含相同等級的兩張牌,另一等級的兩張牌和一張牌的撲克牌第三等級;我只是不確定如何去做這件事。從一個撲克手中找到一對兩對的mysql

任何幫助表示讚賞。

這裏是我的撲克牌桌

+----------+------+------+------+-----------+-----------+ 
| 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每一個人卡的預聚合獲得普通卡,無論西裝。然後,通過...

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 

應用組這將使你像下面這樣的一個人/遊戲現在

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

,你可以很容易地看到卡,可以做一個簡單的聚集

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 

所以,現在,如何提取任何「手」這也將得到捲起...

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 

在這種情況下,因爲你明確地尋找兩對,我有where子句明確只看他們手中有2張牌。計數(*)= 2表示兩個不同的牌,這會給你最後的牌。

但是從第二張你可以看到,你也可以立即識別出4種更好的手牌,滿屋,3種一對,2對和單張高牌。

然後,你可以簡化卡片表格,以確定一對插孔/ 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