I’ve learned Java for 1 month. This time, I’d like to create a poker game. There are two questions about my program. I hope somebody can help me to fix it.
For your first question, you could map a card name to its value:
Map values = new HashMap();
values.put("A", 11);
values.put("2", 2);
// ...
values.put("King", 10);
// lookup a value:
int kingValue = values.get("King");
For the second, I'd rely on java shuffle function. Create a list of all cards
ArrayList deck = someMagicMethodToPopulateTheList();
and then simply shuffle the deck
Collections.shuffle(deck);
Now you can iterate through the deck and you will see the cards in a random order:
for (Card card:deck) {
System.out.println(card);
}
Note: the Card
class does not extist (yet). I simply invented the name to keep it simple)