Card類java
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 = 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; } }
Poke類sql
package com.company; import java.sql.Connection; import java.util.*; /** * Created by ttc on 2017/6/30. */ 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];// public void makeCards() { //生成52張撲克,印刷撲克 int index = 0; for(int i = 0; i < 4; i++) { for (int j = 0; j < 13; j++) { cards[index] = new Card(); cards[index].setValue(values[j]); cards[index].setColor(colors[i]); index++; } } return; } public void outputCards() { int index2 = 0; for(Card card : cards) { if(index2 % 13 == 0) System.out.println(); System.out.print(card.toString()+" "); index2++; } } ///5--25 //5+(0--20) //Random r = new Random(); //int n = 5 + r.nextInt(20);//生成一個0,到n之間的隨機數,不包括n,包括0 public void shuffle() { Random random = new Random(); for(int n = 0; n < 52; n++) { int index = random.nextInt(n+1);//n以前的某張牌 //交換cards[n]<---->cards[index]; Card cardTemp = cards[n]; cards[n] = cards[index]; cards[index] = cardTemp; } } //一手牌 public Card[] getOneHands() { Card[] cardHands = new Card[5]; // cardHands[0] = cards[0]; // cardHands[1] = cards[13]; // cardHands[2] = cards[26]; // cardHands[3] = cards[39]; // cardHands[4] = cards[1]; for(int i = 0; i < 5; i++) { cardHands[i] = cards[i]; } return cardHands; } public void judgeHandType(Card[] hands) { boolean bIsSameColor = false; boolean bIsShunzi = false; //先判斷這手牌是否是同花 Set<String> colorSets = new HashSet<String>(); for(int i = 0; i < hands.length; i++) { colorSets.add(hands[i].getColor()); } if(colorSets.size()==1) { bIsSameColor=true; //System.out.println("同花"); } Set<Integer> valueSets = new HashSet<Integer>(); List<Integer> valueLists = new ArrayList<Integer>(); for(int i = 0; i < hands.length; i++) { valueSets.add(hands[i].getValue()); valueLists.add(hands[i].getValue()); } Collections.sort(valueLists);//排序 int diff = valueLists.get(4) - valueLists.get(0); if(diff == 4 && valueSets.size() == 5) { 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(valueSets.size() == 5)//這5張牌不是順子,而且值都不一樣 { System.out.println("雜牌"); } else if(valueSets.size() == 4) { System.out.println("一對"); } else { //map的key保存的是牌的值,map的值保存的是一樣值的牌的列表 Map<Integer,List<Card>> map = new HashMap<Integer,List<Card>>(); //將一手牌的數據,從數組結構,轉變成map結構 for(int i = 0; i < hands.length; i++) { Card card = hands[i]; //看card這張牌的值是否在map的key中存在 if(map.containsKey(card.getValue()))//若是存在 { List<Card> lst = map.get(card.getValue()); lst.add(card); } else//不存在 { List<Card> lst = new ArrayList<Card>(); lst.add(card); map.put(card.getValue(),lst); } } if(map.size() == 2)//4帶1,3帶2 { boolean bIsFourWithOne = false; for(Map.Entry<Integer,List<Card>> entry : map.entrySet()) { //entry的值是一個List if(entry.getValue().size() == 4) { bIsFourWithOne = true; break; } } if(bIsFourWithOne == true) { System.out.println("四帶一"); } else { System.out.println("三帶二"); } } else if(map.size() == 3)//221,311 { boolean bIsThreeOneOne = false; for(Map.Entry<Integer,List<Card>> entry : map.entrySet()) { //entry的值是一個List if(entry.getValue().size() == 3) { bIsThreeOneOne = true; break; } } if(bIsThreeOneOne == true) { System.out.println("三條"); } else { System.out.println("兩對"); } } } return; } }
Main類數組
package com.company; import java.util.Scanner; public class Main { public static void main(String[] args) { // write your code here Poke poke = new Poke(); poke.makeCards(); poke.outputCards(); poke.shuffle(); System.out.println("\n洗牌之後"); poke.outputCards(); Card[] hands = poke.getOneHands(); System.out.println(); for(int i = 0; i< 5; i++) { System.out.println(hands[i]); } System.out.println("\n牌型是:"); poke.judgeHandType(hands); } }