Share a Texas hold ' em algorithm

Source: Internet
Author: User
Tags return tag shuffle

Texas hold ' em is presumably a lot of people have played, of course, for the novice need to explain that Texas refers to Texas, not Shandong Dezhou.

These months have been doing a Texas hold ' em server, sharing under the calculation of the largest card type, compared to the card logic algorithm and shuffle method, hope to help everyone.

First we define the poker class

public class Poker{private string tag;//The representation of the picture Poker suit (for the Red Peach, the spades, the plum blossom, the square private int num;//) The large public Poker of the Poker Face (string tag, in T num) {this.num = Num;this.tag = tag;} public int Getnum () {return num;} public void setnum (int num) {this.num = num;} public void Settag (String tag) {This.tag = tag;} Public String Gettag () {return tag;} Public String toString () {return "tag:" + This.tag + "num:" + This.num;}}

here, ABCD stands for four different suits, 2-16 for different cards, a with 14 instead, I don't want to explain more!

The first is to shuffle this piece, we run the idea of not repeating the wheel, borrowing collections.shuffle (List) interface, to the 52 cards scrambled order, and then randomly randomly extracted.

First of all, to get each player's maximum card type, that is, from the N card to take out 5 of the largest, we draw from the N Card 5 cards, there are n multiple algorithms, here is an algorithm

private static Map<integer, list<poker>> seventofivegroups (list<poker> Map) {Map<integer, List <Poker>> Group = new Hashmap<integer, list<poker>> (), int num = 0;for (int a = 0; a < 3; a++) {for ( int B = A + 1; b < 4; b++) {for (int c = b + 1, c < 5; C + +) {for (int d = c + 1, D < 6; d++) {for (int e = d + 1; e < 7; e++) {List<poke r> Pukegroup = new arraylist<poker> ();p Ukegroup.add (Map.get (a));p Ukegroup.add (Map.get (b));p Ukegroup.add ( Map.get (c));p Ukegroup.add (Map.get (d));p Ukegroup.add (Map.get (e)); Group.put (num++, Pukegroup);}}}} num = 0;return Group;}

after we have made all possible combinations, we get the largest number one hand from these combinations. In order to get the largest one, we calculate the size of each hand (five cards). First of these five cards to sort, from big to small, the size of the hand is a long shape, the hand level accounted for two (such as high card 10, a pair of 11, 22 is 12, and so on), five cards, each poker value accounted for two, so the size of the hand is a 12-bit integer,

A deck of cards than 1.2.

Hand level

First card

Second card

Third card

Fourth card

Fifth card

Ten

A (a)

U (B)

Ten (C)

9 (D)

7 (C)

Figure 1.2

   The size of this hand is 101412100907.

It should be noted that in the case of a pair, whether it is two or three pairs, should be a special sort, gourd and three pairs of cases, to put three of the same big cards to the front, and then row pairs, so as to ensure that the comparison of the size of the case, the correct comparison of the size. There is a A2345 such as the case, to say a row in the end, because 34567 of the CIS is bigger than the A2345. Of course, these two situations are special cases and need special treatment.

The algorithm for the corresponding program is:

First calculate the hand level, if it is straight or flush, needless to say, the pair of three double gourd start is relatively difficult to compare, here is an algorithm,

int pukelevel = -1;int flag = 0;for (int i = 0; i < list.size (); i++) {for (int j = i + 1; j < List.size (), + j) {if ( List.get (i). Getnum () = = List.get (j). Getnum ()) {flag++;}} Switch (flag) {Case 6:                         four case 4:                         gourd Case 3: Three case 2: Two teams case 1: Pair case 0: High card}              

This.level is the card type, this is calculated, and then calculate the size of this hand!

Long lon = 1000000000L * 10;this.pkvalue = 0l;for (int i = 0; i < this.list.size (); i++) {int interval = 1;for (int j = I J < This.list.size ()-1; J + +) {interval *= 10;interval *= 10;} This.pkvalue + = This.list.get (i). Getnum () * interval;} This.pkvalue + = This.level * LON;

All that is left is to sort all the cases, and this is the case when all the players are compared!

Hope to be of help to everyone.

Share a Texas hold ' em algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.