C#中的扑克逻辑(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/poker-logic-in-c-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 5 分钟阅读 - 2260 个词 阅读量 0C#中的扑克逻辑(译文)
原文地址:https://www.codeproject.com/Articles/8199/Poker-logic-in-C
原文作者:Tonetheman
译文由本站 robot-v1.0 翻译
前言
Compute poker probabilites with a poker simulation in C#.
使用C#中的扑克模拟来计算扑克概率.
介绍(Introduction)
该代码段使您可以使用C#和.NET很难计算出扑克概率.在此过程中,您将学到:(This code snippet will let you calculate poker probabilities the hard way, using C# and .NET. Along the way, you will learn:)
Enum
以及它们如何帮助消除混乱.(s and how they can help eliminate clutter.)- 如何使用(How to use)
IComparable
在现实世界中的情况.(in a real world situation.) - 如何得分扑克手.(How to score a poker hand.)
- 如何用数学计算扑克概率.(How to calculate poker probabilties with math.)
兴趣点(Points of Interest)
**枚举:(Enums:)**在代码中,枚举器构成了构成牌的两个主要领域概念的基础:等级和西装.(In the code, enumerators form the basis of the two main domain concepts that make up a card: the rank and the suit.)
enum SUIT { None, Diamonds, Hearts, Clubs, Spades }
enum RANK { None, Ace, Two, Three, Four, Five, Six, Seven, Eight,
Nine, Ten, Jack, Queen, King }
通过使用枚举器,填充标准卡座中52张卡片的初始化代码仅需两个简单的代码(By using enumerators, the initialization code that fills out the 52 cards in a standard deck is nothing more than two simple) foreach
陈述.仅当您在创建卡片时必须允许其不具有默认等级的情况下,该代码才变得复杂.(statements. The code is complicated only by the fact that you must allow for a card not to have a default rank when it is created.)
注意,对静态方法的调用(Notice, the call to the static method) Enum.GetValues
返回一个(returns an) Array
,适合(, which is suitable for a) foreach
声明.(statement.)
foreach(SUIT s in Enum.GetValues(typeof(SUIT)))
foreach(RANK r in Enum.GetValues(typeof(RANK)))
if (r!=RANK.None && s!=SUIT.None)
d[counter++] = new Card(r,s);
此方法的唯一技巧是(The only trick to this method is the) typeof
为获取方法调用的枚举器类型所需的call.(call which is needed to get the type of the enumerator for the method call.)
该代码还使用枚举器来定义您可以得到的不同扑克手.最有趣的用途(The code also uses an enumerator to define the different poker hands that you can get. The most interesting use of the) POKERSCORE
枚举器出现在(enumerator comes in the) Stats
类.(class.)
在里面(In the) Append
方法,一个(method, a) POKERSCORE
被铸成(is cast into an) int
用作保持得分信息的数组的索引.(that is used as an index into an array that keeps scoring information.)
在里面(In the) Report
方法,对静态方法的调用(method, the call to the static method) Enum.GetName
获取与每个字符串关联的字符串值(gets the string value associated with each) POKERSCORE
.(.)
for(int i=0;i<counts.Length;++i)
{
Console.WriteLine("{0,-10}\t{1,10}\t{2,10:p4}",
Enum.GetName(typeof(POKERSCORE),i),
counts[i],
counts[i]/(double)_simCount);
}
这是C#如何让您在程序中使用问题域中的单词和短语的另一个示例.(This is another example of how C# lets you use words and phrases from the problem domain in your program.)
**可比性:(IComparable:)**为了使打牌更容易,我们必须按等级对打牌进行排序.这将迫使配对和其他组出现,这将使我们在得分手时采取捷径.在.NET中,(In order to make scoring a poker hand easier, we must sort the poker hand by rank. This will force pairs and other groups to appear that will let us take shortcuts in scoring the hand. In .NET, the) Array.Sort
方法会为您处理排序,只要(method will handle the sorting for you, as long as the elements in the) Array
实施(implement the) IComparable
接口.在代码中,我们实现(interface. In the code, we implement) CompareTo
并使用(and use the) _rank
变量来确定排序位置.(variable to determine the sort position.)
示例代码在这种安排下出现的唯一问题是,在扑克中,A的得分取决于扑克手.例如,以A开头的直线看起来像这样:A2345;但是以A结尾的直线看起来像这样:TJQKA.在这两种情况下,由于我们使用的是(The only problem that the example code presents with this arrangement is that in poker, an Ace is scored differently depending on the poker hand. For instance, a straight that starts with an Ace looks like this: A2345; but a straight that ends with an Ace looks like this: TJQKA. In both cases, we sort an Ace lower in the array since we are using the) enum
确定排序顺序.因此,当我们得分为直线时,我们必须检查特殊情况.(to determine the sort order. So, when we are scoring a straight, we must check for a special case.)
得分手(Scoring a poker hand):在代码中,扑克手以静态方法得分(: In the code, the poker hand is scored in the static method) score
在班上找到(found in the class) PokerLogic
.通话顺序和分数互斥的事实很重要.(. The order of the calls and the fact that the scores are mutually exclusive is important.)
评分顺序相反.四个也是一对.在扑克领域,更希望获得四分之一,正确的分数也应该如此.(The scoring happens in reverse order. A four of a kind is also a pair. In the domain of poker, a four of a kind is more desirable, and so should be the correct score.)
用数学计分扑克手(Scoring a poker hand with math):您可以在Google上查询有关扑克概率的所有参考类型.他们中的大多数人都没有列出杰克或更好的情况.(: You can look up all types of reference on Google on poker probability. Most of them do not list the Jacks or better case.)
可能的对总数由52个选择5或2,598,960个可能的选择给出.(The total number of possible pairs is given by 52 choose 5 or 2,598,960 possible choices.)
要获得Jack或更好的手的总数,您必须执行以下操作:(To get the total number of possible hands for Jacks or better, you must do the following:)
- 为该对(Ace,Jack,Queen或King)选择4个可能的选择之一.(Choose 1 of the 4 possible choices for the pair (Ace, Jack, Queen, or King).)
- 为此选择两套西装.(Pick two suits for this pair.)
- 从剩余的12个等级中选择3张牌.(Pick 3 cards from the remaining 12 ranks.)
- 为最后三张卡中的每张选择一套.(Choose one suit for each of the last three cards.) 这将为您提供337,920或更多更好的机会.(This will give you 337,920 possible hands with Jacks or better.) 在我们的模拟中,应该看到约有13%(337,920/2,598,960)的手被视为Jack或更好.(In our simulation, we should see around 13% (337,920/2,598,960) of the hands counted as Jacks or better.) 默认情况下,当您运行该应用程序时,它将运行5000手扑克游戏并向屏幕显示统计信息.您也可以使用单个命令行参数来运行它,该参数将是发动的手数.(By default, when you run the application, it will run 5000 poker hands and produce statistics to the screen. You may also run it with a single command line parameter that will be the number of hands dealt.) 您必须运行至少100,000手模拟才能真实地了解百分比.(You must run the simulation for at least 100,000 hands to get a true picture of what the percentages will be.)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C# .NET1.1 Win2K WinXP Windows .NET1.0 Visual-Studio VS.NET2003 Dev 新闻 翻译