0

Context: I am trying to make a poker program that gives 5 cards from a deck of cards. I have a constructor that gives random cards and an array in a for loop that calls the constructor to give the user 5 cards. However i do not know how to set it up so that two cards do not have the same value && suit. So far I have tried putting a do/while loop in the for loop to no avail. This is part of the project:

The main class

public class pokerMain {

    public static void main(String[] args) {

        Scanner key = new Scanner(System.in);

    pokerHand[] card = new pokerHand[5];

    System.out.println("Would you like 5 random cards?");

    if(key.next().equals("yes"))
    {
        for(int i = 0; i < 5; i++)
        {
            card[i] = new pokerHand();
            System.out.println(card[i]);
        } 
    }



}

}

and the class that contains the constructor

public class pokerHand {

    private int value;
    private String suit;

    //gives a random card
    public pokerHand()
    {
        Random card = new Random();
        value = card.nextInt(13) + 1;
        int suitNb = card.nextInt(4) + 1;

        switch(suitNb)
        {
        case 1: suit = "hearts"; break;
        case 2: suit = "spades"; break;
        case 3: suit = "diamonds"; break;
        case 4: suit = "clubs"; break;
        }

    }
t3rrh42d2
  • 124
  • 2
  • 9
  • Don't you need a "Deck" for each game that consists of 52 cards? Otherwise even if you make sure there aren't any duplicates in a particular hand, different players could be holding the same cards. – Cameron Jul 16 '14 at 04:38
  • Yeah that would probably be a better idea, however I am still new to all of this so I am trying to work with what I have which is not much. Regardless if I run into problems later on, the information I can learn from this question will be very valuable to me in the future :). – t3rrh42d2 Jul 16 '14 at 04:43
  • I understand. Another callout is that your pokerHand class is really just a pokerCard so you might want to rename it. – Cameron Jul 16 '14 at 05:23

2 Answers2

1

Here are some ideas to implement the deck of cards.

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

public enum Value {
    ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE;
}

public class Card
{
    private final Suite suite;
    private final Value value;

    public Card(final Suite suite, final Value value)
    {
        this.suite = suite;
        this.value = value;
    }

    @Override
    public boolean equals(final Object obj)
    {
        if (this == obj)
        {
            return true;
        }
        if (obj == null)
        {
            return false;
        }
        if (getClass() != obj.getClass())
        {
            return false;
        }
        final Card other = (Card) obj;
        if (suite != other.suite)
        {
            return false;
        }
        if (value != other.value)
        {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode()
    {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((suite == null) ? 0 : suite.hashCode());
        result = prime * result + ((value == null) ? 0 : value.hashCode());
        return result;
    }

    @Override
    public String toString()
    {
        return "[" + value + " of " + suite + "]";
    }
}

public class Deck
{
    public static void main(final String[] args)
    {
        final Deck deck = new Deck();
        for (int i = 0; i < 5; i++)
        {
            System.out.println(deck.deal());
        }
    }

    List<Card> cards = new ArrayList<Card>();

    public Deck()
    {
        // initialise
        for (final Suite suite : Suite.values())
        {
            for (final Value value : Value.values())
            {
                final Card card = new Card(suite, value);
                cards.add(card);
            }
        }
    }

    public Card deal()
    {
        final Random random = new Random(System.nanoTime());
        if (cards.size() > 0)
        {
            return cards.remove(random.nextInt(cards.size()));
        }
        else
        {
            return null;
        }
    }

    @Override
    public String toString()
    {
        return cards.toString();
    }
} 
fipple
  • 360
  • 1
  • 5
0

There are a few ways that you could implement duplicate checking but probably the cleanest way to do it is to leverage the fact that Sets cannot contain duplicates. So first, override hashCode() and equals() in your pokerHand class (you can see how that might be done, for instance, in this question). Then in your main method, put the cards in a Set instead of an array:

if(key.next().equals("yes"))
{
    Set<pokerHand> hands = new HashSet<pokerHands>();
    while (hands.size() < 5) {
      hands.add(new pokerHand());
    }

    for (pokerHand hand : hands) {
      System.out.println(hand);
    }
}

There is a drawback to this, which is that it technically leaves you open to the possibility of an infinite loop if the pokerHand constructor keeps returning elements that are already in the set. Of course in practice that would never happen as long as your pokerHand constructor is properly returning random combinations. The underlying problem is that it's impossible to know what is already in the player's hand when you construct a new playerHand object.

Community
  • 1
  • 1
Cameron
  • 1,804
  • 3
  • 20
  • 37