0

I am making a poker game and to generate a hand that contains non-duplicated I decided to use a HashSet and for-loop to give the player 5 cards.

public class CardDriver {

    public static void main(String[] args) {

        Set<Card> hands = new HashSet<Card>();

        while(hands.size()!= 5) {
            hands.add(new Card());
        }

        System.out.println("Player 1: " + hands);



    }   
}

The Card constructor:

public Card() {

        Random nb = new Random();
        int switchNb = nb.nextInt(13) + 1;

        switch(switchNb)
        {
        case 1: value = Value.Ace; break;
        case 2: value = Value.Two; break;

        ....

        case 11: value = Value.Jack; break;
        case 12: value = Value.Queen; break;
        case 13: value = Value.King; break;
        }

        switchNb = nb.nextInt(4) + 1;

        switch(switchNb)
        {
        case 1: suite = Suite.Hearts; break;
        case 2: suite = Suite.Diamonds; break;
        case 3: suite = Suite.Clubs; break;
        case 4: suite = Suite.Spades; break;
        }


    }

I am faced with three problems and I believe that both can be solved once I figure out how to isolate certain elements (if it is even possible) from the player's hand.

First off I need to use the same HashSet to give 5 more cards to the second player and so on. For example I would have elements 1-5 of the Set go to player 1 and 6-10 go to player 2.

Also, by being able to isolate a card from the set, it would also enable me to replace a card (video poker).

Lastly, I figured that by being able to isolate each card in the user's hand, it would make it easier for me to analyze the value of the hand each player has. I am not very experience when it comes to using sets so perhaps it would be better to use an array?

Edit: Example of an output: Player 1: [Three of Hearts, King of Clubs, Ten of Clubs, Two of Spades, Ace of Hearts]

t3rrh42d2
  • 134
  • 1
  • 2
  • 10
  • 1
    I don't understand what you mean by "isolate a card". – Chris Bogart Jul 19 '14 at 03:59
  • 1
    Have you considered constructing a Deck object and simply dealing to each hands as needed? Also if Suite and Value are enums you can just do Suite.values(i) – vandale Jul 19 '14 at 04:01
  • 1
    why not use a class called something like... `Deck`, this class will contain 52 cards (one of each) and when one is given out it is transfered from the `Deck` to the `Hand` – ug_ Jul 19 '14 at 04:02
  • Isolate an element of the set. For example if my output is Player 1: [Three of Hearts, King of Clubs, Ten of Clubs, Two of Spades, Ace of Hearts], I want to be able to take the Ten of Clubs and change it to a different card or I want to only display the last 3 cards instead of all 5 – t3rrh42d2 Jul 19 '14 at 04:02
  • In situations where you care about position in the hand (e.g. "the last three cards"), then you're right that an array would be better than a set, because you could refer to them with an array index. – Chris Bogart Jul 19 '14 at 04:10
  • Your code seems to potentially deal the same card more then once because you are generating the cards independently of each other. As others say, you need to implement Deck. I wold use an ArrayList and really take the drawn cards out of it. – Harald Jul 19 '14 at 05:36

0 Answers0