A card has two attributes, one is a suit (suit), one is a point (rank), and the following is a simple class
Card
Package Com.syz.card;import Java.io.serializable;public Class card implements serializable{private static final long s Erialversionuid = -4279016186402068401l; private int suit; private int rank; public static final int SPADE = 0; public static final int heart = 1; public static final int CLUB = 2; public static final int DIAMOND = 3; public static final int BLACK = 4; public static final int RED = 5; private static final string[] suits = new string[] {"Spades", "Hearts", "Plum", "squares", "small", "big"}; public static final int ACE = 1000; public static final int 1001; public static final int three = 1002; public static final int four = 1003; public static final int FIVE = 1004; public static final int SIX = 1005; public static final int SEVEN = 1006; public static final int EIGHT = 1007; public static final int NINE = 1008; public static final int TEN = 1009; public static final int JACK = 1010; public static final int QUEEN = 1011; public static final int KING = 1012; public static final int JOKER = 1013; private static final string[] ranks = new string[] {"A", "2", "3", "4", "5", "6", "7", "8", "9", "Ten", "J", "Q "," K "," King "}; public Card (int suit, int rank) {if (Suit >-1 && Suit < 6) {if (Suit < 4) { if (Rank < | | rank > 1012) {throw new IllegalArgumentException ("Color or point parameter Error!) "); }} else {if (rank! = 1013) {throw new IllegalArgumentException ( "Color or point parameter error!" "); }}} else {throw new IllegalArgumentException ("Color parameter Error! "); } this.suit = suit; This.rank = rank; } public int Getsuit () {return suit; } public int GetRank () {return rank; } @Override Public String toString () {return suits[suit] + ranks[rank-1000];}}
After writing, to new card, need two parameters, suit and rank, and suit and rank is related to black can correspond to a2345678910jqk, then the black king, Red King. This is checked in the constructor, and throws a parameter error exception if it is not met.
Here is the test class
Cardclient
Package Com.syz.card;import Java.util.arraylist;import Java.util.list;public class Cardclient {public static void main (string[] args) {test2 (); } private static void Test1 () {card c = new Card (Card.black, Card.joker); System.out.println (c); } private static void Test2 () {int[] suits = new int[] {card.spade, Card.heart, Card.club, Card . DIAMOND}; int[] ranks = new int[] {card.ace, card.two, Card.three, Card.four, Card.five, Card.six, Card.seven, Card. EIGHT, Card.nine, Card.ten, Card.jack, Card.queen, card.king}; list<card> cards = new arraylist<card> (); for (int i = 0, i < suits.length; i++) {for (int j = 0; J < Ranks.length; J + +) {Cards.ad D (New Card (Suits[i], ranks[j])); }} cards.add (New Card (Card.black, Card.joker)); Cards.add (New Card (card.red, Card.joker)); System.out.println (cards); }} Test results:
[Spades A, Spades 2, Spades 3, Spades 4, Spades 5, Spades 6, Spades 7, Spades 8, Spades 9, Spades 10, Spades J, Spades Q, Spades K, Hearts A, Hearts 2, Hearts 3, Hearts 4, Hearts 5, Hearts 6, Hearts 7, Hearts 8, Hearts 9, Hearts 10, Hearts of Hearts, Red Peach q, peach k, plum blossom A, plum 2, plum 3, plum 4, plum 5, plum 6, plum 7, plum 8, plum 9, plum blossom 10, plum blossom j, plum blossom Q, plum blossom K, square A, square 2, Block 3, Square 4, Block 5, Block 6, Block 7, Block 8, Block 9, Block 10, Block J, Square Q, Square K, Xiao Wang, king]
Such a pair of poker was created successfully.
Poker originally has 52, no size king, later added the size of the king. Now some poker will bring a whiteboard card inside is some ads or something, if you add this card, you have to change the card class.
Let's look at the second way to create a deck of cards, but there are a lot of classes here, and the benefit is that you can expand. Look at the code:
1.Suit
Package Com.syz.pattern.bridge;
Public abstract class Suit {
Private face face;
Public Suit (face face) {
This.face = face;
}
public void Show () {
Face.show (this);
}
Protected abstract String getName ();
}
2.Face
Package Com.syz.pattern.bridge;
Public abstract class Face {
Protected abstract String getName ();
public void Show (Suit Suit) {
System.out.println (Suit.getname () + this.getname ());
}
}
Above two abstract classes, referencing each other. is the parent class of the suit and the number of points.
A few subclasses of suit:
Package Com.syz.pattern.bridge;public class Spade extends Suit {public Spade (face face) { super (face); } @Override public String getName () { return "spades"; }}
Package Com.syz.pattern.bridge;public class Heart extends Suit {public Heart (face face) { super (face); } @Override protected String getName () { return "red Peach"; }}
Package Com.syz.pattern.bridge;public class Club extends Suit {public Club (face face) { super face; } @Override protected String getName () { return "plum"; }}
Package Com.syz.pattern.bridge;public class Diamond extends Suit {public Diamond (face face) { super (face); } @Override protected String getName () { return "block"; }}
Package Com.syz.pattern.bridge;public class Black extends Suit {public Black (face face) { super (face); } @Override protected String getName () { return "small"; }}
Package Com.syz.pattern.bridge;public class Red extends Suit {public Red (face face) { super (face); } @Override protected String getName () { return "large"; }}
Several subcategories of face
Package Com.syz.pattern.bridge;public class Ace extends face { @Override protected String getName () { return "A"; }}
Package Com.syz.pattern.bridge;public class extends face { @Override protected String getName () { Return "2"; }}
Package Com.syz.pattern.bridge;public Class Three extends face { @Override protected String getName () { Return "3"; }}
Package Com.syz.pattern.bridge;public Class Four extends face { @Override protected String getName () { Return "4"; }}
Package Com.syz.pattern.bridge;public class Five extends face { @Override protected String getName () { Return "5"; }}
Package Com.syz.pattern.bridge;public class Six extends face { @Override protected String getName () { return "6"; }}
Package Com.syz.pattern.bridge;public class Seven extends face { @Override protected String getName () { return "7"; }}
Package Com.syz.pattern.bridge;public class Eight extends face { @Override protected String getName () { Return "8"; }}
Package Com.syz.pattern.bridge;public class Nine extends face { @Override protected String getName () { return "9"; }}
Package Com.syz.pattern.bridge;public class Ten extends face { @Override protected String getName () { Return "ten"; }}
Package Com.syz.pattern.bridge;public class Jack extends face { @Override protected String getName () { return "J"; }}
Package Com.syz.pattern.bridge;public class Queen extends face { @Override protected String getName () { return "Q"; }}
Package Com.syz.pattern.bridge;public class King extends face { @Override protected String getName () { return "K"; }}
Package Com.syz.pattern.bridge;public class Joker extends face { @Override protected String getName () { Return "King"; }}
Next look at some tests:
Suit C1 = new Spade (new Ace ());
C1.show ();
C1 = new Heart (new Jack ());
C1.show ();
C1 = new Club (new Queen ());
C1.show ();
C1 = new Diamond (new King ());
C1.show ();
C1 = new Spade (new Joker ());
C1.show ();
Test results:
Spades A
Red Peach J
Plum Q
Square K
King of Spades
Color and the combination of points, you can see, there is no rule, "Spades King" such can be spelled out, I will hehe.
So how to combine into a pair of 54 cards, here can be done with reflection, see the code:
Package Com.syz.pattern.bridge;import Java.util.arraylist;import Java.util.list;public class CardClient {@ Suppresswarnings ({"Rawtypes", "Unchecked"}) public static void main (string[] args) throws Exception {//Refle CT string[] suitnames = {"Com.syz.pattern.bridge.Spade", "Com.syz.pattern.bridge.Heart", "COM.SYZ.P Attern.bridge.Club "," Com.syz.pattern.bridge.Diamond "}; String[] Ranknames = {"Com.syz.pattern.bridge.Ace", "Com.syz.pattern.bridge.Two", "Com.syz.pattern.bridge". Three "," Com.syz.pattern.bridge.Four "," com.syz.pattern.bridge.Five "," COM.SYZ.PATTERN.BRIDG E.six "," Com.syz.pattern.bridge.Seven "," Com.syz.pattern.bridge.Eight "," Com.syz.pattern.bridge.Nine ", "Com.syz.pattern.bridge.Ten", "Com.syz.pattern.bridge.Jack", "Com.syz.pattern.bridge.Queen", "Co M.syz.pattern.bridge.king "}; list<suit> cards = new arraylist<> (); for (int i = 0; i < suitnames.length; i++) {Class Suitclazz = Class.forName (Suitnames[i]); for (int j = 0; J < Ranknames.length; J + +) {Class Rankclazz = Class.forName (Ranknames[j]); Cards.add (Suit) suitclazz. GetConstructor (New class[] {face.class}). Newinstance (new object[] {rankclazz.newinstance ()})); }} cards.add (New Black (New Joker ())); Cards.add (New Red (New Joker ())); for (int i = 0; i < cards.size (); i++) {Suit card = Cards.get (i); Card.show (); } }}
Results:
Spades A
Black Peach 2
Black Peach 3
Black Peach 4
Black Peach 5
Black Peach 6
Black Peach 7
Black Peach 8
Black Peach 9
Black Peach 10
Black Peach J
Black Peach Q
Black Peach K
Red Peach A
Red Peach 2
Red Peach 3
Red Peach 4
Red Peach 5
Red Peach 6
Red Peach 7
Red Peach 8
Red Peach 9
Red Peach 10
Red Peach J
Red Peach Q
Red Peach K
Plum Blossom A
Plum 2
Plum 3
Plum 4
Plum 5
Plum 6
Plum 7
Plum 8
Plum 9
Plum 10
Plum Blossom J
Plum Q
Plum Blossom K
Block A
Block 2
Block 3
Block 4
Block 5
Block 6
Block 7
Block 8
Block 9
Block 10
Block J
Block Q
Square K
Xiao Wang
King
How to create a deck of poker with Java