撲克模擬,牌型判斷java版
阿新 • • 發佈:2017-05-28
setvalue () ssa pri isp src [] break -c
Card類
package com.company; public class Card { private String color; private Integer value; public String getColor() { return color; } public void setColor(String color) { this.color = color; } public Integer getValue() {View Codereturn value; } public void setValue(Integer value) { this.value = value; } public String ToString() { String strValue = ""; switch(value) { case 1: { strValue = "A"; break; }case 11: { strValue = "J"; break; } case 12: { strValue = "Q"; break; } case 13: { strValue = "K"; break; }default: strValue = value.toString(); break; } return color+strValue; } }
主類
package com.company; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Random; import java.util.Set; public class Main { public static void Judge(Card[] hands) { boolean bIsSameColor = false; boolean bIsShunzi = false; Set<String> setColors = new HashSet<String>(); for(int i=0; i < hands.length; i++) { setColors.add(hands[i].getColor()); } if(setColors.size()==1) { bIsSameColor=true; //System.out.println("同花"); } List<Integer> lstValues = new ArrayList<Integer>(); Set<Integer> setValues = new HashSet<Integer>(); for(int i=0; i < hands.length; i++) { lstValues.add(hands[i].getValue()); setValues.add(hands[i].getValue()); } //順子,必須滿足3個條件 //1.這手牌先排好序(由大到小)sortHands[5] //2.這手牌裏面牌值不能有重復項 //3.如果sortHands[0]-sortHands[4]==4 Collections.sort(lstValues); int nDiff = lstValues.get(4)-lstValues.get(0); if(setValues.size()==5&&nDiff==4) { bIsShunzi = true; //System.out.println("順子"); } if(bIsSameColor&&bIsShunzi) { System.out.println("同花順"); } else if(bIsSameColor) { System.out.println("同花"); } else if(bIsShunzi) { System.out.println("順子"); } else if(setValues.size()==5) { System.out.println("雜牌"); } Map<Integer,List<Card>> map = new HashMap<Integer,List<Card>>(); for(int i = 0; i < hands.length; i++) { int nValue = hands[i].getValue();//紅桃9 if(map.containsKey(nValue)) { List<Card> lstCards = map.get(nValue); lstCards.add(hands[i]); } else//不包含 { List<Card> lstCards = new ArrayList<Card>(); lstCards.add(hands[i]); map.put(nValue, lstCards); } } if(map.size()==4) { System.out.println("一對"); } else if(map.size() == 2)//4帶1,3帶2 { boolean bIsFourWithOne = false; for(Map.Entry<Integer, List<Card>> e : map.entrySet() ) { if(e.getValue().size() == 4) { bIsFourWithOne = true; } } if(bIsFourWithOne == true) { System.out.println("四帶一"); } else { System.out.println("三帶二"); } // Map<String,String> map2; // map2.add("apple","蘋果"); // map2.add("orange","桔子"); // map2.values(); } else if(map.size() == 3)//311,221 { boolean bIsThreeOneOne = 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,[方塊3]) // (5,[紅桃5]) // (9,[黑桃9,草花9,紅桃9]) // // // // (13,[紅桃13,方塊13]) // (5,[草花5,黑桃5]) // (2,[紅桃2]) // // (4,[黑桃4,草花4,紅桃4,方塊4]) // (9,[草花9]) // //4帶1,3帶2,311,221,2111 // map.size()==4//2111 // if(map.size()==2) // abs(map.value[0].size()-map.value[1].size())==3 // //4帶1 // abs(map.value[0].size()-map.value[1].size())==1 // //,3帶2 // if(map.size()==3) // // //311,221 } public static void generateHands(Card[] pokes,Card[] hands) { for(int i = 0; i < hands.length; i++) { hands[i] = pokes[i]; } } public static void outputCards(Card[] pokes) { for(int i = 0; i < pokes.length; i++) { if(i%13==0) System.out.println(); System.out.print(pokes[i].ToString()+" "); } } public static void shuffle(Card[] pokes) { Random r = new Random(); for(int i = 0; i < pokes.length; i++) { int n = r.nextInt(i+1); Card cTemp; cTemp = pokes[i]; pokes[i] = pokes[n]; pokes[n] = cTemp; } } public static void main(String[] args) { //System.out.println("helloworld"); String[] Colors = {"紅桃","方片","黑桃","草花"}; Integer[] Values = {1,2,3,4,5,6,7,8,9,10,11,12,13}; Card[] pokes = new Card[52]; Card[] hands = new Card[5]; int nIndex = 0; for(int i = 0; i < Colors.length; i++) { for(int j = 0; j < Values.length; j++) { pokes[nIndex] = new Card(); pokes[nIndex].setColor(Colors[i]); pokes[nIndex].setValue(Values[j]); nIndex++; } } outputCards(pokes); shuffle(pokes); generateHands(pokes,hands); outputCards(hands); Judge(hands); } }View Code
撲克模擬,牌型判斷java版