您好,我需要帮助来弄清楚如何从扑克牌中找到两对。 我相信我需要计算不同牌的数量,然后根据以下逻辑告诉我它是否是两对:两对是一手扑克牌,包含两张相同等级的牌,两张不同等级的牌和一张不同等级的牌。第三等级;我只是不知道该怎么做。
感谢任何帮助。
这是我的扑克牌 table
+----------+------+------+------+-----------+-----------+
| 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 | |
+----------+--------+----+-----+----+----+----+----------+
最佳答案
我会通过 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 的牌。 count(*) = 2 的组意味着两张不同的牌,这将为您提供最后一手牌。
但正如您从第二张中看到的,您还可以立即识别出 4 同种、葫芦、3 同种、2 对和单高牌中更好的一手牌。
然后,您可以将牌表简化为数字/面,用于确定一对 J/3 比 10 和 9 的手牌更高,因为您不关心牌的花色,只关心它的面值,当与其他手相比。
关于mysql - 在mysql中从扑克牌中找到两对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42542692/