java 判断牌型?
Card类
1 package com.jokes; 2 3 public class Card { 4 private String suit; 5 private int num; 6 7 public Card(String suit, String num) { 8 super(); 9 this.suit = suit; 10 11 if(num.equals("J")) { 12 this.num = 11; 13 } else if(num.equals("Q")) { 14 this.num = 12; 15 } else if(num.equals("K")) { 16 this.num = 13; 17 } else if(num.equals("A")) { 18 this.num = 14; 19 } else { 20 this.num = Integer.parseInt(num); 21 } 22 } 23 24 public String getSuit() { 25 return suit; 26 } 27 28 public void setSuit(String suit) { 29 this.suit = suit; 30 } 31 32 public int getNum() { 33 return num; 34 } 35 36 public void setNum(int num) { 37 this.num = num; 38 } 39 40 @Override 41 public String toString() { 42 return "Card [suit=" + suit + ", num=" + num + "]"; 43 } 44 45 46 }
从命令行读入5张牌,然后输出牌型
1 package com.jokes; 2 3 import java.util.ArrayList; 4 import java.util.Collections; 5 import java.util.Comparator; 6 import java.util.List; 7 import java.util.Scanner; 8 9 public class Solution { 10 11 public static void main(String[] args) { 12 int SIZE = 5; 13 14 Scanner sc = new Scanner(System.in); 15 List<Card> cards = new ArrayList<Card>(); 16 17 String input = sc.nextLine(); 18 String[] ss = input.split(" "); 19 20 for(int i = 0; i < SIZE; i++) { 21 String suit = ss[2*i]; 22 String num = ss[2*i+1]; 23 24 Card card = new Card(suit, num); 25 cards.add(card); 26 } 27 28 Collections.sort(cards, new Comparator<Card>(){ 29 public int compare(Card c1, Card c2) { 30 if(c1.getNum() < c2.getNum()) { 31 return 1; 32 } 33 34 if(c1.getNum() == c2.getNum()) { 35 return 0; 36 } 37 38 return -1; 39 } 40 }); 41 42 // 判断牌型 43 if(isSF(cards)) { //牌型1 44 System.out.println("同一花色顺子"); 45 System.out.println(1); 46 } else if(isST(cards)) { 47 System.out.println("四张相同的数字+单张"); 48 System.out.println(2); 49 } else if(isHL(cards)) { 50 System.out.println("三张相同的数字+一对"); 51 System.out.println(3); 52 } else if(isSZ(cards)) { 53 System.out.println("同一花色"); 54 System.out.println(4); 55 } else if(isTHX(cards)) { 56 System.out.println("花色不一样的顺子"); 57 System.out.println(5); 58 } else if(isTH(cards)) { 59 System.out.println("三张相同两张单"); 60 System.out.println(6); 61 } else { 62 System.out.println(7); 63 } 64 65 } 66 67 // 牌型1 同一花色的顺子 68 public static boolean isSF(List<Card> cards) { 69 String suit = cards.get(0).getSuit(); 70 int min = cards.get(0).getNum(); 71 int max = cards.get(0).getNum(); 72 73 74 for(int i = 1; i < cards.size(); i++) { 75 // 有花色异常就退出 76 if(!suit.equals(cards.get(i).getSuit())){ 77 return false; 78 } 79 80 int num = cards.get(i).getNum(); 81 if(num > max) { 82 max = num; 83 } else if(num < min) { 84 min = num; 85 } 86 } 87 88 if((max-min) == 4) { 89 return true; 90 } else { 91 return false; 92 } 93 } 94 95 96 // 牌型2 四条 四张相同的数字+单张 97 public static boolean isST(List<Card> cards) { 98 if(cards.get(0).getNum() == cards.get(3).getNum()) { 99 return true; 100 } else if(cards.get(1).getNum() == cards.get(4).getNum()) { 101 return true; 102 } else { 103 return false; 104 } 105 } 106 107 // 牌型3 葫芦 三张相同的数字+一对 108 public static boolean isHL(List<Card> cards) { 109 if(cards.get(0).getNum() == cards.get(2).getNum() && cards.get(3).getNum() == cards.get(4).getNum()) { 110 return true; 111 } else if(cards.get(0).getNum() == cards.get(1).getNum() && cards.get(2).getNum() == cards.get(4).getNum()) { 112 return true; 113 } 114 115 return false; 116 } 117 118 // 牌型4 同一花色 119 public static boolean isSZ(List<Card> cards) { 120 for(int i = 0; i < cards.size()-1; i++) { 121 if(!cards.get(i).getSuit().equals(cards.get(i+1).getSuit())){ 122 return false; 123 } 124 } 125 return true; 126 } 127 128 //牌型5 花色不一样的顺子 129 public static boolean isTHX(List<Card> cards) { 130 for(int i = 0; i < cards.size()-1; i++) { 131 if(cards.get(i).getNum() != cards.get(i+1).getNum()-1) { 132 return false; 133 } 134 } 135 136 return true; 137 } 138 139 //牌型6 三张相同两张单 140 public static boolean isTH(List<Card> cards) { 141 if(cards.get(0).getNum() == cards.get(2).getNum() && 142 cards.get(3).getNum() != cards.get(4).getNum() && 143 cards.get(3).getNum() != cards.get(0).getNum() && 144 cards.get(4).getNum() != cards.get(0).getNum()) { 145 return true; 146 } else if(cards.get(1).getNum() == cards.get(3).getNum() && 147 cards.get(0).getNum() != cards.get(4).getNum()&& 148 cards.get(0).getNum() != cards.get(1).getNum()&& 149 cards.get(4).getNum() != cards.get(4).getNum()) { 150 return true; 151 } else if(cards.get(2).getNum() == cards.get(4).getNum() && 152 cards.get(0).getNum() != cards.get(1).getNum()&& 153 cards.get(0).getNum() != cards.get(2).getNum()&& 154 cards.get(1).getNum() != cards.get(2).getNum()) { 155 return true; 156 } 157 158 return false; 159 } 160 161 }
运行例子:
积沙成塔