首页 > 代码库 > 扑克模拟,牌型判断java版
扑克模拟,牌型判断java版
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() { return value; } public void setValue(Integer value) { this.value =http://www.mamicode.com/ 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 = http://www.mamicode.com/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); }}
扑克模拟,牌型判断java版
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。