Card class
PackageCom.company; Public classCard {PrivateString color; PrivateInteger value; PublicString GetColor () {returncolor; } Public voidsetcolor (String color) { This. color =color; } PublicInteger GetValue () {returnvalue; } Public voidSetValue (Integer value) { This. Value =value; } Publicstring ToString () {string strvalue= ""; Switch(value) { Case1: {strvalue= "A"; Break; } Case11: {strvalue= "J"; Break; } Case12: {strvalue= "Q"; Break; } Case13: {strvalue= "K"; Break; } default: strvalue=value.tostring (); Break; } returncolor+strvalue; }}
View Code
Main class
PackageCom.company;Importjava.util.ArrayList;Importjava.util.Collections;ImportJava.util.HashMap;ImportJava.util.HashSet;Importjava.util.List;ImportJava.util.Map;ImportJava.util.Random;ImportJava.util.Set; Public classMain { Public Static voidJudge (card[] hands) {BooleanBissamecolor =false; BooleanBisshunzi =false; Set<String> setcolors =NewHashset<string>(); for(inti=0; i < hands.length; i++) {Setcolors.add (Hands[i].getcolor ()); } if(Setcolors.size () ==1) {Bissamecolor=true; //System.out.println ("flush");} List<Integer> lstvalues =NewArraylist<integer>(); Set<Integer> setvalues =NewHashset<integer>(); for(inti=0; i < hands.length; i++) {Lstvalues.add (Hands[i].getvalue ()); Setvalues.add (Hands[i].getvalue ()); } //CIS, must meet 3 conditions//1. This hand first order (from big to small) sorthands[5]//2. There is no duplicate of the card value in this hand.//3. If sorthands[0]-sorthands[4]==4Collections.sort (lstvalues); intNdiff = Lstvalues.get (4)-lstvalues.get (0); if(Setvalues.size () ==5&&ndiff==4) {Bisshunzi=true; //System.out.println ("Shun Zi"); } if(bissamecolor&&Bisshunzi) {System.out.println ("The same flower Shun"); } Else if(Bissamecolor) {System.out.println ("The same Flower"); } Else if(Bisshunzi) {System.out.println ("Shun Zi"); } Else if(Setvalues.size () ==5) {System.out.println (Motley); } Map<Integer,List<Card>> map =NewHashmap<integer,list<card>>(); for(inti = 0; i < hands.length; i++) { intNvalue = Hands[i].getvalue ();//Red Peach 9 if(Map.containskey (Nvalue)) {List<Card> lstcards =Map.get (Nvalue); Lstcards.add (Hands[i]); } Else//does not contain{List<Card> lstcards =NewArraylist<card>(); Lstcards.add (Hands[i]); Map.put (Nvalue, lstcards); } } if(Map.size () ==4) {System.out.println (A); } Else if(map.size () = = 2)//4 with 1,3 belt 2 { BooleanBisfourwithone =false; for(Map.entry<integer, list<card>>E:map.entryset ()) { if(E.getvalue (). Size () = = 4) {Bisfourwithone=true; } } if(Bisfourwithone = =true) {System.out.println ("Four with one"); } Else{System.out.println ("Three-belt two"); }//map<string,string> map2;//Map2.add ("Apple", "Apple");//Map2.add ("Orange", "orange");//map2.values (); } Else if(map.size () = = 3)//311,221 { BooleanBisthreeoneone =false; for(Map.entry<integer, list<card>>E:map.entryset ()) { if(E.getvalue (). Size () = = 3) {Bisthreeoneone=true; } } if(Bisthreeoneone = =true) {System.out.println ("311"); } Else{System.out.println ("221"); } }//int i = 7;//Card c = card1;//(3,[block 3])//(5,[Peach 5])//(9,[9, Grass flower 9, Hearts 9])////////(13,[Red Peach 13, block))//(5,[grass Flower 5, spades 5])//(2,[Peach 2])////(4,[spades 4, Grass flower 4, peach 4, Square 4])//(9,[grass Flower 9])// //4 with 1,3 belt 2,311,221,2111//map.size () ==4//2111//if (map.size () ==2)//ABS (Map.value[0].size ()-map.value[1].size ()) ==3// //4 with 1//ABS (Map.value[0].size ()-map.value[1].size ()) ==1// //, 3 with 2//if (map.size () ==3)//// //311,221 } Public Static voidgeneratehands (card[] pokes,card[] hands) { for(inti = 0; i < hands.length; i++) {Hands[i]=Pokes[i]; } } Public Static voidoutputcards (card[] pokes) { for(inti = 0; i < pokes.length; i++) { if(i%13==0) System.out.println (); System.out.print (Pokes[i]. ToString ()+" "); } } Public Static voidShuffle (card[] pokes) {Random R=NewRandom (); for(inti = 0; i < pokes.length; i++) { intn = r.nextint (i+1); Card ctemp; Ctemp=Pokes[i]; Pokes[i]=Pokes[n]; Pokes[n]=ctemp; } } Public Static voidMain (string[] args) {//System.out.println ("HelloWorld");string[] Colors= {"Red peach", "square piece", "Spades", "Grass flower"}; Integer[] Values= {1,2,3,4,5,6,7,8,9,10,11,12,13}; Card[] Pokes=Newcard[52]; Card[] Hands=NewCard[5]; intNIndex = 0; for(inti = 0; i < colors.length; i++) { for(intj = 0; J < Values.length; J + +) {Pokes[nindex]=NewCard (); Pokes[nindex].setcolor (Colors[i]); Pokes[nindex].setvalue (Values[j]); NIndex++; }} outputcards (pokes); Shuffle (pokes); Generatehands (Pokes,hands); Outputcards (hands); Judge (hands); }}
View Code
Poker simulation, card type Judgment Java Edition