在mysql中从扑克手中找到两对

时间:2017-03-01 21:45:34

标签: mysql mysql-5.7

嗨,我需要帮助,找出如何从扑克手中找到两对。 我相信我需要计算不同卡片的数量然后告诉我它是否是基于逻辑的两对是两对是包含两张相同等级的牌的扑克牌,两张另一张牌和一张牌第三等级;我只是不确定如何解决这个问题。

任何帮助表示感谢。

这是我的扑克牌表

+----------+------+------+------+-----------+-----------+
| 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 |          |
+----------+--------+----+-----+----+----+----+----------+

2 个答案:

答案 0 :(得分: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))

答案 1 :(得分: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对和单张高牌。

然后你可以将牌桌简化为数字/面,以确定一对Jacks / 3是比10和9更高的牌,因为你不关心牌的套装,只是它的面值当与其他人相比。