Chinaunix首页 | 论坛 | 博客
  • 博客访问: 147377
  • 博文数量: 20
  • 博客积分: 1425
  • 博客等级: 上尉
  • 技术积分: 265
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-27 18:42
文章分类

全部博文(20)

文章存档

2011年(1)

2010年(1)

2009年(2)

2008年(16)

我的朋友

分类: Java

2008-03-27 15:02:03

Card:一张牌

/**
 * Object presenting one card
 * @author reniaL
 */

public class Card {
    private int face;

    private int suit;

    private static final String faces[] = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };

    private static final String suits[] = { "Diamonds", "Clubs", "Hearts", "Spades" };

    public Card(int cardFace, int cardSuit) {
        face = cardFace;
        suit = cardSuit;
    }

    public int getFace() {
        return face;
    }

    public int getSuit() {
        return suit;
    }

    
/**
     * Get the "value" of this card
     */

    public int getValue() {
        return face * 4 + suit;
    }

    public String toString() {
        return faces[face] + " of " + suits[suit];
    }
}

DeckOfCards:一副牌

/**
 * presenting a deck of cards(52)
 * @author reniaL
 */

public class DeckOfCards {
    private Card deck[];

    private int currentCard;

    private static final int NUM = 52;

    private Random rand;

    public DeckOfCards() {
        deck = new Card[NUM];
        currentCard = 0;
        rand = new Random();

        for (int i = 0; i < deck.length; i++) {
            deck[i] = new Card(i / 4, i % 4);
        }
    }

    public void Shuffle() {
        currentCard = 0;

        for (int first = 0; first < deck.length; first++) {
            int second = rand.nextInt(NUM);

            Card temp = deck[first];
            deck[first] = deck[second];
            deck[second] = temp;
        }
    }

    public Card DealCard() {
        if (currentCard < deck.length) {
            return deck[currentCard++];
        } else
            return null;
    }
}

TypeOfHand:一手牌的类型

/**
 * enumeration of type of a hand of cards
 * @author reniaL
 */

public enum TypeOfHand {
    
//     declare constants of enum type
    NOTHING(0, "Nothing"),
    PAIR(1, "A Pair"),
    THREE(2, "Three Of A Kind"),
    DOUBLE_PAIR(3, "Two Pairs"),
    HOUSE(4, "House"),
    FOUR(5, "Four Of A Kind"),
    FLUSH(6, "Flush"),
    STRAIGHT(7, "Straight");

    private final int type;

    private final String typeName;

    TypeOfHand(int theType, String theTypeName) {
        type = theType;
        typeName = theTypeName;
    }

    public int getType() {
        return type;
    }

    public String getTypeName() {
        return typeName;
    }
}

HandOfCards:一手牌

/**
 * A. a pair

 * B. two pairs

 * C. three of a kind

 * D. a full house

 * E. four of a kind

 * F. a flush

 * G. a straight
 */

public class HandOfCards {
    private static final int NUM = 5;
// number of cards

    private Card myCards[];

    private TypeOfHand type;
// type of this hand

    private int value;
// value of this hand

    private ArrayList<Integer> unneeded;
// array of index of the unneeded cards

    
/**
     * Deal from deckOfCards 5 times to make a hand of cards, and sort
     */

    public HandOfCards(DeckOfCards deckOfCards) {
        myCards = new Card[NUM];
        unneeded = new ArrayList<Integer>();
        for (int i = 0; i < NUM; i++) {
            myCards[i] = deckOfCards.DealCard();
        }

        
// sort and analyze the cards on hand
        sortCards();
        analyze();
    }

    
/**
     * Analyze the cards on hand to determine the type and value
     */

    private void analyze() {
        unneeded.clear();
        if (checkPair()) {
// type is PAIR, continue to check if it's double pair, three, four, or house
            if (checkThree()) {
                if (!checkFour()) {
                    checkHouse();
                }
            } else {
                checkDoublePair();
            }
        } else if (!checkStraight()) {
            if (!checkFlush()) {
// NOTHING
                type = TypeOfHand.NOTHING;
                value = myCards[4].getValue();
                for (int i = 0; i < 5; i++) {
                    unneeded.add(i);
                }
            }
        }
    }

    private boolean checkPair() {
        for (int i = 1; i < NUM; i++) {
            if (myCards[i].getFace() == myCards[i - 1].getFace()) {
                type = TypeOfHand.PAIR;
                value = type.getType() * 52 + myCards[i].getValue();
                for (int j = 0; j < NUM; j++) {
                    if ((j != i) && (j != i - 1)) {
                        unneeded.add(j);
                    }
                }
                return true;
            }
        }
        return false;
    }

