Note: The following algorithms describe the case that only one sub-card (excluding the king of the size) is allowed.
1. Description of the zookeeper rule (everyone understands it. Here is a brief description ):
1) Each player has three cards;
2) The card size is 2, 3, 4, 5, 6, 7, 8, 9, and 10 (represented by T), J, Q, K, and A, and the size increases sequentially;
3) the color of the card is black peach (represented by H), red heart (represented by X), plum blossom (represented by M), square (represented by F), and the size decreases sequentially;
4) The cards are leopard (three cards have the same number size), flush,Same flower(If this is not implemented, you can add the player you are interested in, or you are welcome to communicate with me), shunzi, pair, and cards. The size decreases in turn;
5) players compare the card type first, for example, according to the rules in 4). For example, when all the players are leopards, the card size of the leopard is compared (for example, H9X9M9> F8X8M8; when the types are the same, compare them by the nominal value in 2), for example, H9X9M2> F8X8HA, M9H9M2
2. algorithm features:
1) implemented in an object-oriented way. The following types are constructed: The objects (enumeration) of the card value, the card color objects (enumeration), and the players (enumeration, such as leopard and flush) an object corresponding to a playing card (a card has a card title attribute and a color attribute) and a Player Object (the player has three playing cards and the card type attribute );
2) the comparison function is implemented mainly through the compareTo interface of Java Comparable interface, which makes it easy to sort the cards in the player's hands (by calling the Collections. sort method). Colleagues have avoided many if else comparisons;
3. Code above:
1) card nominal value Enumeration
/*** File name: PorkActor. java * description:
<描述>
* Modification time *
*/Package com. dobuy. zhajinhua ;/**** Card number of each playing card **
*/Public enum PorkActor {TWO ('2'), THREE ('3'), FOUR ('4'), FIVE ('5 '), SIX ('6'), SEVEN ('7'), EIGHT ('8'), NIME ('9'), TEN ('T '), J ('J'), Q ('q'), K ('k'), A ('A'); private char num; private PorkActor (char num) {this. num = num;}/*** get num ** @ return returns num */private char getNum () {return num ;}/**** Find the card enumeration object corresponding to the card number ** @ param num * @ return *
*/Public static PorkActor getPorkActor (char num) {for (PorkActor porkActor: PorkActor. values () {if (porkActor. getNum () = num) {return porkActor ;}} return null ;}}
2) card color Enumeration
/*** File name: PorkColor. java * description:
<描述>
* Modification time *
*/Package com. dobuy. zhajinhua ;/**** Playing card colors **
*/Public enum PorkColor {F ('F'), M ('M'), X ('x'), H ('H '); /*** card color */private char color; private PorkColor (char color) {this. color = color ;}/**** Find the color enumeration object of playing cards based on the color character ** @ param color * @ return *
*/Public static PorkColor getPorkColor (char color) {for (PorkColor porkColor: PorkColor. values () {if (porkColor. color = color) {return porkColor ;}} return null ;}}
3) playing cards
/*** File name: Pork. java * description:
<描述>
* Modification time *
*/Package com. dobuy. zhajinhua ;/**** A playing card object * 1. implement the Comparable interface and use the compareTo method to compare the size * 2. comparison rules: * 1) Check the card number first, and the number is big. * 2) when the card number is the same, the color is big ;**
*/Public class Pork implements Comparable
{/*** Poker Card Number */private PorkActor porkActor;/*** poker card color */private PorkColor porkColor;/*** string with a length of 2, numbers and colors of Cards received: 0th digits and 1st digits
<默认构造函数>
*/Public Pork (String porkAttr) {char porkActor = porkAttr. charAt (1); char porkColor = porkAttr. charAt (0); setPorkActor (PorkActor. getPorkActor (porkActor); setPorkColor (PorkColor. getPorkColor (porkColor);}/*** get porkActor ** @ return returns porkActor */public PorkActor getPorkActor () {return porkActor ;} /*** set porkActor ** @ param to assign a value to porkActor */public void setPorkActor (PorkActor porkActor) {This. porkActor = porkActor;}/*** get porkColor ** @ return returns porkColor */public PorkColor getPorkColor () {return porkColor ;} /*** set porkColor ** @ param to assign a value to porkColor */public void setPorkColor (PorkColor porkColor) {this. porkColor = porkColor;}/*** overload method ** @ return */@ Override public int hashCode () {final int prime = 31; int result = 1; result = prime * result + (porkActor = Null )? 0: porkActor. hashCode (); result = prime * result + (porkColor = null )? 0: porkColor. hashCode (); return result;}/*** overload method ** @ param obj * @ return */@ Override public boolean equals (Object obj) {if (this = obj) return true; if (obj = null) return false; if (getClass ()! = Obj. getClass () return false; Pork other = (Pork) obj; if (porkActor! = Other. porkActor) return false; if (porkColor! = Other. porkColor) return false; return true;}/*** overload method ** @ param o * @ return */@ Override public int compareTo (Pork o) {// first compare the card surface size int compare = getPorkActor (). compareTo (o. getPorkActor (); // if (compare = 0) when the card is at the same time {// return getPorkColor (). compareTo (o. getPorkColor ();} return compare;}/*** overload method ** @ return */@ Override public String toString () {return "Pork [porkActor =" + porkActor + ", porkColor =" + porkColor + "]" ;}}
4) players
/*** File name: PorkPlayer. java * description:
<描述>
* Modification time *
*/Package com. dobuy. zhajinhua; import java. util. ArrayList; import java. util. Collections; import java. util. List ;/**** Poker players **
*/Public class PorkPlayer implements Comparable
{Private String allPorks;/*** players have three cards */private List
Porks;/*** three types of playing cards: leopard and flush */private PorkType porkType;/*** each player has three playing cards by default.
<默认构造函数>
*/Public PorkPlayer (String porksStr) {this. allPorks = porksStr; init (porksStr);}/*** get allPorks ** @ return returns allPorks */public String getAllPorks () {return allPorks ;}/***
* Calculate the player's card type based on the three cards that are sent ** @ param porksStr *
*/Private void init (String porksStr) {porks = new ArrayList
(3); int index = 0; int size = porksStr. length (); Pork pork = null; while (index <size) {pork = new Pork (porksStr. substring (index, index + 2); porks. add (pork); index + = 2;} // sort Collections from small to large. sort (porks); // determine the type of the three cards if (isBaozi () {porkType = PorkType. BAOZI;} else if (isTonghuashun () {porkType = PorkType. TONGHUASHUN;} else if (isShunzi () {porkType = PorkType. SHUNZI;} else if (isDuizi () {porkType = PorkType. DUIZI;} else {porkType = PorkType. SANPAI ;}}/***
* Determine whether it is a leopard (the leopard must have the same size of three cards) * @ return *
*/Private boolean isBaozi () {Pork pork = porks. get (0); for (int I = 1, size = porks. size (); I <size; I ++) {if (pork. getPorkActor ()! = Porks. get (I). getPorkActor () {return false ;}} return true ;}/***
* Determine if it is shun zi * @ return *
*/Private boolean isShunzi () {for (int I = 1, size = porks. size (); I <size; I ++) {if (porks. get (I-1 ). getPorkActor (). compareTo (porks. get (I ). getPorkActor ())! =-1) {return false;} return true ;}/***
* Determine whether it is flush ** @ return *
*/Private boolean isTonghuashun () {if (! IsShunzi () {return false;} Pork pork = porks. get (0); for (int I = 1, size = porks. size (); I <size; I ++) {if (pork. getPorkColor ()! = Porks. get (I). getPorkColor () {return false ;}} return true ;}/***
* Whether it is a child ** @ return *
*/Private boolean isDuizi () {for (int I = 1, size = porks. size (); I <size; I ++) {if (porks. get (I-1 ). getPorkActor (). compareTo (porks. get (I ). getPorkActor () = 0) {return true ;}} return false ;}/***
* Obtain the playing cards corresponding to the pair in the poker player's hand (regardless of the color) ** @ return *
*/Private Pork getDuiziPork () {for (int I = 1, size = porks. size (); I <size; I ++) {if (porks. get (I-1 ). getPorkActor (). compareTo (porks. get (I ). getPorkActor () = 0) {return porks. get (I) ;}} return null ;}/***
* Obtain the non-pair card in the player's hands * @ return *
*/Private Pork getNoDuiziPork () {// the player has only three cards and is a pair. The cards are sorted. If the first two are equal, the last one is not paired, otherwise, the last two are paired with 0th different if (porks. get (0 ). compareTo (porks. get (1) = 0) {return porks. get (2);} else {return porks. get (0) ;}/ *** get porkType *** @ return returns porkType */public PorkType getPorkType () {return porkType ;} /*** overload method ** @ return */@ Override public int hashCode () {final int prime = 31; int result = 1; result = pr Ime * result + (porkType = null )? 0: porkType. hashCode (); result = prime * result + (porks = null )? 0: porks. hashCode (); return result;}/*** overload method ** @ param obj * @ return */@ Override public boolean equals (Object obj) {if (this = obj) return true; if (obj = null) return false; if (getClass ()! = Obj. getClass () return false; PorkPlayer other = (PorkPlayer) obj; if (porkType! = Other. porkType) return false; if (porks = null) {if (other. porks! = Null) return false;} else if (! Porks. equals (other. porks) return false; return true;}/*** overload method ** @ return */@ Override public String toString () {return "PorkPlayer [porks =" + porks + ", porkType =" + porkType + "]";} /*** overload method ** @ param o * @ return */@ Override public int compareTo (PorkPlayer o) {int compare = getPorkType (). compareTo (o. getPorkType (); // TODO if (compare = 0) {switch (getPorkType () {/*** leopard, flush, straight forward Compare the top brands (the top brands compare the sizes first and then the colors) */case BAOZI: case TONGHUASHUN: case SHUNZI: {return porks. get (2 ). compareTo (o. porks. get (2);} case DUIZI: {/*** child comparison */Pork duizi1 = getDuiziPork (); Pork duizi2 = o. getDuiziPork (); // compare the size of the child first. compare the size of the child with the same size. if (duizi1.getPorkActor () = duizi2.getPorkActor () {compare = getNoDuiziPork (). getPorkActor (). compareTo (o. getNoDuiziPork (). getPorkActor (); // compare the largest suit I F (compare = 0) {return duizi1.getPorkColor (). compareTo (duizi2.getPorkColor ();} return compare;} else {// compare the size of the child to return duizi1.getPorkActor (). compareTo (duizi2.getPorkActor () ;}} case SANPAI: {// The hash card starts from the maximum number and only compares the card nominal value. If it is the same, it starts from the second largest value, for (int size = porks. size (), I = size-1; I> = 0; I --) {compare = porks. get (I ). getPorkActor (). compareTo (o. porks. get (I ). getPorkActo R (); if (compare! = 0) {return compare ;}/// indicates that all three cards have the same face value, and return porks are the most popular ones. get (2 ). getPorkColor (). compareTo (o. porks. get (2 ). getPorkColor () ;}}return compare ;}}
5) single poker Unit Testing
/*** File name: PorkTest. java * description:
<描述>
* Modification time *
*/Package com. dobuy. zhajinhua; import static org. junit. Assert. assertTrue; import org. junit. Test ;/****
<一句话功能简述>
**
*/Public class PorkTest {/**** Case 1: Compare the card size when the card size is different *
* // @ Test public void test1 () {Pork pork1 = new Pork ("H5"); Pork pork2 = new Pork ("X6"); assertTrue (pork1.compareTo (pork2) <0 );}/**** Case 1: Compare the card colors when the card faces are of the same size *
* // @ Test public void test2 () {Pork pork1 = new Pork ("H5"); Pork pork2 = new Pork ("X5"); assertTrue (pork1.compareTo (pork2)> 0 );}}
6) player Unit Testing
/*** File name: PorkPlayerTest. java * description:
<描述>
* Modification time *
*/Package com. dobuy. zhajinhua; import static org. junit. Assert. assertTrue; import org. junit. Test ;/****
<一句话功能简述>
**
*/Public class PorkPlayerTest {@ Test public void test1 () {PorkPlayer player1 = new PorkPlayer ("H2F2X2"); assertTrue (player1.getPorkType () = PorkType. BAOZI); PorkPlayer player2 = new PorkPlayer ("H2H3H4"); assertTrue (player2.getPorkType () = PorkType. TONGHUASHUN); PorkPlayer player3 = new PorkPlayer ("H2F3H4"); assertTrue (player3.getPorkType () = PorkType. SHUNZI); PorkPlayer player4 = new PorkPlayer ("H3F3H4"); assertTrue (player4.getPorkType () = PorkType. DUIZI); PorkPlayer player5 = new PorkPlayer ("H3F6H4"); assertTrue (player5.getPorkType () = PorkType. SANPAI) ;}@ Test public void test2 () {/*** case 1: Compare the sizes of leopards */PorkPlayer player11 = new PorkPlayer ("H2F2X2 "); porkPlayer player12 = new PorkPlayer ("HAFAXA"); assertTrue (player11.compareTo (player12) <0);/*** case 2: leopard is greater than flush */PorkPlayer player21 = new PorkPlayer ("H2F2X2"); PorkPlayer player22 = new PorkPlayer ("H3H5H4"); assertTrue (player21.compareTo (player22)> 0 ); /*** case 3: Compare the data size when all data is flat. */PorkPlayer player31 = new PorkPlayer ("H3H5H4"); PorkPlayer player32 = new PorkPlayer ("F5F6F7 "); assertTrue (player31.compareTo (player32) <0);/*** case 4: flush is greater than shunzi */PorkPlayer player41 = new PorkPlayer ("H3H5H4 "); porkPlayer player42 = new PorkPlayer ("F5H6F7"); assertTrue (player41.compareTo (player42)> 0);/*** case 5, compare the largest brands (first, compare the largest card faces, and compare the suit) */PorkPlayer player51 = new PorkPlayer ("H3X5H4 "); porkPlayer player52 = new PorkPlayer ("F5H6F7"); assertTrue (player51.compareTo (player52) <0);/*** case 6: shunzi is greater than sub-pair */PorkPlayer player61 = new PorkPlayer ("H3X5H4"); PorkPlayer player62 = new PorkPlayer ("f5366f7"); assertTrue (player61.compareTo (player62)> 0 ); /*** case 7.1: Compare the child size when all objects are correct */PorkPlayer player71 = new PorkPlayer ("H3F5H5"); PorkPlayer player72 = new PorkPlayer ("XAH6FA "); assertTrue (player71.compareTo (player72) <0);/*** case 7.2: The child size is the same for all pairs, compare the size of the scattered cards */PorkPlayer player73 = new PorkPlayer ("HAF5MA"); PorkPlayer player74 = new PorkPlayer ("XAH6FA"); assertTrue (player73.compareTo (player74) <0 ); /*** case 7.3: Compare the size of all pairs (the size of the pair is the same and the size of the scattered cards is compared) */PorkPlayer player75 = new PorkPlayer ("HAF5MA "); porkPlayer player76 = new PorkPlayer ("XAH6FA"); assertTrue (player75.compareTo (player76) <0);/*** case 7.4: when both are pairs, the three cards have the same face values, compare the best suit of the pair */PorkPlayer player77 = new PorkPlayer ("HAX5MA"); PorkPlayer player78 = new PorkPlayer ("XAM5FA"); assertTrue (player77.compareTo (player78)> 0 ); /*** case 8: Sub-board bigger than hash */PorkPlayer player81 = new PorkPlayer ("H3F5X3"); PorkPlayer player82 = new PorkPlayer ("FQH9F7 "); assertTrue (player81.compareTo (player82)> 0);/*** case 9.1: when all are cards, compare the maximum card size */PorkPlayer player91 = new PorkPlayer ("H3F5HJ"); PorkPlayer player92 = new PorkPlayer ("X4H6FT"); assertTrue (player91.compareTo (player92)> 0);/*** case 9.2, compare the nominal value of 2nd major brands */PorkPlayer player93 = new PorkPlayer ("H3X5HJ"); PorkPlayer player94 = new PorkPlayer ("X4M2FJ"); assertTrue (player93.compareTo (player94)> 0);/*** case 9.3: When all cards are cards, the maximum and 2nd nominal values are the same, compare the minimum card size */PorkPlayer player95 = new PorkPlayer ("H3X5HJ"); PorkPlayer player96 = new PorkPlayer ("X4M5FJ"); assertTrue (player95.compareTo (player96) <0);/*** case 9.4: When all cards are cards, all the three cards have the same par values, compare the best suit */PorkPlayer player97 = new PorkPlayer ("H3X5HJ"); PorkPlayer player98 = new PorkPlayer ("X3M5FJ"); assertTrue (player97.compareTo (player98)> 0 );}}