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]