Choosing the Best 7-Card Hand (Poker Texas Hold'em)

I used the game of Texas Hold'em using C #.
I wrote classes like Card, Deck, Player, Table, etc.

For instance:

Player player1 = new Player("player1"); player1.Card1 = new Card(4, Symbol.Clubs, true); player1.Card2 = new Card(5, Symbol.Clubs, true); Card card1 = new Card(4, Symbol.Clubs, true); Card card2 = new Card(7, Symbol.Hearts, true); Card card3 = new Card(2, Symbol.Spades, true); Card card4 = new Card(4, Symbol.Diamonds, true); Card card5 = new Card(4, Symbol.Clubs, true); Card[] tableCards = {card1, card2, card3, card4, card5}; 

I also wrote several methods for evaluating an array of maps, such as IsFlush, IsStraight, IsPair, etc.
My question is: how can I choose the best hand combination if I have 7 cards (2 hands, 5 from the table).
In this code example, this is {4,4,4,4,7}.

+6
source share
5 answers

Do not write your code with 5 cards. Instead, write it as a whole. Thus,

 ContainsStraightFlush ContainsFourOfAKind ContainsFullHouse 

etc .. would eat a collection of cards and return the truth if there is a subset of these cards - a straight flush, four types, etc. respectively.

Then runs back from the highest level to the lowest. If one of these methods returns true, you can easily choose the best hand that satisfies this condition. For example, on

 2h Kh Qh Jh Th 9h 6c 

ContainsStraightFlush will return true, and then you can choose 9h Th Jh Qh Kh as the best hand.

+3
source

Start by sorting the cards, and this will make your search for the best hand much easier, then you just need to compare neighboring elements for multiples of the same number or straights. then you just have a special case to look for a flash

+2
source

The easiest way to do this is to make each 5-card collection possible and capture the manual value. Remember the best hand. There are only 21 combinations for the situation with 7 cards, so it is not good from the point of view of optimality, but it is not scary if you do not use it for research.

 foreach (possible 5 card combination in allCards) bestHand = Max(bestHand, GetValue(possible)); 

Alternatively, you can create an array that has 1 entry for each map, and each index is a pointer to a new array that has all 2 combinations of maps, and each index in this array for all 3 combinations of maps, etc. If you work with all possible abstractions of suit and rank, the total size of the data structure is about 128 MB in memory. There is a reference implementation in C ++ on 2 + 2 forums.

+2
source

Don't do it this way, it is terribly slow and actually quite cumbersome to write (the logic is rather hairy). In addition, for modeling in Monte Carlo, where you need to run hundreds of millions of hands, this approach simply does not hold.

See this link for an overview of the available methods. In fact, using table-based comparison algorithms is much simpler than using loadload if statements.

Most of the routines presented in this article are available in the already integrated C # libraries into which you can plug in your code.

[The first high-performance original idea is there , and uses lookup tables, perfect hashes, and a good prime multiplication trick to evaluate a hand at a glance.]

+1
source

create an array of cards and add the first 5 cards. Then, take the extra card, find the lowest card in your hand and see if the extra card is larger, if so, switch them. Remembering that you need to find pairs, straight and full houses, etc.

-1
source

Source: https://habr.com/ru/post/887223/


All Articles