`
红桃貳
  • 浏览: 40430 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

java 学习笔记 - 扑克牌模型

 
阅读更多

在oracle 的java文档里看到 扑克牌的类就把它抄了一遍:

 

单张扑克:

package enumtype;

public class Card {

    private final Ranks rank;

    private final Suits suit;

    enum Suits {
        DIAMONDS, CLUBS, HEARTS, SPADES
    }

    enum Ranks {
        ACE, DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING
    }

    public Card(Ranks rank, Suits suit) {

        this.rank = rank;
        this.suit = suit;
    }

    public Suits getSuit() {
        return suit;
    }

    public Ranks getRank() {
        return rank;
    }

    public String toString() {
        return this.suitToString(suit) + " " + this.rankToString(rank);
    }

    public static String rankToString(Ranks rank) {
        switch (rank) {
        case ACE:
            return "Ace";
        case DEUCE:
            return "Deuce";
        case THREE:
            return "Three";
        case FOUR:
            return "Four";
        case FIVE:
            return "Five";
        case SIX:
            return "Six";
        case SEVEN:
            return "Seven";
        case EIGHT:
            return "Eight";
        case NINE:
            return "Nine";
        case TEN:
            return "Ten";
        case JACK:
            return "Jack";
        case QUEEN:
            return "Queen";
        case KING:
            return "King";
        default:
            // Handle an illegal argument. There are generally two
            // ways to handle invalid arguments, throwing an exception
            // (see the section on Handling Exceptions) or return null
            return null;
        }
    }

    public static String suitToString(Suits suit) {
        switch (suit) {
        case DIAMONDS:
            return "Diamonds";
        case CLUBS:
            return "Clubs";
        case HEARTS:
            return "Hearts";
        case SPADES:
            return "Spades";
        default:
            return null;
        }
    }

    public static void main(String[] args) {

        // must run program with -ea flag (java -ea ..) to
        // use assert statements
        assert rankToString(Ranks.ACE) == "Ace";
        assert rankToString(Ranks.DEUCE) == "Deuce";
        assert rankToString(Ranks.THREE) == "Three";
        assert rankToString(Ranks.FOUR) == "Four";
        assert rankToString(Ranks.FIVE) == "Five";
        assert rankToString(Ranks.SIX) == "Six";
        assert rankToString(Ranks.SEVEN) == "Seven";
        assert rankToString(Ranks.EIGHT) == "Eight";
        assert rankToString(Ranks.NINE) == "Nine";
        assert rankToString(Ranks.TEN) == "Ten";
        assert rankToString(Ranks.JACK) == "Jack";
        assert rankToString(Ranks.QUEEN) == "Queen";
        assert rankToString(Ranks.KING) == "King";

        assert suitToString(Suits.DIAMONDS) == "Diamonds";
        assert suitToString(Suits.CLUBS) == "Clubs";
        assert suitToString(Suits.HEARTS) == "Hearts";
        assert suitToString(Suits.SPADES) == "Spades";

    }

}

 

    一副牌:

 

package enumtype;

import enumtype.Card.Ranks;
import enumtype.Card.Suits;

public class Deck {

    public final Card cards[];

    public Deck() {
        cards = new Card[(Suits.values().length * Ranks.values().length)];
        int i = 0;
        for (Suits suit : Suits.values()) {
            for (Ranks rank : Ranks.values()) {
                cards[i] = new Card(rank, suit);
                i++;
            }
        }
    }

    public Card getCard(Suits suit, Ranks rank) {
        return cards[(suit.ordinal() * rank.ordinal())];
    }

    public static void main(String[] args) {
        Deck deck = new Deck();
        System.out.println(deck.getCard(Suits.CLUBS, Ranks.ACE));
    }
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics