{"id":21071581,"url":"https://github.com/esrrhs/texas_algorithm","last_synced_at":"2025-07-17T07:34:15.604Z","repository":{"id":87695700,"uuid":"141111276","full_name":"esrrhs/texas_algorithm","owner":"esrrhs","description":"texas algorithm 德州扑克算法","archived":false,"fork":false,"pushed_at":"2019-11-14T06:39:10.000Z","size":127093,"stargazers_count":81,"open_issues_count":0,"forks_count":17,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-16T05:38:04.690Z","etag":null,"topics":["algorithm","texas"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/esrrhs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-07-16T08:42:11.000Z","updated_at":"2025-05-03T14:45:58.000Z","dependencies_parsed_at":null,"dependency_job_id":"029e2c97-4584-49c0-939a-c567d7d6da00","html_url":"https://github.com/esrrhs/texas_algorithm","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/esrrhs/texas_algorithm","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esrrhs%2Ftexas_algorithm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esrrhs%2Ftexas_algorithm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esrrhs%2Ftexas_algorithm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esrrhs%2Ftexas_algorithm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/esrrhs","download_url":"https://codeload.github.com/esrrhs/texas_algorithm/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esrrhs%2Ftexas_algorithm/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265577681,"owners_count":23791203,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithm","texas"],"created_at":"2024-11-19T18:53:40.602Z","updated_at":"2025-07-17T07:34:15.598Z","avatar_url":"https://github.com/esrrhs.png","language":"Java","readme":"# 德州扑克算法\n用于带鬼牌的德州扑克的算法，目前支持两张鬼，包括以下功能\n* 查表算法(内存占用十几M)\n* 评估算法(内存占用300M)\n\n## 使用\n``` xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.github.esrrhs\u003c/groupId\u003e\n    \u003cartifactId\u003etexas_algorithm\u003c/artifactId\u003e\n    \u003cversion\u003e1.0.12\u003c/version\u003e\n\u003c/dependency\u003e\n```\n``` java\n// 获取2张手牌5张公牌的最大的5张牌\nTexasAlgorithmUtil.getMax(\"黑2,黑3\", \"方2,方A,黑7,黑5,鬼\");\n// 获取7张牌的大小，用于比牌\nint win = TexasAlgorithmUtil.getWinPosition(\"方4,方A,鬼,黑A,黑3,黑5,黑6\");\n// 获取2张手牌4张公牌的胜率，用于评估\nfloat p = TexasAlgorithmUtil.getHandProbability(\"方3,鬼\", \"黑2,黑4,黑5,黑K\");\n```\n\n## 测试玩玩\n* 解压texas_algorithm.rar到当前文件夹\n* 运行TestUtil.Main\n\n## 生成表玩玩\n* 解压texas_algorithm.rar到当前文件夹\n* 运行TexasAlgorithmUtil.Main,需要添加vm参数-Xmx8000m\n\n# 查表算法\n查表算法，给定任意7张牌（5张和6张也支持），查表给出5张最大牌的牌面以及大小、胜率、类型。查表方法很简单，下面讲一下生成表的算法。\n\n### 算法实现\n\n#### 穷举C(52, 7)的组合\n52张牌再加2张鬼牌里面选7张，一共有1亿多种组合，对7张牌进行编码变成long类型，得到一个1亿长度的数组。\n* 给定6张和5张，也是同理生成\n\n#### 多线程快速排序\n对这1亿长度的数组进行从小到大排序，排序依据就是7选5后的大小。使用多线程快速排序，在8核的机器上，排完大概需要10小时。\n* 如果把最终的查表算法替换原始的比牌算法，速度可以缩短到2小时。\n\n#### 结果输出\n数组已经排好序，现在按照顺序输出到一个文件，内容有key、大小顺序、max牌的值、max牌的类型、可阅读的牌面信息。最后文件大小差不多12G。\n* 注意到大小其实是阶梯状的，就是有很多牌是一样大，但是先后顺序不同，所以在输出的时候，要再做一下比牌处理。\n\n\n#### 结果去色\n1亿条数据如果直接用，内存会爆，使用去色算法缩减规模。分为有花色和无花色两个文件，最后文件总大小18M。实际加载到内存占用几十M。\n\n* 对于同花的类型，比如同花、同花顺、皇家同花顺，7张牌的分布肯定是比如红红红红红梅黑，就是至少5张牌是同花色的，于是可以转变花色成为方方方方方黑黑，节省key值\n* 对于非同花的类型，花色毫无作用，那么只需要把花色全去掉，变成方方方方方方方即可\n\n#### 查询方法\n给定7张牌，先去同花表里查，如果没有就去非同花表里查，两个都有就谁大选谁。\n\n# 评估算法\n评估算法，给定2张手牌，0-4张公牌，大致估算出这手牌在1v1情况下的胜率。\n\n### 算法实现\n\n#### 胜率计算\n注意到前面已经生成了7张牌的大小顺序了，那么现在给定N张牌(2\u003c=N\u003c=6），只需要去7张牌的集合里遍历，看包含这N张牌的7张牌的胜率，做一下平均值就是平均胜率。顺便还会生出最大胜率最小胜率。5个输出文件最终大小是2G。\n\n#### 结果去色\n这个N张牌的胜率表同样存在重复的，采用类似的方法去掉花色，分为两张表，查询先查询原始表，没有再去查询去掉花色的表。通过这种方法，6个文件可以缩减到300M。实际加载到内存差不多200M。\n\n#### 公牌查询\n把公牌代入上面计算的胜率表中，查询得到公牌的胜率情况，也就是说对方用这个公牌去组成7张牌的平均胜率记为P1，以及最大和最小胜率P1Max和P1Min。\n\n#### 手牌公牌查询\n把我的手牌和公牌加起来，代入上面的胜率表中，查询得到一个平均胜率P2。注意这时候P2是不准确的，因为手牌被重复使用了。这里存在误差。\n\n#### 胜率预估\nP1、P2都已经拿到，根据P1和P2的关系用P1Max和P1Min做下差值即可得出胜率。这里假定分布是均匀的所以也会有误差。\n\n#### 预估误差\n如果采用最原始的方法穷举所有组合，即固定手牌和固定公牌，穷举剩下公牌和对方手牌，并计算胜率，目前2张手牌4张公牌需要20多天才能计算完，并且数据量已经超标。通过和实际胜率比较，误差大部分在0.1以内，比如实际胜率0.5，预估0.6。\n\n## 其他\n\u003ca href=\"https://github.com/esrrhs/majiang_algorithm\"\u003e麻将算法\u003c/a\u003e\n\u003ca href=\"https://github.com/esrrhs/teenpatti_algorithm\"\u003e印度炸金花算法\u003c/a\u003e\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fesrrhs%2Ftexas_algorithm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fesrrhs%2Ftexas_algorithm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fesrrhs%2Ftexas_algorithm/lists"}