Java is an example of a method to generate 52 cards by simulating a poker shuffle

  • 2020-12-16 05:58:27
  • OfStack

An example of Java simulation poker shuffle to generate 52 cards is presented in this paper. To share for your reference, the details are as follows:

Requirements:

Generate 52 cards, simulate card shuffle, and output.

Implementation code:


package com.NCU.ZHANGhuirong;
import java.util.ArrayList;
import java.util.Collections;
public class Card {
  public String poker(int num) {
    String str = "";
    String[] face = { "♥", "♠", "♣", "♦" };
    String[] number = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10",
        "J", "Q", "K" };
    str += face[num % 4] + number[num % 13];
    return str;
  }
  public void shuffle(@SuppressWarnings("rawtypes") ArrayList list) {
  }
  @SuppressWarnings("unchecked")
  public static void main(String[] args) {
    @SuppressWarnings("rawtypes")
    ArrayList list = new ArrayList();
    Card card = new Card();
    for (int i = 0; i < 52; i++) {
      list.add(card.poker(i));
    }
    for (int i = 0; i < list.size(); i++) {
      System.out.printf("%s\t", list.get(i));
      if ((i + 1) % 13 == 0) {
        System.out.println(" ");
      }
    }
    System.out.println();
    Collections.shuffle(list);
    System.out.println(" After the shuffle :");
    for (int i = 0; i < list.size(); i++) {
      System.out.printf("%s\t", list.get(i));
      if ((i + 1) % 13 == 0) {
        System.out.println(" ");
      }
    }
  }
}

Output:


♥A ♠2 ♣3 ♦4 ♥5 ♠6 ♣7 ♦8 ♥9 ♠10 ♣J ♦Q ♥K  
♠A ♣2 ♦3 ♥4 ♠5 ♣6 ♦7 ♥8 ♠9 ♣10 ♦J ♥Q ♠K  
♣A ♦2 ♥3 ♠4 ♣5 ♦6 ♥7 ♠8 ♣9 ♦10 ♥J ♠Q ♣K  
♦A ♥2 ♠3 ♣4 ♦5 ♥6 ♠7 ♣8 ♦9 ♥10 ♠J ♣Q ♦K  

 After the shuffle :
♥3 ♥9 ♦6 ♥J ♦K ♥4 ♦8 ♥K ♦Q ♦5 ♣7 ♠J ♠A  
♦10 ♣A ♥8 ♠9 ♥Q ♦4 ♠6 ♠8 ♥10 ♣2 ♣10 ♦7 ♠10 
♥A ♣J ♠K ♠5 ♥2 ♣8 ♦J ♠Q ♦3 ♦9 ♣Q ♣K ♣3  
♥5 ♣6 ♣5 ♦2 ♦A ♥7 ♠4 ♥6 ♠7 ♣4 ♠3 ♠2 ♣9

For more information about java algorithm, please visit Java Data Structure and Algorithm Tutorial, Java Operation of DOM Node Skills Summary, Java File and Directory Operation Skills Summary and Java Cache Operation Skills Summary.

I hope this article has been helpful in java programming.


Related articles: