包:com.youth.game.base
1. Card.java
package com.youth.game.base;
import java.util.Arrays;
import com.youth.game.exception.CardFormatException;
public class Card implements Comparable<Card>{
//牌的索引集,一共有52张牌
private static final byte[] cards ={
0,1,2,3,4,5,6,7,8,9,10,11,12,//花色为:黑桃
13,14,15,16,17,18,19,20,21,22,23,24,25,//红桃
26,27,28,29,30,31,32,33,34,35,36,37,38,//梅花
39,40,41,42,43,44,45,46,47,48,49,50,51//方块
};
private static final char[] values = {'2','3','4','5','6','7','8','9','T','J','K','Q','A'};
private int cardIndex; //牌的索引
private int valueIndex;//牌的值
private CardSuit cardSuit;//牌的花色
//private int count=0;//用于统计 本张牌出现的次数 辅助字段 主要用于排序
public Card(int cardIndex, int valueIndex) throws CardFormatException {
super();
if (valueIndex <0||valueIndex >51 ||cardIndex<0||cardIndex>12) {
throw new CardFormatException("纸牌的数值有错");
}
this.cardIndex = cardIndex;
this.valueIndex = valueIndex;
byte val= cards[cardIndex];
try {
this.cardSuit = CardSuit.int2CardSuit(val);
} catch (Exception e) {
e.printStackTrace();
throw new CardFormatException("纸牌的颜色信息有错");
}
}
public Card(String value) throws CardFormatException {
super();
String suit =value.substring(0, 1);
String val =value.substring(1, value.length());
this.valueIndex = Arrays.binarySearch(values, val.charAt(0));
if (this.valueIndex <0) {
throw new CardFormatException();
}
try {
this.cardSuit = CardSuit.valueOf(suit);
} catch (Exception e) {
e.printStackTrace();
throw new CardFormatException();
}
this.cardIndex = cardSuit.ordinal()*13+valueIndex;
}
public int getCardIndex() {
return cardIndex;
}
public void setCardIndex(int cardIndex) {
this.cardIndex = cardIndex;
}
public int getValueIndex() {
return valueIndex;
}
public void setValueIndex(int valueIndex) {
this.valueIndex = valueIndex;
}
public CardSuit getCardSuit() {
return cardSuit;
}
public void setCardSuit(CardSuit cardSuit) {
this.cardSuit = cardSuit;
}
//public int getCount() {
// return count;
//}
//public void setCount(int count) {
// this.count = count;
//}
@Override
public String toString() {
return cardSuit.name()+(values[valueIndex]);
}
@Override
public int compareTo(Card o) {
// //先比较纸牌出现的次数 次数多的放在前面
// if(this.count!=o.getCount()){
// return this.count -o.getCount();
// }
int val = valueIndex%13-o.getValueIndex()%13;
//如果牌的数值相同,那么就比较牌的花色 花色的大小为 黑 >红 >梅 >方
if (val==0) {
return -(this.cardSuit.compareTo( o.cardSuit));
// return -(this.valueIndex/13-o.getValueIndex()/13);
}
return val;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + cardIndex;
result = prime * result + valueIndex;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Card other = (Card) obj;
if (cardIndex != other.cardIndex)
return false;
if (valueIndex != other.valueIndex)
return false;
return true;
}
}
2. CardSuit.java
package com.youth.game.base;
public enum CardSuit {
/**
* 黑桃(spade S)
*/
S,
/**
* 红桃(heart H)
*/
H,
/**
* 梅花(club C)
*/
C,
/**
* 方块(diamond D)
*/
D;
public static CardSuit int2CardSuit(int val){
switch (val/13) {
case 0:
return CardSuit.S;
case 1:
return CardSuit.H;
case 2:
return CardSuit.C;
case 3:
return CardSuit.D;
default:
return null;
}
}
}
3. CardType.java
package com.youth.game.base;
import java.util.List;
import com.youth.game.exception.CardFormatException;
/**
* 牌型
* @author hadoop
*
*/
public enum CardType {
/**
* 皇家同花顺 Royal straight flush (比如黑桃10,J,Q,K,A)
*/
ROYAL_STRAIGHT_FLUSH,
/**
* 同花顺 Straight flush (比如黑桃3,4,5,6,7)
*/
STRAIGHT_FLUSH,
/**
* 四条,炸弹 Four of a kind (比如四条9和一个其它任何牌)
*/
FOUR_OF_A_KIND,
/**
* 满堂彩 Full house (三条加一对)
*/
FULL_HOUSE,
/**
* 清一色 Flush (比如梅花2,5,6,8,J)
*/
FLUSH,
/**
* 一条龙 Straight (比如4,5,6,7,8不同花色混杂)
*/
STRAIGHT,
/**
* 三条 Three of a Kind (比如三个8和其它任意两张单牌)
*/
THREE_OF_A_KIND,
/**
* 两对 Two Pairs
*/
TWO_PAIRS,
/**
* 一对 One Pair
*/
ONE_PAIR,
/**
* 散牌 High Card
*/
HIGH_CARD;
public static CardType getCardType(List<Card> list) throws CardFormatException{
if (list==null||list.size()!=5) {
throw new CardFormatException("纸牌的数量有错!");
}
return null;
};
}
4. Constant.java
package com.youth.game.base;
public class Constant {
//读取文件的路径
public static String rootPath= Constant.class.getClassLoader().getResource("").getPath()+"/../../config/";
private Constant() {
super();
}
}
5. CountCard.java
package com.youth.game.base;
import java.util.List;
/**
* 用于封装统计纸牌数量的一个辅助类
* @author hadoop
*
*/
public class CountCard implements Comparable<CountCard> {
private List<Card> cards; //数值相同的所有的纸牌的集合
private int count; //相同的纸牌的个数 也就是 cards的长度
private int valueIndex; //纸牌的值的索引 也就表示纸牌的值
public CountCard(List<Card> cards) {
super();
if(cards==null || cards.size()==0){
throw new IllegalArgumentException("构造器的参数list长度必须大于1");
}
this.cards = cards;
this.count = cards.size();
this.valueIndex = cards.get(0).getValueIndex();
}
public List<Card> getCards() {
return cards;
}
public int getCount() {
return count;
}
public int getValueIndex() {
return valueIndex;
}
@Override
public int compareTo(CountCard o) {
//先比较 相同牌出现的次数
if (this.count != o.getCount()) {
return this.count - o.getCount();
}
//再比较牌的大小
return this.getCards().get(0).compareTo(o.getCards().get(0));
}
}
6. FiveCards.java
package com.youth.game.base;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.youth.game.util.CardUtils;
/**
* 五张牌组成的对象,也就表示一手牌 只要用于封装数据
* @author hadoop
*
*/
public class FiveCards implements Comparable<FiveCards> {
private List<Card> cards;
private CardType cardType;
private boolean isflush; //是否为同花
private boolean isStraight;//是否为顺子
public FiveCards(List<Card> cards) {
super();
Collections.sort(cards);
this.cards = cards;
//是否为同花
this.isflush = CardUtils.isFlush(cards);
//是否为顺子
this.isStraight = CardUtils.isStraight(cards);
List<CountCard> list = CardUtils.countCard(cards);
//获取牌出现次数的 集合 例如 牌 J J J J K 次数的集合为 4 1
List<Integer> count = new ArrayList<Integer>();
for (CountCard countCard : list) {
count.add(countCard.getCards().size());
}
int size = list.size();
int maxCount = Collections.max(count);//获取出现最多的次数
//可用通过重复的个数来判断 一手牌的类型 后面的注释的数字为 相同牌出现的次数
if (size==2) {
//此时 牌型可能是 4 1 或者 3 2
if(maxCount==4){
this.cardType = CardType.FOUR_OF_A_KIND;
}else {
this.cardType = CardType.FULL_HOUSE;
}
} else if (size==3) {
//此时的牌型可能是 2 2 1 或者 3 1 1
if(maxCount==3){
this.cardType = CardType.THREE_OF_A_KIND;
}else {
this.cardType = CardType.TWO_PAIRS;
}
}else if (size==4) {
//此时的牌型可能是 2 1 1 1
this.cardType = CardType.ONE_PAIR;
}else if (size==5) {
//此时的牌型可能是 1 1 1 1 1
if(isflush&&isStraight){
//同花顺
//获取最大的牌的值 和最小的牌的值
Collections.sort(cards);
int maxVal = cards.get(cards.size()).getValueIndex();
if(maxVal==12){
this.cardType = CardType.ROYAL_STRAIGHT_FLUSH;
}else{
this.cardType = CardType.STRAIGHT_FLUSH;
}
}else if(isStraight) {
this.cardType = CardType.STRAIGHT;
}else if(isflush) {
this.cardType = CardType.FLUSH;
}else {
this.cardType = CardType.HIGH_CARD;
}
}
}
public List<Card> getCards() {
return cards;
}
public CardType getCardType() {
return cardType;
}
@Override
public int compareTo(FiveCards o) {
if (!this.cardType.equals(o.getCardType())) {
return this.cardType.compareTo(o.getCardType());
}
//为顺子或者同花 就需要比较牌的大小
if(isStraight||isflush||cardType.equals(CardType.HIGH_CARD)){
for (int i = cards.size(); i >=0; i--) {
int result = cards.get(i).compareTo(cards.get(i));
if(result!=0){
return result;
}
}
}else {
//有对子的时候
List<CountCard> list = CardUtils.countCard(this.cards);
List<CountCard> otherList = CardUtils.countCard(o.getCards());
Collections.sort(list);
Collections.sort(otherList);
for (int i = list.size(); i >=0; i--) {
int result = list.get(i).compareTo(otherList.get(i));
if(result!=0){
return result;
}
}
}
return 0;
}
}
|