package com.company; public class Main { public static void main(String[] args) { //1.先生成52张扑克 Poke poke = new Poke(); poke.makeCards(); //2.将生成的扑克打印出来 poke.showCards(); //3.洗牌 poke.shuffle(); //4.打印洗好之后的扑克牌 System.out.println(); System.out.println("\n"+"洗牌之后"); poke.showCards(); System.out.println(); //获得一手牌 Card[] handCards = poke.getHandCards(); for(Card c :handCards){ System.out.println(c); } //判断牌型 poke.judgeCardType(handCards); } }
package com.company; /** * 定义一个扑克牌的类,有花色属性,还有牌面值的属性 * Created by ttc on 2017/7/1. */ public class Card { private String color; private Integer value;//考虑到之后牌面值会有j q k 会用到toString方法,所以定义Integer类型的变量 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 = value; } //重写Card类的toString方法,能打印"J","Q","K","A" public String toString(){ String strValue = value.toString(); switch(strValue){ case "1": strValue = "A"; break; case "11": strValue = "J"; break; case "12": strValue = "Q"; break; case "13": strValue = "K"; break; default: break; } return color+strValue; } }
package com.company; import java.util.*; /** * 扑克的功能类 * Created by ttc on 2017/7/1. */ public class Poke { String[]colors = {"红桃","黑桃","方片","草花"}; Integer[]values = {1,2,3,4,5,6,7,8,9,10,11,12,13}; Card [] cards = new Card[52]; //定义生产扑克的方法,双For循环 public void makeCards(){ int index = 0; for(int i = 0;i<colors.length;i++){ for(int j = 0;j<values.length;j++){ cards[index] = new Card(); cards[index].setColor(colors[i]); cards[index].setValue(values[j]); index++; } } return; } //显示扑克的方法 public void showCards(){ int index2 = 0; for(Card c : cards){ if(index2%13==0){ System.out.println(); } System.out.print(c.toString()); index2++; } } //定义洗牌的方法 public void shuffle(){ Random r = new Random(); for(int i = 0 ;i<cards.length;i++){ int index = r.nextInt(i+1); Card temp = cards[i]; cards[i] = cards[index]; cards[index] = temp; } } //定义取一手牌的方法 public Card[] getHandCards(){ Card[] handCards = new Card[5]; for(int i = 0;i<5;i++){ handCards[i] = cards[i]; } return handCards; } //判断牌型的方法 public void judgeCardType(Card [] handCards){ //先判断这手牌是不是同花 boolean bIsSameColor = false; boolean bIsShunZi = false; Set<String> colorSet = new HashSet<>(); for(int i = 0;i<handCards.length;i++){ colorSet.add(handCards[i].getColor()); } if(colorSet.size()==1){ bIsSameColor = true; } //在判断是不是顺子 Set<Integer> valueSet = new HashSet<>();//用于判断这手牌中是否有重复的牌 List<Integer> valueList = new ArrayList<>(); for(int i = 0;i<handCards.length;i++){ valueSet.add(handCards[i].getValue()); valueList.add(handCards[i].getValue()); } Collections.sort(valueList); int diff = valueList.get(4)-valueList.get(0); if(diff == 4 && valueSet.size()==5) { bIsShunZi = true; } if(bIsSameColor&&bIsShunZi){ System.out.println("同花顺"); }else if(bIsSameColor){ System.out.println("同花"); }else if(bIsShunZi){ System.out.println("顺子"); }else if (valueSet.size()==5){ System.out.println("杂牌"); }else if (valueSet.size()==4){ System.out.println("一对"); } else{ //map的key保存的是牌的值,map的值保存的是同样值的牌的列表 Map<Integer,List<Card>> map = new HashMap<>(); for(int i = 0;i<handCards.length;i++){ Card card = handCards[i]; if(!map.containsKey(card.getValue())){ //不包括的情况,需要添加 List<Card> listCard = new ArrayList<>(); listCard.add(card); map.put(card.getValue(),listCard); }else{ //已经存在,就只在LIST中添加 List<Card> listCard = map.get(card.getValue()); listCard.add(card); } } if(map.size()==2){ //有4带1和3带2两种情况 boolean bIsFourWithOne = false; for(Map.Entry<Integer,List<Card>> m :map.entrySet()){ if(m.getValue().size()==4){ bIsFourWithOne = true; break; } } if(bIsFourWithOne==true){ System.out.println("4带1"); }else{ System.out.println("3带2"); } }else if (map.size()==3){ //311,221两种可能 boolean bIsThreeWithOneOne = false; for(Map.Entry<Integer,List<Card>> m : map.entrySet()){ if(m.getValue().size()==3){ bIsThreeWithOneOne = true; break; } } if (bIsThreeWithOneOne==true){ System.out.println("311"); }else{ System.out.println("221"); } } } return; } }