質問

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() + " "); 
        }
    }
}
役に立ちましたか?

解決

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
    }
}

他のヒント

Your modelling of cards is flawed, and your problem highlights this. Rank is not numeric and should not be an 'int'. You might want to use an enum for the rank.

You might be tempted to give your rank an integer value, but this is probably not a good idea. The value is generally defined by the game you are playing, and not by the deck of cards. For example in poker a king is ranked higher than a jack, but in blackjack they are ranked the same.

May be source code shown below will help you:

public enum Faces
{
    JACK( 1, "Jack" ), QUEEN( 2, "Queen" ), KING( 3, "King" ), ACE( 4, "Ace" );

    public int GetNumber()
    {
        return m_number;

    }

    public String GetName()
    {
        return m_name;

    }

    private Faces( int number_, String name_ )
    {
        m_number    = number_;
        m_name      = name_;

    }

    private int     m_number;
    private String  m_name;

}

you may delete the enum Faces you already have and add the following method to class Card:

   public String getFace(int rank){
    String result = "";
    switch (rank){
        case 11: result = "Jack"; break;
        case 12: result = "QUEEN"; break;
        case 13: result = "KING"; break;
        case 14: result = "ACE"; break;
        default:{
            result = Integer.toString(rank);
            break;
        }
    }
    return result;
}

and modify your format() method to look like this:

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

This will do the job ;-)

A card consists of RANK and SUIT. Common poker software convention is to represent cards as SUIT+RANK Strings, as in 3c for 3 of clubs and Kd for king of diamonds. (see pokerstove or most hand history producers and consumers)

You need to expand the concept of RANK beyond faces. I've been using the Steve Brecher poker eval library, and his RANK class looks like this:

A simple lookup of the string "23456789TJQKA" converts between the enum and the RANK. There is a similar SUIT enum and a pretty reasonable Comparable class that combines the two.

public static enum Rank {
    TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE;

    /**
     * @return the character in {@link #RANK_CHARS} denoting this rank.
     */
    public char toChar() {
        return RANK_CHARS.charAt(this.ordinal());
    }

    /**
     * @param c
     *            a character present in {@link #RANK_CHARS} (case
     *            insensitive)
     * @return the Rank denoted by character.
     * @throws IllegalArgumentException
     *             if c not in {@link #RANK_CHARS}
     */
    public static Rank fromChar(char c) {

        int i = RANK_CHARS.indexOf(Character.toUpperCase(c));
        if (i >= 0)
            return Rank.values()[i];
        throw new IllegalArgumentException("'" + c + "'");
    }

    /**
     * @return the pip value of this Rank, ranging from 2 for a
     *         <code>TWO</code> (deuce) to 14 for an <code>ACE</code>.
     */
    public int pipValue() {
        return this.ordinal() + 2;
    }

    public static final String RANK_CHARS = "23456789TJQKA";
}

You can find his old but still perfectly serviceable libraries here: http://www.brecware.com/Software/software.html or http://pokersource.sourceforge.net/

U can try this solution which uses enum. First of all you need to create two enums. One for suits and the other for cards.

The enum for suits is:

 public enum suits
     {
     club, diamond,heart,spades
     }

The enum for cards is:

 public enum cards 
     {

     two(2), three(3), four(4), five(5), six(6), seven(7), eight(8),nine(9),
     ten(10), jack(11), queen(12), king(13),ace(14);
     int number;
     private cards(int number)
     {
         this.number=number;
     }
     public int getNumber()
     {
         return this.number;
     }
     }

THen in your main class you can loop through both the enums to get your result as follows:

public class Javaclass
    {

     public static void main(String[] args)
        {
            int count=2;
            for(suits suit:suits.values())
            {
            for(cards card: cards.values())
            {
            if(count<=10)
            System.out.println(card.getNumber()+" of "+suit);
            else
            System.out.println(card+" of "+suit);
            count++;
            }
            count=2;
             System.out.println("");
            }

        }
}

Hope this helps.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top