Java exercise - poker card shoe
statement: Learn from other bloggers, thanks for sharing, where he is writing about.
Realization of ideas
- - Construction of a playing card
- - Construction of a set of playing cards
- - Test
Construction of a playing card
/ ** * @author Hasty * class defines a single poker * have flowers, size * / public class Card { Private String Flower; // pattern Private int daxiao; // Points / * constructor * @ param Flower * @param daxiao * / public Card (Flower String, int daxiao) { the this .flower = Flower; the this .daxiao = daxiao; } / * (non-the Javadoc) * # @see java.lang.Object toString () * Get the size of this pattern cards card, for a particular size, such as 1 - a, output conversion * / public String toString () { String daxiaoStr = "" ; Switch (daxiao) { Case . 1: daxiaoStr = "A"; BREAK ; Case . 11: daxiaoStr = "J"; BREAK ; Case 12 is: daxiaoStr = "Q"; BREAK ; Case 13 is: daxiaoStr = "K"; BREAK ; default : daxiaoStr = String.valueOf (daxiao) ; } return Flower + daxiaoStr; } }
Construction of a set of playing cards
public class Poker { Private static String [] = {Flowers "spades", "hearts", "flower", "block" }; Private static int [] = {daxiaos. 1, 2,. 3,. 4,. 5,. 6 ,. 7,. 8,. 9, 10,. 11, 12 is, 13 is }; Private card [] cards; // card arrays, there should be 54, this removes king size, only 52 / ** * constructor * example sequentially of 52 cards * / public Poker () { Cards = new new Card [52 is ]; for ( int I = 0; I <flowers.length; I ++ ) { for ( int J = 0; J <daxiaos.length; J ++ ) { Cards [I13 is + J *] = new new Card (Flowers [I], daxiaos [J]); } } } / ** * shuffle (random scrambled) * For Example 52 Card scrambled random permutation array * / public void shuffle () { for ( int I = 0, len = cards.length; I <len; I ++ ) { int index = ( int ) (Math.random () * len); Card TEMP = Cards [index]; Cards [index ] = cards [I]; cards [I] = TEMP; } } / ** * licensing * @param index dealer location * * / public Card deal(int index) { return cards[index]; } }
test
public class the Test { public static void main (String [] args) { Poker Poker = new new Poker (); // generates cards poker.shuffle (); // shuffling (scrambled) Card poker.deal C1 = (0 ); // first card // create four players, each player 13 cards empty card [] = PERSON1 new new card [13 ]; card [] PERSON2 = new new card [13 ]; card [] person3 = new new card [13 ]; card [] person4 = new new card [13 ]; // assignment for the players of empty cards for(int i=1;i<=52;i++) { if(i<=13) person1[i-1]=poker.deal(i-1); if(i>13&&i<=26) person2[i-1-13]=poker.deal(i-1); if(i>26&&i<=39) person3[i-1-26]=poker.deal(i-1); if(i>39&&i<=52) person4[i-1-39]=poker.deal(i-1); } //打印 System.out.println("玩家1"); for (Card card : person1) { System.out.print(card+" "); } System.out.println(""); System.out.println("玩家2"); for (Card card : person2) { System.out.print(card+" "); } System.out.println(""); System.out.println("玩家3"); for (Card card : person3) { System.out.print(card+" "); } System.out.println(""); System.out.println("玩家4"); for (Card card : person4) { System.out.print(card+" "); } } }
result:
Player 1
hearts block 6 4 4 block A flower 2 spades hearts Q hearts J hearts 3 spades K K block 8 block 7 of spades 5 spades
player 2
flower hearts A spade 2 3 7 flower flower 5 9 9 flower box flower box J K J spade spade flower 8 8 10 hearts
player 3
block 7 hearts hearts 2 4 6 flower box 2 Q spades spades 9 spades A flower flower 10 7 10 red hearts A peach block Q
player 4
hearts block 9 8 5 flower flower block 10 block K Q 3 of spades hearts spades 5 6 6 spades J hearts flower box 3 4
Precautions
1. Do not forget to import the class Card Poker class, import and Card Poker in Test class inside;
2. To think, a DIY to do to really understand.