    private boolean checkDoublePair() {
        int i = 1;
        while ((i < NUM) && (myCards[i].getFace() != myCards[i - 1].getFace())) {
            i++;
        }
        for (int j = i + 2; j < NUM; j++) {
            if (myCards[j].getFace() == myCards[j - 1].getFace()) {
                unneeded.clear();
                type = TypeOfHand.DOUBLE_PAIR;
                value = type.getType() * 52 + myCards[j].getValue();
                if (j == 3) {
// AABBX
                    unneeded.add(4);
                } else {
                    if (i == 1) {
// AAXBB
                        unneeded.add(2);
                    } else {
// XAABB
                        unneeded.add(0);
                    }
                }
                return true;
            }
        }
        return false;
    }

    private boolean checkThree() {
        for (int i = 2; i < NUM; i++) {
            if ((myCards[i].getFace() == myCards[i - 1].getFace())
                    && (myCards[i].getFace() == myCards[i - 2].getFace())) {
                unneeded.clear();
                type = TypeOfHand.THREE;
                value = type.getType() * 52 + myCards[i].getValue();
                for (int j = 0; j < NUM; j++) {
                    if ((j != i) && (j != i - 1) && (j != i - 2)) {
                        unneeded.add(j);
                    }
                }
                return true;
            }
        }
        return false;
    }

    private boolean checkHouse() {
        if ((myCards[0].getFace() == myCards[1].getFace())
                && (myCards[3].getFace() == myCards[4].getFace())
                && ((myCards[2].getFace() == myCards[0].getFace()) || (myCards[2]
                        .getFace() == myCards[4].getFace()))) {
            type = TypeOfHand.HOUSE;
            if (myCards[2].getFace() == myCards[0].getFace()) {
                value = type.getType() * 52 + myCards[2].getValue();
            } else {
                value = type.getType() * 52 + myCards[4].getValue();
            }
            unneeded.clear();
            return true;
        }
        return false;
    }

    private boolean checkFour() {
        for (int i = 3; i < NUM; i++) {
            if ((myCards[i].getFace() == myCards[i - 1].getFace())
                    && (myCards[i].getFace() == myCards[i - 2].getFace())
                    && (myCards[i].getFace() == myCards[i - 3].getFace())) {
                type = TypeOfHand.FOUR;
                value = type.getType() * 52 + myCards[i].getValue();
                unneeded.clear();
                return true;
            }
        }
        return false;
    }

    private boolean checkFlush() {
        int suit = myCards[0].getSuit();
        for (int i = 1; i < NUM; i++) {
            if (myCards[i].getSuit() != suit) {
                return false;
            }
        }
        type = TypeOfHand.FLUSH;
        value = type.getType() * 52 + myCards[4].getValue();
        unneeded.clear();
        return true;
    }

    private boolean checkStraight() {
        for (int i = 1; i < NUM; i++) {
            if ((myCards[i].getFace() - myCards[i - 1].getFace() != 1)) {
                return false;
            }
        }
        type = TypeOfHand.STRAIGHT;
        value = type.getType() * 52 + myCards[4].getValue();
        unneeded.clear();
        return true;
    }

    
/**
     * Sort cards on hand, insertion sort
     */

    private void sortCards() {
        Card tempCard;
        for (int i = 0; i < NUM; i++) {
            for (int j = i; (j > 0)
                    && (myCards[j].getValue() < myCards[j - 1].getValue()); j--) {
                tempCard = myCards[j];
                myCards[j] = myCards[j - 1];
                myCards[j - 1] = tempCard;
            }
        }
    }

    public void reDeal(DeckOfCards deckOfCards) {
        for (int i : unneeded) {
            myCards[i] = deckOfCards.DealCard();
        }

        
// sort and analyze the cards on hand

        sortCards();
        analyze();
    }

    public void display() {
        for (int i = 0; i < NUM; i++) {
            System.out.print(myCards[i] + "\t");
        }
        System.out.println("\nType: " + type.getTypeName());
        System.out.println("Value: " + value);
        if (unneeded.size() == 0) {
            System.out.println("Unneeded: empty");
        } else {
            System.out.println("Unneeded: " + unneeded);
        }
    }

    public TypeOfHand getType() {
        return type;
    }

    public int getValue() {
        return value;
    }

    public ArrayList<Integer> getUnneeded() {
        return unneeded;
    }

    public Card[] getMyCards() {
        return myCards;
    }

    public void setMyCards(Card[] myCards) {
        this.myCards = myCards;

        sortCards();
        analyze();
    }
}

GameTest:测试

public class GameTest {
    public static void main(String[] args) {
        DeckOfCards deckOfCards = new DeckOfCards();
        deckOfCards.Shuffle();
        HandOfCards hand = new HandOfCards(deckOfCards);
        System.out.println("Before redeal:");
        hand.display();

        hand.reDeal(deckOfCards);
        System.out.println("\nAfter redeal:");
        hand.display();
    }
}

阅读(714) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~