2017-03-01 49 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