Java Exercise - Poker Licenser

Keywords: Java

Java Exercise - Poker Licenser
Statement: Learn from other bloggers. Thank you for sharing. Here I write it myself.
Implementation ideas

  • - Build a poker card
  • - Construct a set of playing cards
  • - Testing

Build a poker card

/**
* @author Winter and winter
* Define a single card class
* Flower pattern, size
*/
public class Card {

private String flower; // Flower pattern
private int daxiao; // Point

/**Construction method
* @param flower
* @param daxiao
*/
public Card(String flower, int daxiao) {
this.flower = flower;
this.daxiao = daxiao;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
* Get the size and size of the card. For special sizes, such as 1-A, convert when output.
*/
public String toString() {
String daxiaoStr = "";
switch(daxiao) {
case 1: daxiaoStr = "A"; break;
case 11: daxiaoStr = "J"; break;
case 12: daxiaoStr = "Q"; break;
case 13: daxiaoStr = "K"; break;
default: daxiaoStr = String.valueOf(daxiao);
}
return flower + daxiaoStr;
}
}

 

Construct a set of playing cards

public class Poker {

private static String[] flowers = {"Spade", "Heart", "Grass flower", "block"};
private static int[] daxiaos = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
private Card[] cards;//There should be 54 cards in the card array. Only 52 cards are removed from the card array.

/**
* constructor
* Instantiate 52 cards in sequence
*/
public Poker() {
cards = new Card[52];
for(int i = 0; i < flowers.length; i++) {
for(int j = 0; j < daxiaos.length; j++) {
cards[i * 13 + j] = new Card(flowers[i], daxiaos[j]);
}
}
}

/**
* Shuffling (random order)
* Random Random Random Array Array of 52 Card Instances
*/
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 Place of licensing
* 
*/
public Card deal(int index) {
return cards[index];
}

}

 

Testing

 

public class Test {

public static void main(String[] args) {
Poker poker = new Poker();//Generate poker cards
poker.shuffle(); // Shuffling (disorder)
Card c1 = poker.deal(0); // Play the first card
//Create four players, 13 empty cards for each player.
Card [] person1=new Card[13];
Card [] person2=new Card[13];
Card [] person3=new Card[13];
Card [] person4=new Card[13];
//Assign the player's empty card.
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); 
}
//Printing
System.out.println("Player 1");
for (Card card : person1) {
System.out.print(card+" ");
}
System.out.println("");
System.out.println("Player 2");
for (Card card : person2) {
System.out.print(card+" ");
}
System.out.println("");
System.out.println("Player 3");
for (Card card : person3) {
System.out.print(card+" ");
}
System.out.println("");
System.out.println("Player 4");
for (Card card : person4) {
System.out.print(card+" ");
}
}
}

Result:

Player 1
Red Peach 6 Squares 4 Squares A Spades 4 Grass Flowers 2 Red Peaches Q Red Peaches J Red Peaches K Squares 3 Spades K Squares 8 Spades 7 Spades 5
Player 2
Caohua A Spade 2 Heart 7 Grass 3 Grass Flower 5 Square J Block 9 Grass Flower 9 Grass Flower K Spade 8 Grass Flower J Spade 10 Red Peach 8
Player 3
Block 7, Heart 4, Heart 2, Grass 6, Spade Q, Spade 9, Spade A, Grass 10, Flower 7, Heart 10, Grass A, Block Q
Player 4
Red Peach 9 Square, 5 Grass Flowers 8 Square, 10 Square K Grass Flowers Q Spades 3 Red Peaches 5 Spades 6 Spades J Red Peaches 3 Square, 6 Grass Flowers 4

Matters needing attention

1. Don't forget to import Card in the Poker class and Poker and Card in the Test class.
2. Think for yourself, do something to really understand.

Posted by Willburt on Thu, 03 Oct 2019 14:08:27 -0700