Search code examples
javaenumsplaying-cards

How to loop using enum just for certain portions(face cards) in a deck of cards


I am creating a deck of poker cards (52 cards). I want to be able to print it out for example:

2 of Club, 3 of Club...... 
2 of Diamond, 3 of Diamond.... 
Queen of Diamond, King of Diamond 
and so on for the 52 cards. 

I am able to do this now but having problem when it comes to the face cards which are the Jack, Queen, King and Ace. Currently I am using numbers to represent them. so instead of printing Jack of Clubs, it shows as 11 of Clubs which is wrong. I tried to store the face cards in an enum and tried to loop them but can't really get around to do it.

Can I get some advice on how I could get my face cards in instead of representing them as numbers. I have attached my main method and the class below. Thanks for help.

//Card Class
import java.util.Objects;

public class Card {

    public enum Suits{
        CLUBS, DIAMONDS, HEARTS, SPADES;  
    }

    public enum Faces{
        JACK, QUEEN, KING, ACE;  
    }

    private int rank;
    private String suit; 

    public Card(int rank, String suit){ 
        this.rank = rank;
        this.suit = suit;
    }

    public int getRank(){
        return rank; 
    }

    public String getSuit(){
        return suit;
    }

    public String format(){
        return String.format("%d of %s, ", getRank(), getSuit()); 
    }
}

//Main method
public class CardTester {

    public static void main(String[] args) {

        Card[] cards = new Card[52];  

        int i = 0;
        for (Card.Suits suit : Card.Suits.values()) { 
            for (int y = 2; y < 15; y++) {
                cards[i] = new Card(y, suit.name());  
                i++; 
            }
        }      
        for(Card p : cards){ 
            System.out.print(p.format() + " "); 
        }
    }
}

Solution

  • Change your format() method to:

    public String format(){
        if (getRank() < 11) {
            return String.format("%d of %s, ", getRank(), getSuit());
        }
        else {
            Faces face = Faces.values()[getRank() - 11];
            return String.format("%s of %s, ", face, getSuit());
        }
    }
    

    Alternatively, here's a better implementation for Card:

    import java.util.Objects;
    
    public class Card {
    
        public enum Suit {
            CLUBS, DIAMONDS, HEARTS, SPADES;
        }
    
        public enum Rank {
            TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, 
            JACK, QUEEN, KING, ACE;
        }
    
        private final Suit suit;
        private final Rank rank;
    
        public Card(Suit suit, Rank rank) {
            this.suit = suit;
            this.rank = rank;
        }
    
        public Suit getSuit(){
            return suit;
        }
    
        public Rank getRank(){
            return rank; 
        }
    
        @Override
        public String toString() {
            return rank + " of " + suit;
        }
    
        @Override
        public int hashCode() {
            int hash = 5;
            hash = 97 * hash + Objects.hashCode(this.suit);
            hash = 97 * hash + Objects.hashCode(this.rank);
            return hash;
        }
    
        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Card other = (Card) obj;
            if (this.suit != other.suit) {
                return false;
            }
            if (this.rank != other.rank) {
                return false;
            }
            return true;
        }
    }
    

    You can use it like this:

    import java.util.ArrayList;
    import java.util.List;
    
    public class CardTester {
    
        public static void main(String[] args) {
    
            List<Card> cardList = new ArrayList<>();
            Card lastCard = new Card(Card.Suit.SPADES, Card.Rank.ACE);
    
            for (Card.Suit suit : Card.Suit.values()) { 
                for (Card.Rank rank : Card.Rank.values()) {
    
                    Card card = new Card(suit, rank);
                    cardList.add(card);
    
                    if (!card.equals(lastCard)) {
                        System.out.print(card + ", ");
                    }
                    else {
                        System.out.print(card);
                    }
                }
            }
    
            // use cardList
        }
    }