How to print deck of cards in Java

In this tutorial, we will see how to print a deck of cards using Java. As we all know, there are 52 cards in the deck. Each of the card has a rank and belongs to one of the four suits. Let’s see.

Java program to print deck of cards

In this program, we will first define two arrays for suits and ranks respectively. Since we already know that the length of deck is 52, we will define another array to represent our deck of cards of size 52.

After that, we will initialize our deck with default values by iterating over ranks and suits in a nested loop.




While initializing the array, have a look at the index carefully. I have used a single dimension array for the deck here. To ensure that all the ranks are stored sequentially, I have multiplied i by the length of the suits array and added j for assignment of each suit.

Java code to print deck of cards:

public class Prog { 
    public static void main(String[] args) { 
        String[] card_rank = new String[13];
        for(int i=1;i<card_rank.length;i++){
            if(i==1)
            card_rank[i]="Ace";
            else if(i==11)
            card_rank[i]="Jack";
            else if(i==12)
            card_rank[i]="Queen";
            else if(i==13)
            card_rank[i]="King";
            else 
            card_rank[i]=Integer.toString(i);
        }
        String[] card_suit = {"Clubs", "Hearts", "Spades", "Diamonds"}; 
        int len = 52; 
        String[] cards_deck = new String[len]; 
        int rank_len=card_rank.length;
        int suit_len=card_suit.length;
        for (int i=0; i<rank_len; i++) { 
            for (int j=0; j<suit_len; j++) { 
                cards_deck[(suit_len)*i + j] = card_rank[i] + " of " + card_suit[j];
                }}
                for (int i=0; i<len; i++) { 
                    int index = i + (int) (Math.random() * (len-i)); 
                    String temp = cards_deck[index]; 
                    cards_deck[index] = cards_deck[i]; 
                    cards_deck[i] = temp;
                    } 
                    for (int i=0; i<len; i++) 
                    System.out.println(cards_deck[i]);    
    } }

 

Shuffling of the deck:

Further, the deck will be shuffled for each execution of the program.

We shuffle the deck of cards by swapping each card at random with the cards that occur after its position in the given array using Math.random() function.

The Math.random() function returns a floating-point number between the range [0,1) , where 0 (inclusive) and 1 (exclusive).This random number can be scaled according to our desired range. Here, I have multiplied the method by (len-i) so that the scaled number ranges from 0 to (len-i), where len is length of the deck.

Output:

4 of Diamonds
3 of Spades
Queen of Hearts
9 of Clubs
King of Clubs
.....
5 of Spades
4 of Spades
King of Hearts

Also read,

How to create a Puzzle game with java


Leave a Reply

Your email address will not be published. Required fields are marked *